From 2aed0841bcc676b91a10ad4bfd4682b510ed982e Mon Sep 17 00:00:00 2001 From: Kat Date: Thu, 7 Oct 2021 08:53:23 +0100 Subject: [PATCH] extract numeric html attributes to a helper --- app/helpers/numeric_questions_helper.rb | 12 +++++++++++ ...ller.js => numeric_question_controller.js} | 0 app/views/form/_numeric_question.html.erb | 5 +---- spec/features/case_log_spec.rb | 1 - spec/helpers/numeric_questions_helper_spec.rb | 21 +++++++++++++++++++ 5 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 app/helpers/numeric_questions_helper.rb rename app/javascript/controllers/{number_question_controller.js => numeric_question_controller.js} (100%) create mode 100644 spec/helpers/numeric_questions_helper_spec.rb diff --git a/app/helpers/numeric_questions_helper.rb b/app/helpers/numeric_questions_helper.rb new file mode 100644 index 000000000..0a8889d04 --- /dev/null +++ b/app/helpers/numeric_questions_helper.rb @@ -0,0 +1,12 @@ +module NumericQuestionsHelper + def numeric_question_html_attributes(question) + return {} if question["fields-to-add"].blank? || question["result-field"].blank? + + { + "data-controller": "numeric-question", + "data-action": "numeric-question#calculateFields", + "data-affected": "#{question['result-field'].to_s.dasherize}-field", + "data-calculated": question["fields-to-add"].to_json, + } + end +end diff --git a/app/javascript/controllers/number_question_controller.js b/app/javascript/controllers/numeric_question_controller.js similarity index 100% rename from app/javascript/controllers/number_question_controller.js rename to app/javascript/controllers/numeric_question_controller.js diff --git a/app/views/form/_numeric_question.html.erb b/app/views/form/_numeric_question.html.erb index f05f4a16f..6de29a111 100644 --- a/app/views/form/_numeric_question.html.erb +++ b/app/views/form/_numeric_question.html.erb @@ -3,8 +3,5 @@ label: { text: question["header"].html_safe, size: "l"}, min: question["min"], max: question["max"], step: question["step"], width: 20, :readonly => question["readonly"], - :"data-controller" => "number-question", - :"data-action"=> "number-question#calculateFields", - :"data-affected" => "#{question["result-field"].to_s.dasherize}-field", - :"data-calculated" => question["fields-to-add"].to_json + **numeric_question_html_attributes(question) %> diff --git a/spec/features/case_log_spec.rb b/spec/features/case_log_spec.rb index ddd14ae5f..458bef58b 100644 --- a/spec/features/case_log_spec.rb +++ b/spec/features/case_log_spec.rb @@ -130,7 +130,6 @@ RSpec.describe "Test Features" do fill_in("support_charge", with: 4) expect(page).to have_field("total-charge-field", with: "10") end - end describe "Back link directs correctly" do diff --git a/spec/helpers/numeric_questions_helper_spec.rb b/spec/helpers/numeric_questions_helper_spec.rb new file mode 100644 index 000000000..0d5436983 --- /dev/null +++ b/spec/helpers/numeric_questions_helper_spec.rb @@ -0,0 +1,21 @@ +require "rails_helper" + +RSpec.describe NumericQuestionsHelper do + let(:form) { Form.new(2021, 2022) } + let(:questions) { form.questions_for_page("rent") } + + describe "html attributes" do + it "returns empty hash if fields-to-add or result-field are empty " do + expect(numeric_question_html_attributes(questions["total_charge"])).to eq({}) + end + + it "returns html attributes if fields-to-add or result-field are not empty " do + expect(numeric_question_html_attributes(questions["basic_rent"])).to eq({ + "data-controller": "numeric-question", + "data-action": "numeric-question#calculateFields", + "data-affected": "#{questions['basic_rent']['result-field'].to_s.dasherize}-field", + "data-calculated": questions["basic_rent"]["fields-to-add"].to_json, + }) + end + end +end