Browse Source

Correctly add and display calculated total charge (#250)

* correctly add rent and charges

* Display correct calculated total charge
pull/253/head
kosiakkatrina 3 years ago committed by GitHub
parent
commit
fb9a78a608
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      app/models/case_log.rb
  2. 2
      app/webpacker/controllers/numeric_question_controller.js
  3. 4
      spec/features/form/progressive_total_field_spec.rb
  4. 14
      spec/features/form/saving_data_spec.rb
  5. 8
      spec/models/case_log_spec.rb

2
app/models/case_log.rb

@ -263,7 +263,7 @@ private
self.totchild = get_totchild
self.totelder = get_totelder
self.totadult = get_totadult
self.tcharge = brent.to_i + scharge.to_i + pscharge.to_i + supcharg.to_i
self.tcharge = brent.to_f + scharge.to_f + pscharge.to_f + supcharg.to_f
self.has_benefits = get_has_benefits
self.nocharge = household_charge == "Yes" ? "No" : "Yes"
end

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

8
spec/models/case_log_spec.rb

@ -1138,16 +1138,16 @@ RSpec.describe Form, type: :model do
CaseLog.create({
managing_organisation: organisation,
owning_organisation: organisation,
brent: 5,
scharge: 10,
brent: 5.77,
scharge: 10.01,
pscharge: 3,
supcharg: 12,
supcharg: 12.2,
})
end
it "correctly sums rental charges" do
record_from_db = ActiveRecord::Base.connection.execute("select tcharge from case_logs where id=#{case_log.id}").to_a[0]
expect(record_from_db["tcharge"]).to eq(30)
expect(record_from_db["tcharge"]).to eq(30.98)
end
end

Loading…
Cancel
Save