Browse Source

Add tests for copy key and copy to page and question specs

pull/2672/head
Rachael Booth 10 months ago
parent
commit
10e96cc165
  1. 37
      spec/models/form/page_spec.rb
  2. 65
      spec/models/form/question_spec.rb

37
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

65
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

Loading…
Cancel
Save