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.
99 lines
3.4 KiB
99 lines
3.4 KiB
3 years ago
|
require "rails_helper"
|
||
|
|
||
2 years ago
|
RSpec.describe Form::Lettings::Questions::SchemeId, type: :model do
|
||
3 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) }
|
||
|
|
||
|
it "has correct page" do
|
||
|
expect(question.page).to eq(page)
|
||
|
end
|
||
|
|
||
|
it "has the correct id" do
|
||
|
expect(question.id).to eq("scheme_id")
|
||
|
end
|
||
|
|
||
|
it "has the correct header" do
|
||
|
expect(question.header).to eq("What scheme is this log for?")
|
||
|
end
|
||
|
|
||
|
it "has the correct check_answer_label" do
|
||
|
expect(question.check_answer_label).to eq("Scheme name")
|
||
|
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 eq("Enter scheme name or postcode")
|
||
|
end
|
||
|
|
||
|
it "has the correct conditional_for" do
|
||
|
expect(question.conditional_for).to be_nil
|
||
|
end
|
||
|
|
||
|
it "is not marked as derived" do
|
||
|
expect(question.derived?).to be false
|
||
|
end
|
||
|
|
||
|
context "when a user is signed in" do
|
||
|
let(:organisation) { FactoryBot.create(:organisation) }
|
||
|
let(:organisation_2) { FactoryBot.create(:organisation) }
|
||
3 years ago
|
let(:user) { FactoryBot.create(:user, organisation:) }
|
||
2 years ago
|
let(:scheme) { FactoryBot.create(:scheme, owning_organisation: organisation, managing_organisation: organisation) }
|
||
2 years ago
|
let(:lettings_log) { FactoryBot.create(:lettings_log, created_by: user, needstype: 2) }
|
||
3 years ago
|
|
||
|
before do
|
||
3 years ago
|
FactoryBot.create(:scheme, owning_organisation: organisation_2)
|
||
3 years ago
|
end
|
||
|
|
||
2 years ago
|
context "when a scheme with at least 1 location exists" do
|
||
|
before do
|
||
|
FactoryBot.create(:location, scheme:)
|
||
|
end
|
||
|
|
||
|
it "has the correct answer_options based on the schemes the user's organisation owns or manages" do
|
||
2 years ago
|
expected_answer = { "" => "Select an option", scheme.id.to_s => scheme }
|
||
2 years ago
|
expect(question.displayed_answer_options(lettings_log)).to eq(expected_answer)
|
||
2 years ago
|
end
|
||
|
end
|
||
|
|
||
|
context "when there are no schemes with locations" do
|
||
2 years ago
|
it "returns a hash with one empty option" do
|
||
2 years ago
|
expect(question.displayed_answer_options(lettings_log)).to eq({ "" => "Select an option" })
|
||
2 years ago
|
end
|
||
|
end
|
||
|
|
||
|
context "when the question is not answered" do
|
||
|
it "returns 'select an option' as selected answer" do
|
||
2 years ago
|
lettings_log.update!(scheme: nil)
|
||
|
answers = question.displayed_answer_options(lettings_log).map { |key, value| OpenStruct.new(id: key, name: value.respond_to?(:service_name) ? value.service_name : nil, resource: value) }
|
||
2 years ago
|
answers.each do |answer|
|
||
|
if answer.resource == "Select an option"
|
||
2 years ago
|
expect(question.answer_selected?(lettings_log, answer)).to eq(true)
|
||
2 years ago
|
else
|
||
2 years ago
|
expect(question.answer_selected?(lettings_log, answer)).to eq(false)
|
||
2 years ago
|
end
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
context "when the question is answered" do
|
||
|
it "returns 'select an option' as selected answer" do
|
||
2 years ago
|
lettings_log.update!(scheme:)
|
||
|
answers = question.displayed_answer_options(lettings_log).map { |key, value| OpenStruct.new(id: key, name: value.respond_to?(:service_name) ? value.service_name : nil, resource: value) }
|
||
2 years ago
|
answers.each do |answer|
|
||
|
if answer.id == scheme.id
|
||
2 years ago
|
expect(question.answer_selected?(lettings_log, answer)).to eq(true)
|
||
2 years ago
|
else
|
||
2 years ago
|
expect(question.answer_selected?(lettings_log, answer)).to eq(false)
|
||
2 years ago
|
end
|
||
|
end
|
||
2 years ago
|
end
|
||
3 years ago
|
end
|
||
|
end
|
||
|
end
|