Browse Source

Display correct calculated total charge

pull/250/head
Kat 3 years ago
parent
commit
3bff0d78d9
  1. 2
      app/webpacker/controllers/numeric_question_controller.js
  2. 4
      spec/features/form/progressive_total_field_spec.rb
  3. 14
      spec/features/form/saving_data_spec.rb

2
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;
}

4
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

14
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

Loading…
Cancel
Save