Browse Source
* feat: conditional question * db:update * tests: add tests for year dependent behaviour * db: migratepull/1285/head^2
natdeanlewissoftwire
2 years ago
committed by
GitHub
8 changed files with 209 additions and 39 deletions
@ -1,7 +1,14 @@
|
||||
class Form::Sales::Pages::LivingBeforePurchase < ::Form::Page |
||||
def questions |
||||
@questions ||= [ |
||||
Form::Sales::Questions::LivingBeforePurchase.new(nil, nil, self), |
||||
] |
||||
living_before_purchase, |
||||
Form::Sales::Questions::LivingBeforePurchaseYears.new(nil, nil, self), |
||||
].compact |
||||
end |
||||
|
||||
def living_before_purchase |
||||
if form.start_date.year >= 2023 |
||||
Form::Sales::Questions::LivingBeforePurchase.new(nil, nil, self) |
||||
end |
||||
end |
||||
end |
||||
|
@ -1,15 +1,26 @@
|
||||
class Form::Sales::Questions::LivingBeforePurchase < ::Form::Question |
||||
def initialize(id, hsh, page) |
||||
super |
||||
@id = "proplen" |
||||
@check_answer_label = "Number of years living in the property before purchase" |
||||
@header = "How long did the buyer(s) live in the property before purchase?" |
||||
@hint_text = "You should round this up to the nearest year. If the buyers haven't been living in the property, enter '0'" |
||||
@type = "numeric" |
||||
@min = 0 |
||||
@max = 80 |
||||
@step = 1 |
||||
@width = 5 |
||||
@suffix = " years" |
||||
@id = "proplen_asked" |
||||
@check_answer_label = "Buyer lived in the property before purchasing" |
||||
@header = "Did the buyer live in the property before purchasing it?" |
||||
@hint_text = nil |
||||
@type = "radio" |
||||
@answer_options = ANSWER_OPTIONS |
||||
@conditional_for = { |
||||
"proplen" => [0], |
||||
} |
||||
@hidden_in_check_answers = { |
||||
"depends_on" => [ |
||||
{ |
||||
"proplen_asked" => 0, |
||||
}, |
||||
], |
||||
} |
||||
end |
||||
|
||||
ANSWER_OPTIONS = { |
||||
"0" => { "value" => "Yes" }, |
||||
"1" => { "value" => "No" }, |
||||
}.freeze |
||||
end |
||||
|
@ -0,0 +1,31 @@
|
||||
class Form::Sales::Questions::LivingBeforePurchaseYears < ::Form::Question |
||||
def initialize(id, hsh, page) |
||||
super |
||||
@id = "proplen" |
||||
@check_answer_label = "Number of years living in the property before purchase" |
||||
@header = header_text |
||||
@hint_text = hint_text |
||||
@type = "numeric" |
||||
@min = 0 |
||||
@max = 80 |
||||
@step = 1 |
||||
@width = 5 |
||||
@suffix = " years" |
||||
end |
||||
|
||||
def header_text |
||||
if form.start_date.year >= 2023 |
||||
"How long did they live there?" |
||||
else |
||||
"How long did the buyer(s) live in the property before purchase?" |
||||
end |
||||
end |
||||
|
||||
def hint_text |
||||
if form.start_date.year >= 2023 |
||||
"You should round up to the nearest year" |
||||
else |
||||
"You should round this up to the nearest year. If the buyers haven't been living in the property, enter '0'" |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,5 @@
|
||||
class AddProplenAskedToSalesLog < ActiveRecord::Migration[7.0] |
||||
def change |
||||
add_column :sales_logs, :proplen_asked, :integer |
||||
end |
||||
end |
@ -0,0 +1,114 @@
|
||||
require "rails_helper" |
||||
|
||||
RSpec.describe Form::Sales::Questions::LivingBeforePurchaseYears, type: :model do |
||||
subject(:question) { described_class.new(question_id, question_definition, page) } |
||||
|
||||
let(:question_id) { nil } |
||||
let(:question_definition) { nil } |
||||
let(:subsection) { instance_double(Form::Subsection, form: instance_double(Form, start_date:)) } |
||||
let(:page) { instance_double(Form::Page, subsection:) } |
||||
|
||||
context "when 2022" do |
||||
let(:start_date) { Time.utc(2022, 2, 8) } |
||||
|
||||
it "has correct page" do |
||||
expect(question.page).to eq(page) |
||||
end |
||||
|
||||
it "has the correct id" do |
||||
expect(question.id).to eq("proplen") |
||||
end |
||||
|
||||
it "has the correct header" do |
||||
expect(question.header).to eq("How long did the buyer(s) live in the property before purchase?") |
||||
end |
||||
|
||||
it "has the correct check_answer_label" do |
||||
expect(question.check_answer_label).to eq("Number of years living in the property before purchase") |
||||
end |
||||
|
||||
it "has the correct type" do |
||||
expect(question.type).to eq("numeric") |
||||
end |
||||
|
||||
it "is not marked as derived" do |
||||
expect(question.derived?).to be false |
||||
end |
||||
|
||||
it "has the correct hint" do |
||||
expect(question.hint_text).to eq("You should round this up to the nearest year. If the buyers haven't been living in the property, enter '0'") |
||||
end |
||||
|
||||
it "has correct width" do |
||||
expect(question.width).to eq(5) |
||||
end |
||||
|
||||
it "has correct step" do |
||||
expect(question.step).to eq(1) |
||||
end |
||||
|
||||
it "has correct suffix" do |
||||
expect(question.suffix).to eq(" years") |
||||
end |
||||
|
||||
it "has correct min" do |
||||
expect(question.min).to eq(0) |
||||
end |
||||
|
||||
it "has correct max" do |
||||
expect(question.max).to eq(80) |
||||
end |
||||
end |
||||
|
||||
context "when 2023" do |
||||
let(:start_date) { Time.utc(2023, 2, 8) } |
||||
|
||||
it "has correct page" do |
||||
expect(question.page).to eq(page) |
||||
end |
||||
|
||||
it "has the correct id" do |
||||
expect(question.id).to eq("proplen") |
||||
end |
||||
|
||||
it "has the correct header" do |
||||
expect(question.header).to eq("How long did they live there?") |
||||
end |
||||
|
||||
it "has the correct check_answer_label" do |
||||
expect(question.check_answer_label).to eq("Number of years living in the property before purchase") |
||||
end |
||||
|
||||
it "has the correct type" do |
||||
expect(question.type).to eq("numeric") |
||||
end |
||||
|
||||
it "is not marked as derived" do |
||||
expect(question.derived?).to be false |
||||
end |
||||
|
||||
it "has the correct hint" do |
||||
expect(question.hint_text).to eq("You should round up to the nearest year") |
||||
end |
||||
|
||||
it "has correct width" do |
||||
expect(question.width).to eq(5) |
||||
end |
||||
|
||||
it "has correct step" do |
||||
expect(question.step).to eq(1) |
||||
end |
||||
|
||||
it "has correct suffix" do |
||||
expect(question.suffix).to eq(" years") |
||||
end |
||||
|
||||
it "has correct min" do |
||||
expect(question.min).to eq(0) |
||||
end |
||||
|
||||
it "has correct max" do |
||||
expect(question.max).to eq(80) |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue