diff --git a/test/integration/updating_permissions_for_apps_with_many_permissions_test.rb b/test/integration/updating_permissions_for_apps_with_many_permissions_test.rb index 3b638931c..0cff95573 100644 --- a/test/integration/updating_permissions_for_apps_with_many_permissions_test.rb +++ b/test/integration/updating_permissions_for_apps_with_many_permissions_test.rb @@ -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 diff --git a/test/support/autocomplete_helpers.rb b/test/support/autocomplete_helpers.rb new file mode 100644 index 000000000..c03f3c4fd --- /dev/null +++ b/test/support/autocomplete_helpers.rb @@ -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 diff --git a/test/test_helper.rb b/test/test_helper.rb index e310ca3f2..2e50df3a5 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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 @@ -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}"