Skip to content

Commit

Permalink
Add autocomplete helper module
Browse files Browse the repository at this point in the history
This helper module adds an assertion method that can be used across
specs that test our custom autocomplete behaviours

It's used here for updating permissions tests, and will be used in the
next commit in inviting users tests when adding autocomplete to the
organisation field
  • Loading branch information
yndajas committed Sep 19, 2024
1 parent 37b2f1e commit 993de39
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,16 @@ def assert_select_permission_to_grant_with_javascript

click_link "Update permissions for #{@application.name}"

@autocomplete_input = find(".autocomplete__input")
@autocomplete_input_element = find(".autocomplete__input")
@select_element = find("#new_permission_id-select", visible: false)
assert_equal "", @autocomplete_input.value
assert_equal "", @select_element.value

# when I type a few characters from a permission called "adding"
@autocomplete_input.fill_in with: "add"
autocomplete_option = find(".autocomplete__option")

# the autcomplete value reflects what I typed, a matching option appears, but the select element remains empty
assert_equal "add", @autocomplete_input.value
assert_equal @new_permission_to_grant.name, autocomplete_option.text
assert_equal "", @select_element.value

# when I click on the matching option
autocomplete_option.click

# the autocomplete and select elements reflect my selection
assert_equal @new_permission_to_grant.name, @autocomplete_input.value
assert_equal @new_permission_to_grant.id.to_s, @select_element.value
assert_select_with_autocomplete(
autocomplete_input_element: @autocomplete_input_element,
select_element: @select_element,
option_text: @new_permission_to_grant.name,
option_value: @new_permission_to_grant.id.to_s,
unique_partial_string: "add",
)
end

context "with apps that have more than eight permissions" do
Expand Down
28 changes: 28 additions & 0 deletions test/support/autocomplete_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module AutocompleteHelpers
def assert_select_with_autocomplete(
autocomplete_input_element:,
select_element:,
option_text:,
option_value:,
unique_partial_string:
)
assert_equal "", autocomplete_input_element.value
assert_equal "", select_element.value

# when I type a few characters from the option that are unique to that option
autocomplete_input_element.fill_in with: unique_partial_string
autocomplete_option = find(".autocomplete__option")

# the autcomplete value reflects what I typed, a matching option appears, but the select element remains empty
assert_equal unique_partial_string, autocomplete_input_element.value
assert_equal option_text, autocomplete_option.text
assert_equal "", select_element.value

# when I click on the matching option
autocomplete_option.click

# the autocomplete and select elements reflect my selection
assert_equal option_text, autocomplete_input_element.value
assert_equal option_value, select_element.value
end
end
2 changes: 2 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def assert_not_authorised
require "support/removing_access_helpers"
require "support/updating_permissions_helpers"
require "support/flash_helpers"
require "support/autocomplete_helpers"

class ActiveRecord::Base
mattr_accessor :shared_connection
Expand Down Expand Up @@ -117,6 +118,7 @@ class ActionDispatch::IntegrationTest
include RemovingAccessHelpers
include UpdatingPermissionsHelpers
include FlashHelpers
include AutocompleteHelpers

def assert_response_contains(content)
assert page.has_content?(content), "Expected to find '#{content}' in:\n#{page.text}"
Expand Down

0 comments on commit 993de39

Please sign in to comment.