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.
148 lines
4.3 KiB
148 lines
4.3 KiB
2 years ago
|
require "rails_helper"
|
||
|
|
||
2 years ago
|
RSpec.describe Form::Lettings::Questions::StockOwner, type: :model do
|
||
2 years ago
|
subject(:question) { described_class.new(question_id, question_definition, page) }
|
||
|
|
||
|
let(:question_id) { nil }
|
||
|
let(:question_definition) { nil }
|
||
|
let(:page) { instance_double(Form::Page) }
|
||
|
let(:subsection) { instance_double(Form::Subsection) }
|
||
|
let(:form) { instance_double(Form) }
|
||
|
|
||
|
it "has correct page" do
|
||
|
expect(question.page).to eq(page)
|
||
|
end
|
||
|
|
||
|
it "has the correct id" do
|
||
|
expect(question.id).to eq("owning_organisation_id")
|
||
|
end
|
||
|
|
||
|
it "has the correct header" do
|
||
|
expect(question.header).to eq("Which organisation owns this property?")
|
||
|
end
|
||
|
|
||
|
it "has the correct check_answer_label" do
|
||
2 years ago
|
expect(question.check_answer_label).to eq("Stock owner")
|
||
2 years ago
|
end
|
||
|
|
||
|
it "has the correct type" do
|
||
|
expect(question.type).to eq("select")
|
||
|
end
|
||
|
|
||
|
it "has the correct hint_text" do
|
||
|
expect(question.hint_text).to be_nil
|
||
|
end
|
||
|
|
||
|
describe "answer options" do
|
||
|
let(:options) { { "" => "Select an option" } }
|
||
|
|
||
|
context "when current_user nil" do
|
||
|
it "shows default options" do
|
||
|
expect(question.answer_options).to eq(options)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
context "when user not support and owns own stock" do
|
||
|
let(:user) { create(:user, :data_coordinator, organisation: create(:organisation, holds_own_stock: true)) }
|
||
|
let(:options) do
|
||
|
{
|
||
|
"" => "Select an option",
|
||
|
user.organisation.id => "#{user.organisation.name} (Your organisation)",
|
||
|
}
|
||
|
end
|
||
|
|
||
|
before do
|
||
|
question.current_user = user
|
||
|
end
|
||
|
|
||
2 years ago
|
it "shows stock owners with own org at the top" do
|
||
2 years ago
|
expect(question.answer_options).to eq(options)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
context "when user support" do
|
||
|
before do
|
||
|
question.current_user = create(:user, :support)
|
||
|
end
|
||
|
|
||
|
let(:expected_opts) do
|
||
|
Organisation.all.each_with_object(options) do |organisation, hsh|
|
||
|
hsh[organisation.id] = organisation.name
|
||
|
hsh
|
||
|
end
|
||
|
end
|
||
|
|
||
|
it "shows all orgs" do
|
||
|
expect(question.answer_options).to eq(expected_opts)
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
it "is marked as derived" do
|
||
|
expect(question.derived?).to be true
|
||
|
end
|
||
|
|
||
|
describe "#hidden_in_check_answers?" do
|
||
2 years ago
|
context "when support" do
|
||
|
let(:user) { create(:user, :support) }
|
||
|
|
||
|
it "is not hidden in check answers" do
|
||
|
expect(question.hidden_in_check_answers?(nil, user)).to be false
|
||
|
end
|
||
|
end
|
||
|
|
||
|
context "when org holds own stock", :aggregate_failures do
|
||
|
let(:user) { create(:user, :data_coordinator, organisation: create(:organisation, holds_own_stock: true)) }
|
||
|
|
||
2 years ago
|
context "when stock owners == 0" do
|
||
2 years ago
|
before do
|
||
2 years ago
|
user.organisation.stock_owners.delete_all
|
||
2 years ago
|
end
|
||
2 years ago
|
|
||
|
it "is hidden in check answers" do
|
||
2 years ago
|
expect(user.organisation.stock_owners.count).to eq(0)
|
||
2 years ago
|
expect(question.hidden_in_check_answers?(nil, user)).to be true
|
||
|
end
|
||
|
end
|
||
|
|
||
2 years ago
|
context "when stock owners != 0" do
|
||
2 years ago
|
before do
|
||
2 years ago
|
create(:organisation_relationship, child_organisation: user.organisation)
|
||
2 years ago
|
end
|
||
2 years ago
|
|
||
2 years ago
|
it "is visible in check answers" do
|
||
2 years ago
|
expect(user.organisation.stock_owners.count).to eq(1)
|
||
2 years ago
|
expect(question.hidden_in_check_answers?(nil, user)).to be false
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
2 years ago
|
context "when org does not hold own stock", :aggregate_failures do
|
||
|
let(:user) { create(:user, :data_coordinator, organisation: create(:organisation, holds_own_stock: false)) }
|
||
2 years ago
|
|
||
2 years ago
|
context "when stock owners <= 1" do
|
||
2 years ago
|
before do
|
||
2 years ago
|
create(:organisation_relationship, child_organisation: user.organisation)
|
||
2 years ago
|
end
|
||
|
|
||
|
it "is hidden in check answers" do
|
||
2 years ago
|
expect(user.organisation.stock_owners.count).to eq(1)
|
||
2 years ago
|
expect(question.hidden_in_check_answers?(nil, user)).to be true
|
||
|
end
|
||
2 years ago
|
end
|
||
|
|
||
2 years ago
|
context "when stock owners >= 2" 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 visible in check answers" do
|
||
2 years ago
|
expect(user.organisation.stock_owners.count).to eq(2)
|
||
2 years ago
|
expect(question.hidden_in_check_answers?(nil, user)).to be false
|
||
|
end
|
||
2 years ago
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|