From 10e96cc165a8cae40da578bb62b60250f0f57cf8 Mon Sep 17 00:00:00 2001 From: Rachael Booth Date: Wed, 9 Oct 2024 12:29:08 +0100 Subject: [PATCH] Add tests for copy key and copy to page and question specs --- spec/models/form/page_spec.rb | 37 +++++++++++++++++- spec/models/form/question_spec.rb | 65 ++++++++++++++++++++++++++++++- 2 files changed, 98 insertions(+), 4 deletions(-) diff --git a/spec/models/form/page_spec.rb b/spec/models/form/page_spec.rb index c66c0eca8..1ca01875a 100644 --- a/spec/models/form/page_spec.rb +++ b/spec/models/form/page_spec.rb @@ -8,8 +8,8 @@ RSpec.describe Form::Page, type: :model do let(:depends_on) { nil } let(:enabled) { true } let(:depends_on_met) { true } - let(:form) { instance_double(Form, depends_on_met:) } - let(:subsection) { instance_double(Form::Subsection, depends_on:, enabled?: enabled, form:) } + let(:form) { instance_double(Form, depends_on_met:, type: "form-type") } + let(:subsection) { instance_double(Form::Subsection, depends_on:, enabled?: enabled, form:, id: "subsection-id") } let(:page_id) { "net_income" } let(:questions) { [["earnings", { "conditional_for" => { "age1": nil }, "type" => "radio" }], %w[incfreq]] } let(:page_definition) do @@ -24,6 +24,39 @@ RSpec.describe Form::Page, type: :model do expect(page.id).to eq(page_id) end + it "sets copy_key in the default style" do + expect(page.copy_key).to eq("#{form.type}.#{subsection.id}.#{questions[0].id}") + end + + context "when header is not provided" do + let(:page_definition) do + { + "questions" => questions, + } + end + + context "and translation is present" do + before do + allow(I18n).to receive(:t).and_return("page header copy") + allow(I18n).to receive(:exists?).and_return(true) + end + + it "uses header from translations" do + expect(page.header).to eq("page header copy") + end + end + + context "and translation is not present" do + before do + allow(I18n).to receive(:exists?).and_return(false) + end + + it "uses empty header" do + expect(page.header).to eq("") + end + end + end + it "has a header" do expect(page.header).to eq("Test header") end diff --git a/spec/models/form/question_spec.rb b/spec/models/form/question_spec.rb index ca67e1103..d059edf59 100644 --- a/spec/models/form/question_spec.rb +++ b/spec/models/form/question_spec.rb @@ -14,8 +14,8 @@ RSpec.describe Form::Question, type: :model do let(:answer_options) { { "1" => { "value" => "Weekly" }, "2" => { "value" => "Monthly" } } } let(:inferred_check_answers_value) { [{ "condition" => { "postcode_known" => 0 }, "value" => "Weekly" }] } - let(:form) { instance_double(Form, depends_on_met:, conditional_question_conditions:) } - let(:subsection) { instance_double(Form::Subsection, form:) } + let(:form) { instance_double(Form, depends_on_met:, conditional_question_conditions:, type: "form-type", start_date: Time.utc(2024, 12, 25)) } + let(:subsection) { instance_double(Form::Subsection, form:, id: "subsection-id") } let(:page) { instance_double(Form::Page, subsection:, routed_to?: true, questions: form_questions) } let(:question_id) { "earnings" } let(:question_definition) do @@ -38,6 +38,67 @@ RSpec.describe Form::Question, type: :model do expect(question.id).to eq(question_id) end + it "sets copy_key in the default style" do + expect(question.copy_key).to eq("#{form.type}.#{subsection.id}.#{question_id}") + end + + context "when copy is not provided" do + let(:question_definition) do + { + "type" => type, + "min" => 0, + "step" => 1, + "answer_options" => answer_options, + "readonly" => readonly, + "result-field" => "tcharge", + "fields-to-add" => %w[brent scharge pscharge supcharg], + "inferred_check_answers_value" => inferred_check_answers_value, + "suffix" => suffix, + "prefix" => prefix, + "hidden_in_check_answers" => {} + } + end + + context "and translations are present" do + before do + allow(I18n).to receive(:t).with("forms.#{form.start_date.year}.#{question.copy_key}.question_text", { default: "" }).and_return("header copy") + allow(I18n).to receive(:t).with("forms.#{form.start_date.year}.#{question.copy_key}.check_answer_label", { default: "" }).and_return("check answer label copy") + allow(I18n).to receive(:t).with("forms.#{form.start_date.year}.#{question.copy_key}.hint_text", { default: "" }).and_return("hint text copy") + allow(I18n).to receive(:exists?).and_return(true) + end + + it "uses header from translations" do + expect(question.header).to eq("header copy") + end + + it "uses check answer label from translations" do + expect(question.check_answer_label).to eq("check answer label copy") + end + + it "uses hint text from translations" do + expect(question.hint_text).to eq("hint text copy") + end + end + + context "and translations are not present" do + before do + allow(I18n).to receive(:exists?).and_return(false) + end + + it "uses empty header" do + expect(question.header).to eq("") + end + + it "uses empty check answer label" do + expect(question.check_answer_label).to eq("") + end + + it "uses empty hint text" do + expect(question.hint_text).to eq("") + end + end + end + it "has a header" do expect(question.header).to eq("What is the tenant’s /and partner’s combined income after tax?") end