From 635f6a88bf1d24a165d7542bb08554d27aaa3c81 Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Tue, 9 Aug 2022 11:05:49 +0100 Subject: [PATCH] Ensure all checkbox selections are preserved not just the first that doesn't match the all --- app/controllers/form_controller.rb | 3 ++- spec/features/form/checkboxes_spec.rb | 15 ++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/app/controllers/form_controller.rb b/app/controllers/form_controller.rb index fd3972ac4..ad3f858e3 100644 --- a/app/controllers/form_controller.rb +++ b/app/controllers/form_controller.rb @@ -161,10 +161,11 @@ private def question_missing_response?(responses_for_page, question) if %w[checkbox validation_override].include?(question.type) - question.answer_options.keys.reject { |x| x.match(/divider/) }.all? do |option| + answered = question.answer_options.keys.reject { |x| x.match(/divider/) }.map do |option| session["fields"][option] = @case_log[option] = params["case_log"][question.id].include?(option) ? 1 : 0 params["case_log"][question.id].exclude?(option) end + answered.all? else session["fields"][question.id] = @case_log[question.id] = responses_for_page[question.id] responses_for_page[question.id].nil? || responses_for_page[question.id].blank? diff --git a/spec/features/form/checkboxes_spec.rb b/spec/features/form/checkboxes_spec.rb index 92555977c..bb6e17d8a 100644 --- a/spec/features/form/checkboxes_spec.rb +++ b/spec/features/form/checkboxes_spec.rb @@ -45,12 +45,21 @@ RSpec.describe "Checkboxes" do allow(case_log).to receive(:update).and_return(false) end - it "shows an error summary", js:true do + it "shows an error summary" do visit("/logs/#{id}/accessibility-requirements") - page.check("case-log-accessibility-requirements-housingneeds-a-field", allow_label_click: true) - page.check("case-log-accessibility-requirements-housingneeds-c-field", allow_label_click: true) + page.check("case-log-accessibility-requirements-housingneeds-a-field") + page.check("case-log-accessibility-requirements-housingneeds-c-field") click_button("Save and continue") expect(page).to have_title("Error") end + + it "persists the original selections" do + visit("/logs/#{id}/accessibility-requirements") + page.check("case-log-accessibility-requirements-housingneeds-a-field") + page.check("case-log-accessibility-requirements-housingneeds-c-field") + click_button("Save and continue") + expect(page).to have_checked_field("case-log-accessibility-requirements-housingneeds-a-field") + expect(page).to have_checked_field("case-log-accessibility-requirements-housingneeds-c-field") + end end end