Browse Source

fixed total charge bug (#529)

* fixed total charge bug, when maximum value is exceeded the total will now continue to work

* Remove binding.pry left accidentally
pull/534/head
Ted-U 3 years ago committed by GitHub
parent
commit
06bb64d20e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 10
      app/frontend/controllers/numeric_question_controller.js
  2. 15
      spec/features/form/progressive_total_field_spec.rb
  3. 1
      spec/fixtures/forms/2021_2022.json

10
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
}

15
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

1
spec/fixtures/forms/2021_2022.json vendored

@ -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": [

Loading…
Cancel
Save