Browse Source

fix the ids in numeric questions (#37)

pull/38/head
kosiakkatrina 3 years ago committed by GitHub
parent
commit
4378e83709
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      app/helpers/numeric_questions_helper.rb
  2. 2
      app/javascript/controllers/numeric_question_controller.js
  3. 16
      spec/features/case_log_spec.rb
  4. 2
      spec/helpers/numeric_questions_helper_spec.rb

2
app/helpers/numeric_questions_helper.rb

@ -5,7 +5,7 @@ module NumericQuestionsHelper
{ {
"data-controller": "numeric-question", "data-controller": "numeric-question",
"data-action": "numeric-question#calculateFields", "data-action": "numeric-question#calculateFields",
"data-target": "#{question['result-field'].to_s.dasherize}-field", "data-target": "case-log-#{question['result-field'].to_s.dasherize}-field",
"data-calculated": question["fields-to-add"].to_json, "data-calculated": question["fields-to-add"].to_json,
} }
end end

2
app/javascript/controllers/numeric_question_controller.js

@ -3,7 +3,7 @@ import { Controller } from "@hotwired/stimulus"
export default class extends Controller { export default class extends Controller {
calculateFields() { calculateFields() {
const affectedField = this.element.dataset.target; const affectedField = this.element.dataset.target;
const fieldsToAdd = JSON.parse(this.element.dataset.calculated).map(x => `${x.replaceAll("_","-")}-field`); 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 => 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 => parseInt(x)).reduce((a, b) => a + b, 0);
const elementToUpdate = document.getElementById(affectedField); const elementToUpdate = document.getElementById(affectedField);

16
spec/features/case_log_spec.rb

@ -118,17 +118,17 @@ RSpec.describe "Test Features" do
it "updates total value of the rent", js: true do it "updates total value of the rent", js: true do
visit("/case_logs/#{id}/rent") visit("/case_logs/#{id}/rent")
fill_in("basic_rent", with: 3) fill_in("case-log-basic-rent-field", with: 3)
expect(page).to have_field("total-charge-field", with: "3") expect(page).to have_field("case-log-total-charge-field", with: "3")
fill_in("service_charge", with: 2) fill_in("case-log-service-charge-field", with: 2)
expect(page).to have_field("total-charge-field", with: "5") expect(page).to have_field("case-log-total-charge-field", with: "5")
fill_in("personal_service_charge", with: 1) fill_in("case-log-personal-service-charge-field", with: 1)
expect(page).to have_field("total-charge-field", with: "6") expect(page).to have_field("case-log-total-charge-field", with: "6")
fill_in("support_charge", with: 4) fill_in("case-log-support-charge-field", with: 4)
expect(page).to have_field("total-charge-field", with: "10") expect(page).to have_field("case-log-total-charge-field", with: "10")
end end
end end

2
spec/helpers/numeric_questions_helper_spec.rb

@ -13,7 +13,7 @@ RSpec.describe NumericQuestionsHelper do
expect(numeric_question_html_attributes(questions["basic_rent"])).to eq({ expect(numeric_question_html_attributes(questions["basic_rent"])).to eq({
"data-controller": "numeric-question", "data-controller": "numeric-question",
"data-action": "numeric-question#calculateFields", "data-action": "numeric-question#calculateFields",
"data-target": "#{questions['basic_rent']['result-field'].to_s.dasherize}-field", "data-target": "case-log-#{questions['basic_rent']['result-field'].to_s.dasherize}-field",
"data-calculated": questions["basic_rent"]["fields-to-add"].to_json, "data-calculated": questions["basic_rent"]["fields-to-add"].to_json,
}) })
end end

Loading…
Cancel
Save