Browse Source

Refactor depends_on

pull/2541/head
Kat 2 years ago
parent
commit
9133e208d3
  1. 17
      app/models/form/sales/pages/deposit.rb
  2. 228
      spec/models/form/sales/pages/deposit_spec.rb

17
app/models/form/sales/pages/deposit.rb

@ -12,17 +12,10 @@ class Form::Sales::Pages::Deposit < ::Form::Page
]
end
def depends_on
if form.start_year_after_2024?
[{ "social_homebuy?" => false, "ownershipsch" => 1, "stairowned_100?" => @optional },
{ "ownershipsch" => 2 },
{ "ownershipsch" => 3, "mortgageused" => 1 },
{ "social_homebuy?" => true, "stairowned_100?" => @optional }]
else
[{ "social_homebuy?" => false, "ownershipsch" => 1 },
{ "ownershipsch" => 2 },
{ "ownershipsch" => 3, "mortgageused" => 1 },
{ "social_homebuy?" => true }]
end
def routed_to?(log, _user)
return true if log.ownershipsch == 2 || (log.ownershipsch == 3 && log.mortgageused == 1)
return false if log.stairowned_100? != @optional && form.start_year_after_2024?
log.social_homebuy? == false && log.ownershipsch == 1 || log.social_homebuy? == true
end
end

228
spec/models/form/sales/pages/deposit_spec.rb

