Skip to content

Commit

Permalink
Make organisation field searchable
Browse files Browse the repository at this point in the history
This upgrades the organisations field in the page for inviting a new
user to use the accessible autocomplete module (with progressive
enhancement)
  • Loading branch information
yndajas committed Sep 17, 2024
1 parent 0cf1550 commit 4cb1dae
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 6 deletions.
23 changes: 17 additions & 6 deletions app/views/devise/invitations/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,23 @@
autocomplete: "off",
} %>

<%= render "govuk_publishing_components/components/select", {
id: "user_organisation_id",
name: "user[organisation_id]",
label: "Organisation",
options: options_for_organisation_select(selected: f.object.organisation_id)
} %>
<div data-module="accessible-autocomplete">
<%= render "govuk_publishing_components/components/select", {
id: "user_organisation_id",
name: "user[organisation_id]",
label: "Organisation",
options: options_for_organisation_select(selected: f.object.organisation_id)
} %>

<div class="govuk-button-group">
<%= render "govuk_publishing_components/components/button", {
text: "Clear selection",
type: "button",
classes: "js-autocomplete__clear-button",
secondary_solid: true
} %>
</div>
</div>

<% if policy(User).assign_role? %>
<%= render "govuk_publishing_components/components/select", {
Expand Down
128 changes: 128 additions & 0 deletions test/integration/inviting_users_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,132 @@ class InvitingUsersTest < ActionDispatch::IntegrationTest
end
end
end

context "with JavaScript enabled" do
setup do
use_javascript_driver

create(:organisation, name: "ABCDEF")
create(:organisation, name: "GHIJKL")
@organisation = create(:organisation, name: "MNOPQR")
create(:organisation, name: "STUVWX")
create(:organisation, name: "YZ1234")

superadmin = create(:superadmin_user)
visit root_path
signin_with(superadmin)

visit new_user_invitation_path
fill_in "Name", with: "H from Steps"
fill_in "Email", with: "h@from.steps"
select "Superadmin", from: "Role"

@autocomplete_input_element = find(".autocomplete__input")
@select_element = find("#user_organisation_id-select", visible: false)
end

should "be able to invite a user" do
assert_select_with_autocomplete(
autocomplete_input_element: @autocomplete_input_element,
select_element: @select_element,
option_text: @organisation.name,
option_value: @organisation.id.to_s,
unique_partial_string: "MNO",
)

click_button "Create user and send email"

new_user = User.find_by(email: "h@from.steps", role: Roles::Superadmin.name)
assert_not_nil new_user
assert_equal new_user.organisation, @organisation
end

should("not show an 'Add' button") { assert_no_selector "button", text: "Add" }

should "reset the value of the select element when it no longer matches what's shown in the autocomplete input" do
assert_select_with_autocomplete(
autocomplete_input_element: @autocomplete_input_element,
select_element: @select_element,
option_text: @organisation.name,
option_value: @organisation.id.to_s,
unique_partial_string: "MNO",
)

assert_resets_select_when_desynced_with_autocomplete(
autocomplete_input_element: @autocomplete_input_element,
select_element: @select_element,
option_text: @organisation.name,
unique_partial_string: "MNOP",
)

click_button "Create user and send email"

new_user = User.find_by(email: "h@from.steps", role: Roles::Superadmin.name)
assert_not_nil new_user
assert_nil new_user.organisation
end

should "clear the value of the select and autocomplete elements when clicking the clear button" do
assert_select_with_autocomplete(
autocomplete_input_element: @autocomplete_input_element,
select_element: @select_element,
option_text: @organisation.name,
option_value: @organisation.id.to_s,
unique_partial_string: "MNO",
)

assert_clear_autocomplete_selection_by_click(
autocomplete_input_element: @autocomplete_input_element,
select_element: @select_element,
)

click_button "Create user and send email"

new_user = User.find_by(email: "h@from.steps", role: Roles::Superadmin.name)
assert_not_nil new_user
assert_nil new_user.organisation
end

should "clear the value of the select and autocomplete elements when hitting space on the clear button" do
assert_select_with_autocomplete(
autocomplete_input_element: @autocomplete_input_element,
select_element: @select_element,
option_text: @organisation.name,
option_value: @organisation.id.to_s,
unique_partial_string: "MNO",
)

assert_clear_autocomplete_selection_by_space(
autocomplete_input_element: @autocomplete_input_element,
select_element: @select_element,
)

click_button "Create user and send email"

new_user = User.find_by(email: "h@from.steps", role: Roles::Superadmin.name)
assert_not_nil new_user
assert_nil new_user.organisation
end

should "clear the value of the select and autocomplete elements when hitting enter on the clear button" do
assert_select_with_autocomplete(
autocomplete_input_element: @autocomplete_input_element,
select_element: @select_element,
option_text: @organisation.name,
option_value: @organisation.id.to_s,
unique_partial_string: "MNO",
)

assert_clear_autocomplete_selection_by_enter(
autocomplete_input_element: @autocomplete_input_element,
select_element: @select_element,
)

click_button "Create user and send email"

new_user = User.find_by(email: "h@from.steps", role: Roles::Superadmin.name)
assert_not_nil new_user
assert_nil new_user.organisation
end
end
end

0 comments on commit 4cb1dae

Please sign in to comment.