From 336aaea1e01821b6243434eddb8edd5c9a76dbf9 Mon Sep 17 00:00:00 2001 From: Kat Date: Fri, 1 Oct 2021 09:39:43 +0100 Subject: [PATCH] check questions instead of page names for get answered questions total --- app/helpers/check_answers_helper.rb | 7 +++---- spec/helpers/check_answers_helper_spec.rb | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 spec/helpers/check_answers_helper_spec.rb diff --git a/app/helpers/check_answers_helper.rb b/app/helpers/check_answers_helper.rb index 464f42fab..605d6c9e7 100644 --- a/app/helpers/check_answers_helper.rb +++ b/app/helpers/check_answers_helper.rb @@ -1,10 +1,9 @@ module CheckAnswersHelper def get_answered_questions_total(subsection_pages, case_log) - questions = [] - subsection_pages.keys.each do |page| - questions << page + questions = subsection_pages.values.map do |page| + page["questions"].keys end - return questions.count {|question| !(case_log[question].blank?)} + return questions.count {|questions_for_page| questions_for_page.none? { |question| case_log[question].blank?}} end diff --git a/spec/helpers/check_answers_helper_spec.rb b/spec/helpers/check_answers_helper_spec.rb new file mode 100644 index 000000000..78cc23b25 --- /dev/null +++ b/spec/helpers/check_answers_helper_spec.rb @@ -0,0 +1,18 @@ +require "rails_helper" + +RSpec.describe CheckAnswersHelper do + describe "Get answered questions total" do + let!(:case_log) { FactoryBot.create(:case_log) } + @form = Form.new(2021, 2022) + subsection_pages = @form.pages_for_subsection("income_and_benefits") + + it "returns 0 if no questions are answered" do + expect(get_answered_questions_total(subsection_pages, case_log)).to equal(0) + end + + it "returns 0 if only 1 question on a page gets answered" do + case_log["net_income"] = "123" + expect(get_answered_questions_total(subsection_pages, case_log)).to equal(0) + end + end +end