From dbef8dfba4fb091c4213c11aeb71a51153093f75 Mon Sep 17 00:00:00 2001 From: Daniel Baark <5101747+baarkerlounger@users.noreply.github.com> Date: Tue, 9 Nov 2021 13:36:52 +0000 Subject: [PATCH] CLDC-642: Soft validation UI bug fix (#82) * Soft validation UI needs to be on both questions being validated * Fix whether the box is checked or not * All checkboxes are integers now * Fix * Use enums as well for consistency * Use capybara matcher for less flakiness * Click link already waits * Add a retry to our fetch so tests are more resilient to intermittent network issues --- app/constants/db_enums.rb | 7 +++++++ .../controllers/soft_validations_controller.js | 14 +++++++++++--- app/models/case_log.rb | 1 + app/validations/soft_validations.rb | 2 +- .../form/_validation_override_question.html.erb | 3 ++- config/forms/2021_2022.json | 9 +++++++++ spec/features/case_log_spec.rb | 2 +- 7 files changed, 32 insertions(+), 6 deletions(-) diff --git a/app/constants/db_enums.rb b/app/constants/db_enums.rb index 536620915..84f7d3837 100644 --- a/app/constants/db_enums.rb +++ b/app/constants/db_enums.rb @@ -213,6 +213,13 @@ module DbEnums } end + def self.override_soft_validation + { + "No" => 0, + "Yes" => 1, + } + end + def self.benefits { "All" => 1, diff --git a/app/javascript/controllers/soft_validations_controller.js b/app/javascript/controllers/soft_validations_controller.js index 963dd7a76..a408e4460 100644 --- a/app/javascript/controllers/soft_validations_controller.js +++ b/app/javascript/controllers/soft_validations_controller.js @@ -5,8 +5,13 @@ export default class extends Controller { initialize() { let url = window.location.href + "/soft_validations" + this.fetch_retry(url, { headers: { accept: "application/json" } }, 2) + } + + fetch_retry(url, options, n) { + let self = this let div = this.overrideTarget - fetch(url, { headers: { accept: "application/json" } }) + fetch(url, options) .then(response => response.json()) .then((response) => { if(response["show"]){ @@ -22,7 +27,10 @@ export default class extends Controller { button.checked = false }) } - } - ) + }) + .catch(function(error) { + if (n === 1) throw error + return self.fetch_retry(url, options, n - 1) + }) } } diff --git a/app/models/case_log.rb b/app/models/case_log.rb index ac37461da..b77534795 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -65,6 +65,7 @@ class CaseLog < ApplicationRecord enum leftreg: DbEnums.leftreg, _suffix: true enum illness: DbEnums.illness, _suffix: true enum preg_occ: DbEnums.pregnancy, _suffix: true + enum override_net_income_validation: DbEnums.override_soft_validation, _suffix: true enum housingneeds_a: DbEnums.polar, _suffix: true enum housingneeds_b: DbEnums.polar, _suffix: true enum housingneeds_c: DbEnums.polar, _suffix: true diff --git a/app/validations/soft_validations.rb b/app/validations/soft_validations.rb index ce03fafc6..451988b66 100644 --- a/app/validations/soft_validations.rb +++ b/app/validations/soft_validations.rb @@ -8,7 +8,7 @@ module SoftValidations end def soft_errors_overridden? - !public_send(soft_errors.keys.first).zero? if soft_errors.present? + public_send(soft_errors.keys.first) == "Yes" if soft_errors.present? end private diff --git a/app/views/form/_validation_override_question.html.erb b/app/views/form/_validation_override_question.html.erb index 5e42b32b8..10a6a60e7 100644 --- a/app/views/form/_validation_override_question.html.erb +++ b/app/views/form/_validation_override_question.html.erb @@ -8,7 +8,8 @@ hint: { text: "soft-validations-placeholder-hint-text" } do %> <%= f.govuk_check_box page_info["soft_validations"]&.keys&.first, page_info["soft_validations"]&.keys&.first, - label: { text: "Yes" } + label: { text: "Yes" }, + checked: @case_log[page_info["soft_validations"]&.keys&.first] == "Yes" %> <% end %> diff --git a/config/forms/2021_2022.json b/config/forms/2021_2022.json index 498e064b2..5229155e0 100644 --- a/config/forms/2021_2022.json +++ b/config/forms/2021_2022.json @@ -315,6 +315,15 @@ "10": "Prefer not to say" } } + }, + "soft_validations": { + "override_net_income_validation": { + "check_answer_label": "Net income confirmed?", + "type": "validation_override", + "answer_options": { + "override_net_income_validation": "Yes" + } + } } }, "household_number_of_other_members": { diff --git a/spec/features/case_log_spec.rb b/spec/features/case_log_spec.rb index 160bb883b..0e2ae338b 100644 --- a/spec/features/case_log_spec.rb +++ b/spec/features/case_log_spec.rb @@ -431,7 +431,7 @@ RSpec.describe "Test Features" do fill_in("case-log-earnings-field", with: income_under_soft_limit) click_button("Save and continue") click_link(text: "Back") - expect(page).not_to have_content("Are you sure this is correct?") + expect(page).to have_no_content("Are you sure this is correct?") end it "does not clear the confirmation question if the page is returned to using the back button and the amount is still over the soft limit", js: true do