From c4ae32a0530b1f131ccfc6f4976877a7868cbe8a Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Mon, 11 Oct 2021 16:07:46 +0100 Subject: [PATCH] Make checkboxes checked if we have already saved an answer for them --- app/views/form/_checkbox_question.html.erb | 6 ++++- spec/features/case_log_spec.rb | 29 ++++++++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/app/views/form/_checkbox_question.html.erb b/app/views/form/_checkbox_question.html.erb index ccd50d8a3..1d9c548e8 100644 --- a/app/views/form/_checkbox_question.html.erb +++ b/app/views/form/_checkbox_question.html.erb @@ -6,7 +6,11 @@ <% if key.starts_with?("divider") %> <%= f.govuk_check_box_divider %> <% else %> - <%= f.govuk_check_box question_key, key, label: { text: val }, **stimulus_html_attributes(question) %> + <%= f.govuk_check_box question_key, key, + label: { text: val }, + checked: f.object.send(key), + **stimulus_html_attributes(question) + %> <% end %> <% end %> <% end %> diff --git a/spec/features/case_log_spec.rb b/spec/features/case_log_spec.rb index 238897c94..71532c3cb 100644 --- a/spec/features/case_log_spec.rb +++ b/spec/features/case_log_spec.rb @@ -96,6 +96,14 @@ RSpec.describe "Test Features" do end describe "form questions" do + let(:case_log_with_checkbox_questions_answered) do + FactoryBot.create( + :case_log, :in_progress, + accessibility_requirements_fully_wheelchair_accessible_housing: true, + accessibility_requirements_level_access_housing: true + ) + end + it "can be accessed by url" do visit("/case_logs/#{id}/tenant_age") expect(page).to have_field("case-log-tenant-age-field") @@ -146,6 +154,23 @@ RSpec.describe "Test Features" do visit("/case_logs/#{id}/tenant_age") expect(page).to have_field("case-log-tenant-age-field", with: "12") end + + it "displays checkbox answers in inputs if they are already saved" do + visit("/case_logs/#{case_log_with_checkbox_questions_answered.id}/accessibility_requirements") + # Something about our styling makes the selenium webdriver think the actual radio buttons are not visible so we pass false here + expect(page).to have_checked_field( + "case-log-accessibility-requirements-accessibility-requirements-fully-wheelchair-accessible-housing-field", + visible: false + ) + expect(page).to have_unchecked_field( + "case-log-accessibility-requirements-accessibility-requirements-wheelchair-access-to-essential-rooms-field", + visible: false + ) + expect(page).to have_checked_field( + "case-log-accessibility-requirements-accessibility-requirements-level-access-housing-field", + visible: false + ) + end end describe "Back link directs correctly" do @@ -258,11 +283,11 @@ RSpec.describe "Test Features" do choose("case-log-armed-forces-yes-a-regular-field", allow_label_click: true) expect(page).to have_selector("#armed_forces_injured_div") choose("case-log-armed-forces-injured-no-field", allow_label_click: true) - expect(find_field("case-log-armed-forces-injured-no-field", visible: false).checked?).to be_truthy + expect(page).to have_checked_field("case-log-armed-forces-injured-no-field", visible: false) choose("case-log-armed-forces-no-field", allow_label_click: true) expect(page).not_to have_selector("#armed_forces_injured_div") choose("case-log-armed-forces-yes-a-regular-field", allow_label_click: true) - expect(find_field("case-log-armed-forces-injured-no-field", visible: false).checked?).to be_falsey + expect(page).to have_unchecked_field("case-log-armed-forces-injured-no-field", visible: false) end end end