Browse Source

CLDC-2953 Clear conditional values with errors (#2074)

* Clear conditional values with errors

* Fix dates and refactor
pull/2076/head
kosiakkatrina 1 year ago committed by GitHub
parent
commit
a1ac31c19a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 25
      app/frontend/controllers/conditional_question_controller.js
  2. 44
      spec/features/form/conditional_questions_spec.rb

25
app/frontend/controllers/conditional_question_controller.js

@ -15,24 +15,27 @@ export default class extends Controller {
Object.entries(conditionalFor).forEach(([targetQuestion, conditions]) => { Object.entries(conditionalFor).forEach(([targetQuestion, conditions]) => {
if (!conditions.map(String).includes(String(selectedValue))) { if (!conditions.map(String).includes(String(selectedValue))) {
const textNumericInput = document.getElementById(`${type}-${targetQuestion.replaceAll('_', '-')}-field`) const textNumericInput = document.getElementById(`${type}-${targetQuestion.replaceAll('_', '-')}-field`)
if (textNumericInput == null) { const errorInput = document.getElementById(`${type}-${targetQuestion.replaceAll('_', '-')}-field-error`)
const dateInputs = [1, 2, 3].map((idx) => { const dateInputs = [1, 2, 3].map((idx) => {
return document.getElementById(`${type.replaceAll('-', '_')}_${targetQuestion}_${idx}i`) return document.getElementById(`lettings_log_mrcdate_${idx}i`)
}) })
this.clearDateInputs(dateInputs) this.clearTextInput(textNumericInput)
} else { this.clearTextInput(errorInput)
this.clearTextNumericInput(textNumericInput) this.clearDateInputs(dateInputs)
}
} }
}) })
} }
} }
clearTextNumericInput (input) { clearTextInput (input) {
input.value = '' if (input != null) {
input.value = ''
}
} }
clearDateInputs (inputs) { clearDateInputs (inputs) {
inputs.forEach((input) => { input.value = '' }) inputs.forEach((input) => {
this.clearTextInput(input)
})
} }
} }

44
spec/features/form/conditional_questions_spec.rb

@ -33,12 +33,15 @@ RSpec.describe "Form Conditional Questions" do
before do before do
sign_in user sign_in user
allow(sales_log.form).to receive(:new_logs_end_date).and_return(Time.zone.today + 1.day)
allow(lettings_log.form).to receive(:new_logs_end_date).and_return(Time.zone.today + 1.day)
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)
end end
context "with a page where some questions are only conditionally shown, depending on how you answer the first question" do context "with a page where some questions are only conditionally shown, depending on how you answer the first question" do
before do
allow(sales_log.form).to receive(:new_logs_end_date).and_return(Time.zone.today + 1.day)
allow(lettings_log.form).to receive(:new_logs_end_date).and_return(Time.zone.today + 1.day)
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)
end
it "initially hides conditional questions" do it "initially hides conditional questions" do
visit("/lettings-logs/#{id}/armed-forces") visit("/lettings-logs/#{id}/armed-forces")
expect(page).not_to have_selector("#armed_forces_injured_div") expect(page).not_to have_selector("#armed_forces_injured_div")
@ -57,6 +60,12 @@ RSpec.describe "Form Conditional Questions" do
end end
context "when a conditional question has a saved answer", js: true do context "when a conditional question has a saved answer", js: true do
before do
allow(sales_log.form).to receive(:new_logs_end_date).and_return(Time.zone.today + 1.day)
allow(lettings_log.form).to receive(:new_logs_end_date).and_return(Time.zone.today + 1.day)
allow(FormHandler.instance).to receive(:current_lettings_form).and_return(fake_2021_2022_form)
end
it "is displayed correctly" do it "is displayed correctly" do
lettings_log.update!(postcode_known: 1, postcode_full: "NW1 6RT") lettings_log.update!(postcode_known: 1, postcode_full: "NW1 6RT")
visit("/lettings-logs/#{id}/property-postcode") visit("/lettings-logs/#{id}/property-postcode")
@ -73,4 +82,33 @@ RSpec.describe "Form Conditional Questions" do
expect(page).to have_field("sales-log-age1-field", with: "") expect(page).to have_field("sales-log-age1-field", with: "")
end end
end end
context "when a conditional question has an error" do
let(:lettings_log) do
FactoryBot.create(
:lettings_log,
:completed,
created_by: user,
)
end
before do
FormHandler.instance.use_real_forms!
end
it "shows conditional questions if the required answer is selected and hides it again when a different answer option is selected", js: true do
visit("/lettings-logs/#{id}/lead-tenant-age")
choose("lettings-log-age1-known-0-field", allow_label_click: true)
fill_in("lettings-log-age1-field", with: "200")
click_button("Save and continue")
expect(page).not_to have_field("lettings-log-age1-field")
expect(page).to have_field("lettings-log-age1-field-error")
choose("lettings-log-age1-known-1-field", allow_label_click: true)
expect(page).not_to have_field("lettings-log-age1-field")
expect(page).not_to have_field("lettings-log-age1-field-error")
choose("lettings-log-age1-known-0-field", allow_label_click: true)
expect(page).not_to have_field("lettings-log-age1-field")
expect(page).to have_field("lettings-log-age1-field-error", with: "")
end
end
end end

Loading…
Cancel
Save