You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
156 lines
4.6 KiB
156 lines
4.6 KiB
2 years ago
|
require "rails_helper"
|
||
|
|
||
2 years ago
|
RSpec.describe Form::Lettings::Pages::StockOwner, type: :model do
|
||
2 years ago
|
subject(:page) { described_class.new(page_id, page_definition, subsection) }
|
||
|
|
||
|
let(:page_id) { nil }
|
||
|
let(:page_definition) { nil }
|
||
|
let(:subsection) { instance_double(Form::Subsection) }
|
||
|
let(:form) { instance_double(Form) }
|
||
|
|
||
|
it "has correct subsection" do
|
||
|
expect(page.subsection).to eq(subsection)
|
||
|
end
|
||
|
|
||
|
it "has correct questions" do
|
||
|
expect(page.questions.map(&:id)).to eq(%w[owning_organisation_id])
|
||
|
end
|
||
|
|
||
|
it "has the correct id" do
|
||
2 years ago
|
expect(page.id).to eq("stock_owner")
|
||
2 years ago
|
end
|
||
|
|
||
|
it "has the correct header" do
|
||
|
expect(page.header).to eq("")
|
||
|
end
|
||
|
|
||
|
it "has the correct description" do
|
||
|
expect(page.description).to eq("")
|
||
|
end
|
||
|
|
||
|
it "has the correct depends_on" do
|
||
|
expect(page.depends_on).to be nil
|
||
|
end
|
||
|
|
||
|
describe "#routed_to?" do
|
||
|
let(:log) { create(:lettings_log, owning_organisation_id: nil) }
|
||
|
|
||
|
context "when user nil" do
|
||
|
it "is not shown" do
|
||
|
expect(page.routed_to?(log, nil)).to eq(false)
|
||
|
end
|
||
|
|
||
|
it "does not update owning_organisation_id" do
|
||
|
expect { page.routed_to?(log, nil) }.not_to change(log.reload, :owning_organisation).from(nil)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
context "when support" do
|
||
|
let(:user) { create(:user, :support) }
|
||
|
|
||
|
it "is shown" do
|
||
|
expect(page.routed_to?(log, user)).to eq(true)
|
||
|
end
|
||
|
|
||
|
it "does not update owning_organisation_id" do
|
||
|
expect { page.routed_to?(log, user) }.not_to change(log.reload, :owning_organisation).from(nil)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
context "when not support" do
|
||
|
context "when does not hold own stock" do
|
||
|
let(:user) do
|
||
|
create(:user, :data_coordinator, organisation: create(:organisation, holds_own_stock: false))
|
||
|
end
|
||
|
|
||
2 years ago
|
context "with 0 stock_owners" do
|
||
2 years ago
|
it "is not shown" do
|
||
|
expect(page.routed_to?(log, user)).to eq(false)
|
||
|
end
|
||
|
|
||
|
it "does not update owning_organisation_id" do
|
||
|
expect { page.routed_to?(log, user) }.not_to change(log.reload, :owning_organisation)
|
||
|
end
|
||
2 years ago
|
end
|
||
|
|
||
2 years ago
|
context "with 1 stock_owners" do
|
||
|
let(:stock_owner) { create(:organisation) }
|
||
2 years ago
|
|
||
|
before do
|
||
|
create(
|
||
|
:organisation_relationship,
|
||
|
child_organisation: user.organisation,
|
||
2 years ago
|
parent_organisation: stock_owner,
|
||
2 years ago
|
)
|
||
|
end
|
||
|
|
||
|
it "is not shown" do
|
||
|
expect(page.routed_to?(log, user)).to eq(false)
|
||
|
end
|
||
|
|
||
|
it "updates owning_organisation_id" do
|
||
2 years ago
|
expect { page.routed_to?(log, user) }.to change(log.reload, :owning_organisation).from(nil).to(stock_owner)
|
||
2 years ago
|
end
|
||
|
end
|
||
|
|
||
2 years ago
|
context "with >1 stock_owners" do
|
||
|
let(:stock_owner1) { create(:organisation) }
|
||
|
let(:stock_owner2) { create(:organisation) }
|
||
2 years ago
|
|
||
|
before do
|
||
|
create(
|
||
|
:organisation_relationship,
|
||
|
child_organisation: user.organisation,
|
||
2 years ago
|
parent_organisation: stock_owner1,
|
||
2 years ago
|
)
|
||
|
create(
|
||
|
:organisation_relationship,
|
||
|
child_organisation: user.organisation,
|
||
2 years ago
|
parent_organisation: stock_owner2,
|
||
2 years ago
|
)
|
||
|
end
|
||
|
|
||
|
it "is not shown" do
|
||
|
expect(page.routed_to?(log, user)).to eq(true)
|
||
|
end
|
||
|
|
||
|
it "updates owning_organisation_id" do
|
||
|
expect { page.routed_to?(log, user) }.not_to change(log.reload, :owning_organisation)
|
||
|
end
|
||
2 years ago
|
end
|
||
|
end
|
||
|
|
||
|
context "when holds own stock" do
|
||
|
let(:user) do
|
||
|
create(:user, :data_coordinator, organisation: create(:organisation, holds_own_stock: true))
|
||
|
end
|
||
|
|
||
2 years ago
|
context "with 0 stock_owners" do
|
||
2 years ago
|
it "is not shown" do
|
||
|
expect(page.routed_to?(log, user)).to eq(false)
|
||
|
end
|
||
|
|
||
2 years ago
|
it "updates owning_organisation_id to user organisation" do
|
||
|
expect { page.routed_to?(log, user) }.to change(log.reload, :owning_organisation).from(nil).to(user.organisation)
|
||
2 years ago
|
end
|
||
|
end
|
||
|
|
||
2 years ago
|
context "with >0 stock_owners" do
|
||
2 years ago
|
before do
|
||
2 years ago
|
create(:organisation_relationship, child_organisation: user.organisation)
|
||
|
create(:organisation_relationship, child_organisation: user.organisation)
|
||
2 years ago
|
end
|
||
|
|
||
|
it "is shown" do
|
||
|
expect(page.routed_to?(log, user)).to eq(true)
|
||
|
end
|
||
|
|
||
|
it "does not update owning_organisation_id" do
|
||
|
expect { page.routed_to?(log, user) }.not_to change(log.reload, :owning_organisation).from(nil)
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|