diff --git a/app/frontend/controllers/numeric_question_controller.js b/app/frontend/controllers/numeric_question_controller.js index ae2830750..391bec630 100644 --- a/app/frontend/controllers/numeric_question_controller.js +++ b/app/frontend/controllers/numeric_question_controller.js @@ -11,9 +11,17 @@ export default class extends Controller { calculateFields() { const affectedField = this.element.dataset.target; const fieldsToAdd = JSON.parse(this.element.dataset.calculated).map(x => `case-log-${x.replaceAll("_","-")}-field`); - const valuesToAdd = fieldsToAdd.map(x => document.getElementById(x).value).filter(x => x); + const valuesToAdd = fieldsToAdd.map(x => getFieldValue(x)).filter(x => x); const newValue = valuesToAdd.map(x => parseFloat(x)).reduce((a, b) => a + b, 0).toFixed(2); const elementToUpdate = document.getElementById(affectedField); elementToUpdate.value = newValue; } + } + let getFieldValue = (field) => { + const elementFieldToAdd= document.getElementById(field) + if (elementFieldToAdd) { + return elementFieldToAdd.value + } + return document.getElementById(`${field}-error`).value } + diff --git a/spec/features/form/progressive_total_field_spec.rb b/spec/features/form/progressive_total_field_spec.rb index 23f1c6b63..2e9b0cdf9 100644 --- a/spec/features/form/progressive_total_field_spec.rb +++ b/spec/features/form/progressive_total_field_spec.rb @@ -30,4 +30,19 @@ RSpec.describe "Accessible Automcomplete" do fill_in("case-log-pscharge-field", with: 3) expect(find("#case-log-tcharge-field").value).to eq("8.00") end + + it "total displays despite error message", js: true do + visit("/logs/#{case_log.id}/rent") + fill_in("case-log-brent-field", with: 500) + fill_in("case-log-scharge-field", with: 50) + fill_in("case-log-pscharge-field", with: 50) + fill_in("case-log-supcharg-field", with: 5000) + expect(find("#case-log-tcharge-field").value).to eq("5600.00") + click_button("Save and continue") + expect(page).to have_selector(".govuk-error-summary") + fill_in("case-log-brent-field", with: 500) + expect(find("#case-log-tcharge-field").value).to eq("500.00") + fill_in("case-log-supcharg-field-error", with: 50) + expect(find("#case-log-tcharge-field").value).to eq("550.00") + end end diff --git a/spec/fixtures/forms/2021_2022.json b/spec/fixtures/forms/2021_2022.json index eb41a0819..1aa37557b 100644 --- a/spec/fixtures/forms/2021_2022.json +++ b/spec/fixtures/forms/2021_2022.json @@ -761,6 +761,7 @@ "hint_text": "This is to fund housing-related support services included in the tenancy agreement", "type": "numeric", "min": 0, + "max": 300, "step": 1, "width": 4, "fields-to-add": [