Skip to content

Commit

Permalink
Merge pull request #3179 from alphagov/use-autocomplete-on-other-orga…
Browse files Browse the repository at this point in the history
…nisation-fields

Use autocomplete on other organisation fields
  • Loading branch information
yndajas authored Sep 30, 2024
2 parents 7906b12 + c8fe56b commit cc52c88
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 49 deletions.
13 changes: 12 additions & 1 deletion app/views/account/organisations/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,23 @@
<div class="govuk-grid-column-two-thirds">
<% if policy(%i[account organisations]).update? %>
<%= form_for current_user, url: account_organisation_path do |f| %>
<%= render "govuk_publishing_components/components/select", {
<div data-module="accessible-autocomplete">
<%= render "govuk_publishing_components/components/select", {
id: "user_organisation_id",
name: "user[organisation_id]",
label: "Organisation",
options: options_for_your_organisation_select(current_user)
} %>

<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>
<%= render "govuk_publishing_components/components/button", {
text: "Change organisation"
} %>
Expand Down
14 changes: 12 additions & 2 deletions app/views/batch_invitations/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,23 @@
column, or the value is blank for a row, the user will be
assigned to this organisation instead." } %>

<%= render "govuk_publishing_components/components/select", {
<div data-module="accessible-autocomplete">
<%= render "govuk_publishing_components/components/select", {
id: "batch_invitation_organisation_id",
name: "batch_invitation[organisation_id]",
label: "Organisation",
options: policy_scope(Organisation).not_closed.order(:name).map { |organisation| { text: organisation.name_with_abbreviation, value: 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>
<% end %>

<%= render "govuk_publishing_components/components/button", {
Expand Down
25 changes: 18 additions & 7 deletions app/views/users/organisations/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,24 @@
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= form_for @user, url: user_organisation_path(@user) do %>
<%= render "govuk_publishing_components/components/select", {
id: "user_organisation_id",
name: "user[organisation_id]",
label: "Organisation",
options: options_for_organisation_select(selected: @user.organisation_id),
error_message: @user.errors[:organisation_id].any? ? @user.errors.full_messages_for(:organisation_id).to_sentence : nil
} %>
<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: @user.organisation_id),
error_message: @user.errors[:organisation_id].any? ? @user.errors.full_messages_for(:organisation_id).to_sentence : nil
} %>

<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>
<div class="govuk-button-group">
<%= render "govuk_publishing_components/components/button", {
text: "Change organisation",
Expand Down
39 changes: 0 additions & 39 deletions test/integration/account/organisations_test.rb

This file was deleted.

65 changes: 65 additions & 0 deletions test/integration/account/updating_organisation_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require "test_helper"

class Account::UpdatingOrganisationTest < ActionDispatch::IntegrationTest
should "display read-only values for users who aren't GOVUK Admins" do
organisation = create(:organisation, name: "Department for Viability")
non_govuk_admin_user = create(:super_organisation_admin_user, organisation:)

visit new_user_session_path
signin_with non_govuk_admin_user

visit edit_account_organisation_path

assert has_text? "Department for Viability"
assert_no_selector "select"
end

should "allow GOV.UK admin users to change their organisation" do
current_organisation = create(:organisation, name: "Judiciary")
user = create(:admin_user, organisation: current_organisation)

create(:organisation, name: "Postage")

visit new_user_session_path
signin_with user

visit edit_account_organisation_path

select "Postage", from: "Organisation"
click_button "Change organisation"

assert_current_url account_path
assert page.has_text? "Your organisation is now Postage"
assert_equal "Postage", user.reload.organisation_name
end

context "with JavaScript enabled" do
setup do
use_javascript_driver
end

should "allow GOV.UK admin users to change their organisation" do
current_organisation = create(:organisation, name: "Judiciary")
user = create(:admin_user, organisation: current_organisation)

create(:organisation, name: "Postage")

visit new_user_session_path
signin_with user

visit edit_account_organisation_path

autocomplete_helper = AutocompleteHelper.new
autocomplete_helper.clear_selection
autocomplete_helper.select_autocomplete_option("Postage")
click_button "Change organisation"

assert_current_url account_path
assert page.has_text? "Your organisation is now Postage"

visit edit_account_organisation_path

assert_equal "Postage", user.reload.organisation_name
end
end
end
21 changes: 21 additions & 0 deletions test/integration/batch_inviting_users_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,27 @@ class BatchInvitingUsersTest < ActionDispatch::IntegrationTest
end
end

context "with JavaScript enabled" do
setup do
use_javascript_driver
end

should "allow selecting an organisation and continuing to the next step" do
visit root_path
signin_with create(:superadmin_user)

visit new_batch_invitation_path
path = Rails.root.join("test/fixtures/users.csv")
attach_file("Upload a CSV file", path)
AutocompleteHelper.new.select_autocomplete_option(@cabinet_office.name)

click_button "Manage permissions for new users"
assert assert_selector "h1", text: "Manage permissions for new users"

assert_equal @cabinet_office, BatchInvitation.last.organisation
end
end

def perform_batch_invite_with_user(user, application, organisation:, fixture_file: "users.csv", user_count: 1)
perform_enqueued_jobs do
visit root_path
Expand Down
65 changes: 65 additions & 0 deletions test/integration/users/updating_organisation_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
require "test_helper"

class Users::UpdatingOrganisationTest < ActionDispatch::IntegrationTest
should "display read-only values for users who aren't GOVUK Admins" do
organisation = create(:organisation, name: "Department for Viability")
user = create(:user, organisation:)
non_govuk_admin_user = create(:super_organisation_admin_user, organisation:)

visit new_user_session_path
signin_with non_govuk_admin_user

visit edit_user_path(user)

assert has_text? "Department for Viability"
assert_no_selector "a", text: "Change Organisation"
end

should "allow GOV.UK admin users to change their organisation" do
current_organisation = create(:organisation, name: "Judiciary")
user = create(:user, organisation: current_organisation)
admin_user = create(:admin_user)

create(:organisation, name: "Postage")

visit new_user_session_path
signin_with admin_user

visit edit_user_organisation_path(user)

select "Postage", from: "Organisation"
click_button "Change organisation"

assert_current_url edit_user_path(user)
assert page.has_text? "Updated user #{user.email} successfully"
assert_equal "Postage", user.reload.organisation_name
end

context "with JavaScript enabled" do
setup do
use_javascript_driver
end

should "allow GOV.UK admin users to change their organisation" do
current_organisation = create(:organisation, name: "Judiciary")
user = create(:admin_user, organisation: current_organisation)
admin_user = create(:admin_user)

create(:organisation, name: "Postage")

visit new_user_session_path
signin_with admin_user

visit edit_user_organisation_path(user)

autocomplete_helper = AutocompleteHelper.new
autocomplete_helper.clear_selection
autocomplete_helper.select_autocomplete_option("Postage")
click_button "Change organisation"

assert_current_url edit_user_path(user)
assert page.has_text? "Updated user #{user.email} successfully"
assert_equal "Postage", user.reload.organisation_name
end
end
end
4 changes: 4 additions & 0 deletions test/support/autocomplete_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
class AutocompleteHelper
include Capybara::DSL

def clear_selection
click_button "Clear selection"
end

def select_autocomplete_option(option_string)
autocomplete_input_element = find(".autocomplete__input")
autocomplete_input_element.fill_in with: option_string
Expand Down

0 comments on commit cc52c88

Please sign in to comment.