From 3bff0d78d936b8c5e9ebe6efd40864b51a1e06f7 Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 26 Jan 2022 16:54:18 +0000 Subject: [PATCH] Display correct calculated total charge --- .../controllers/numeric_question_controller.js | 2 +- spec/features/form/progressive_total_field_spec.rb | 4 ++-- spec/features/form/saving_data_spec.rb | 14 +++++++------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/app/webpacker/controllers/numeric_question_controller.js b/app/webpacker/controllers/numeric_question_controller.js index 9bbdf6f9e..ae2830750 100644 --- a/app/webpacker/controllers/numeric_question_controller.js +++ b/app/webpacker/controllers/numeric_question_controller.js @@ -12,7 +12,7 @@ export default class extends Controller { 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 newValue = valuesToAdd.map(x => parseInt(x)).reduce((a, b) => a + b, 0); + const newValue = valuesToAdd.map(x => parseFloat(x)).reduce((a, b) => a + b, 0).toFixed(2); const elementToUpdate = document.getElementById(affectedField); elementToUpdate.value = newValue; } diff --git a/spec/features/form/progressive_total_field_spec.rb b/spec/features/form/progressive_total_field_spec.rb index ba5131b9f..4d30bef38 100644 --- a/spec/features/form/progressive_total_field_spec.rb +++ b/spec/features/form/progressive_total_field_spec.rb @@ -28,8 +28,8 @@ RSpec.describe "Accessible Automcomplete" do visit("/logs/#{case_log.id}/rent") expect(page).to have_selector("#tcharge_div", visible: true) fill_in("case-log-brent-field", with: 5) - expect(find("#case-log-tcharge-field").value).to eq("5") + expect(find("#case-log-tcharge-field").value).to eq("5.00") fill_in("case-log-pscharge-field", with: 3) - expect(find("#case-log-tcharge-field").value).to eq("8") + expect(find("#case-log-tcharge-field").value).to eq("8.00") end end diff --git a/spec/features/form/saving_data_spec.rb b/spec/features/form/saving_data_spec.rb index 4dbff2ebb..2c27dfda3 100644 --- a/spec/features/form/saving_data_spec.rb +++ b/spec/features/form/saving_data_spec.rb @@ -60,17 +60,17 @@ RSpec.describe "Form Saving Data" do it "updates total value of the rent", js: true do visit("/logs/#{id}/rent") - fill_in("case-log-brent-field", with: 3) - expect(page).to have_field("case-log-tcharge-field", with: "3") + fill_in("case-log-brent-field", with: 3.02) + expect(page).to have_field("case-log-tcharge-field", with: "3.02") - fill_in("case-log-scharge-field", with: 2) - expect(page).to have_field("case-log-tcharge-field", with: "5") + fill_in("case-log-scharge-field", with: 2.8) + expect(page).to have_field("case-log-tcharge-field", with: "5.82") fill_in("case-log-pscharge-field", with: 1) - expect(page).to have_field("case-log-tcharge-field", with: "6") + expect(page).to have_field("case-log-tcharge-field", with: "6.82") - fill_in("case-log-supcharg-field", with: 4) - expect(page).to have_field("case-log-tcharge-field", with: "10") + fill_in("case-log-supcharg-field", with: 4.11) + expect(page).to have_field("case-log-tcharge-field", with: "10.93") end it "displays number answers in inputs if they are already saved" do