@ -1,14 +1,16 @@
require "rails_helper"
RSpec.describe Form::Sales::Pages::Deposit, type: :model do
subject(:page) { described_class.new(page_id, page_definition, subsection, ownershipsch: 1, optional: false) }
subject(:page) { described_class.new(page_id, page_definition, subsection, ownershipsch: 1, optional:) }
let(:page_id) { nil }
let(:page_definition) { nil }
let(:subsection) { instance_double(Form::Subsection) }
let(:subsection) { instance_double(Form::Subsection, enabled?: true) }
let(:form) { instance_double(Form, start_year_after_2024?: false, start_date: Time.zone.local(2023, 4, 1)) }
let(:optional) { false }
before do
allow(subsection).to receive(:form).and_return(instance_double(Form, start_year_after_2024?: false, start_date: Time.zone.local(2023, 4, 1)))
allow(subsection).to receive(:form).and_return(form)
end
it "has correct subsection" do
@ -31,52 +33,202 @@ RSpec.describe Form::Sales::Pages::Deposit, type: :model do
expect(page.description).to be_nil
end
it "has correct depends_on" do
expect(page.depends_on).to eq(
[{ "social_homebuy?" => false, "ownershipsch" => 1 },
{ "ownershipsch" => 2 },
{ "ownershipsch" => 3, "mortgageused" => 1 },
{ "social_homebuy?" => true }],
)
end
context "when routing with start year after 2024" do
before do
allow(form).to receive(:start_year_after_2024?).and_return(true)
end
context "and optional is false" do
context "and the log is shared ownership, not social homembuy and stairowned is not 100" do
let(:log) { build(:sales_log, ownershipsch: 1, type: 16, stairowned: 70) }
it "routes to the page" do
expect(page).to be_routed_to(log, nil)
end
end
context "and the log is shared ownership, not social homembuy and stairowned is 100" do
let(:log) { build(:sales_log, ownershipsch: 1, type: 16, stairowned: 100) }
it "does not route to the page" do
expect(page).not_to be_routed_to(log, nil)
end
end
context "and the log is shared ownership, social homebuy and stairowned is not 100" do
let(:log) { build(:sales_log, ownershipsch: 1, type: 18, stairowned: 80) }
it "routes to the page" do
expect(page).to be_routed_to(log, nil)
end
end
context "and the log is shared ownership, social homebuy and stairowned is 100" do
let(:log) { build(:sales_log, ownershipsch: 1, type: 18, stairowned: 100) }
it "does not route to the page" do
expect(page).not_to be_routed_to(log, nil)
end
end
context "and the log is discounted ownership" do
let(:log) { build(:sales_log, ownershipsch: 2, type: 18) }
it "routes to the page" do
expect(page).to be_routed_to(log, nil)
end
end
context "and the log is outright ownership and mortgage used is yes" do
let(:log) { build(:sales_log, ownershipsch: 3, mortgageused: 1) }
it "routes to the page" do
expect(page).to be_routed_to(log, nil)
end
end
context "and ownership is outright sale and mortgage used is not yes" do
let(:log) { build(:sales_log, ownershipsch: 3, mortgageused: 2) }
it "doesn't route to the page" do
expect(page).not_to be_routed_to(log, nil)
end
end
end
context "and optional is true" do
let(:optional) { true }
context "and the log is shared ownership, not social homembuy and stairowned is not 100" do
let(:log) { build(:sales_log, ownershipsch: 1, type: 16, stairowned: 70) }
it "does not route to the page" do
expect(page).not_to be_routed_to(log, nil)
end
end
context "and the log is shared ownership, not social homembuy and stairowned is 100" do
let(:log) { build(:sales_log, ownershipsch: 1, type: 16, stairowned: 100) }
it "routes to the page" do
expect(page).to be_routed_to(log, nil)
end
end
context "and the log is shared ownership, social homebuy and stairowned is not 100" do
let(:log) { build(:sales_log, ownershipsch: 1, type: 18, stairowned: 80) }
context "when optional is true" do
subject(:page) { described_class.new(page_id, page_definition, subsection, ownershipsch: 1, optional: true) }
it "does not route to the page" do
expect(page).not_to be_routed_to(log, nil)
end
end
context "and the log is shared ownership, social homebuy and stairowned is 100" do
let(:log) { build(:sales_log, ownershipsch: 1, type: 18, stairowned: 100) }
it "has correct depends_on" do
expect(page.depends_on).to eq(
[{ "social_homebuy?" => false, "ownershipsch" => 1 },
{ "ownershipsch" => 2 },
{ "ownershipsch" => 3, "mortgageused" => 1 },
{ "social_homebuy?" => true }],
)
it "routes to the page" do
expect(page).to be_routed_to(log, nil)
end
end
end
end
context "when it's a 2024 form" do
context "when routing with start year before 2024" do
before do
allow(subsection).to receive(:form).and_return(instance_double(Form, start_year_after_2024?: true, start_date: Time.zone.local(2024, 4, 1)))
allow(form).to receive(:start_year_after_2024?).and_return(false)
end
it "has correct depends_on" do
expect(page.depends_on).to eq(
[{ "social_homebuy?" => false, "ownershipsch" => 1, "stairowned_100?" => false },
{ "ownershipsch" => 2 },
{ "ownershipsch" => 3, "mortgageused" => 1 },
{ "social_homebuy?" => true, "stairowned_100?" => false }],
)
context "and optional is false" do
context "and the log is shared ownership, not social homembuy and stairowned is not 100" do
let(:log) { build(:sales_log, ownershipsch: 1, type: 16, stairowned: 70) }
it "routes to the page" do
expect(page).to be_routed_to(log, nil)
end
end
context "and the log is shared ownership, not social homembuy and stairowned is 100" do
let(:log) { build(:sales_log, ownershipsch: 1, type: 16, stairowned: 100) }
it "routes to the page" do
expect(page).to be_routed_to(log, nil)
end
end
context "and the log is shared ownership, social homebuy and stairowned is not 100" do
let(:log) { build(:sales_log, ownershipsch: 1, type: 18, stairowned: 80) }
it "routes to the page" do
expect(page).to be_routed_to(log, nil)
end
end
context "and the log is shared ownership, social homebuy and stairowned is 100" do
let(:log) { build(:sales_log, ownershipsch: 1, type: 18, stairowned: 100) }
it "routes to the page" do
expect(page).to be_routed_to(log, nil)
end
end
context "and the log is discounted ownership" do
let(:log) { build(:sales_log, ownershipsch: 2, type: 18) }
it "routes to the page" do
expect(page).to be_routed_to(log, nil)
end
end
context "and the log is outright ownership and mortgage used is yes" do
let(:log) { build(:sales_log, ownershipsch: 3, mortgageused: 1) }
it "routes to the page" do
expect(page).to be_routed_to(log, nil)
end
end
context "and ownership is outright sale and mortgage used is not yes" do
let(:log) { build(:sales_log, ownershipsch: 3, mortgageused: 2) }
it "doesn't route to the page" do
expect(page).not_to be_routed_to(log, nil)
end
end
end
context "and optional is true" do
subject(:page) { described_class.new(page_id, page_definition, subsection, ownershipsch: 1, optional: true) }
it "has correct depends_on" do
expect(page.depends_on).to eq(
[{ "social_homebuy?" => false, "ownershipsch" => 1, "stairowned_100?" => true },
{ "ownershipsch" => 2 },
{ "ownershipsch" => 3, "mortgageused" => 1 },
{ "social_homebuy?" => true, "stairowned_100?" => true }],
)
let(:optional) { true }
context "and the log is shared ownership, not social homembuy and stairowned is not 100" do
let(:log) { build(:sales_log, ownershipsch: 1, type: 16, stairowned: 70) }
it "does routes to the page" do
expect(page).to be_routed_to(log, nil)
end
end
context "and the log is shared ownership, not social homembuy and stairowned is 100" do
let(:log) { build(:sales_log, ownershipsch: 1, type: 16, stairowned: 100) }
it "routes to the page" do
expect(page).to be_routed_to(log, nil)
end
end
context "and the log is shared ownership, social homebuy and stairowned is not 100" do
let(:log) { build(:sales_log, ownershipsch: 1, type: 18, stairowned: 80) }
it "does routes to the page" do
expect(page).to be_routed_to(log, nil)
end
end
context "and the log is shared ownership, social homebuy and stairowned is 100" do
let(:log) { build(:sales_log, ownershipsch: 1, type: 18, stairowned: 100) }
it "routes to the page" do
expect(page).to be_routed_to(log, nil)
end
end
end
end

Loading…
Cancel
Save