Browse Source

Display the schemes with active locations only when selecting a scheme for a case log

pull/744/head
Kat 3 years ago
parent
commit
e6cc95a269
  1. 2
      app/models/form/setup/questions/scheme_id.rb
  2. 47
      spec/features/schemes_spec.rb
  3. 12
      spec/models/form/setup/questions/scheme_id_spec.rb

2
app/models/form/setup/questions/scheme_id.rb

@ -22,7 +22,7 @@ class Form::Setup::Questions::SchemeId < ::Form::Question
def displayed_answer_options(case_log) def displayed_answer_options(case_log)
return {} unless case_log.created_by return {} unless case_log.created_by
user_org_scheme_ids = Scheme.select(:id).where(owning_organisation_id: case_log.created_by.organisation_id).map(&:id) user_org_scheme_ids = Scheme.select(:id).where(owning_organisation_id: case_log.created_by.organisation_id).joins(:locations).merge(Location.where("startdate <= ? or startdate IS NULL", Time.zone.now)).map(&:id)
answer_options.select do |k, _v| answer_options.select do |k, _v|
user_org_scheme_ids.include?(k.to_i) user_org_scheme_ids.include?(k.to_i)
end end

47
spec/features/schemes_spec.rb

@ -772,4 +772,51 @@ RSpec.describe "Schemes scheme Features" do
end end
end end
end end
context "when selecting a scheme" do
let!(:user) { FactoryBot.create(:user, :data_coordinator, last_sign_in_at: Time.zone.now) }
let!(:schemes) { FactoryBot.create_list(:scheme, 5, owning_organisation: user.organisation) }
let(:location) { FactoryBot.create(:location, scheme: schemes[2]) }
let!(:case_log) { FactoryBot.create(:case_log, created_by: user, needstype: 2) }
before do
Timecop.freeze(Time.utc(2022, 6, 3))
location.update!(startdate: nil)
FactoryBot.create(:location, scheme: schemes[0], startdate: nil)
FactoryBot.create(:location, scheme: schemes[1], startdate: nil)
FactoryBot.create(:location, scheme: schemes[1], startdate: nil)
FactoryBot.create(:location, scheme: schemes[1], startdate: Time.utc(2023, 6, 3))
visit("/logs")
fill_in("user[email]", with: user.email)
fill_in("user[password]", with: user.password)
click_button("Sign in")
end
after do
Timecop.unfreeze
end
it "does not display the schemes without a location" do
visit("/logs/#{case_log.id}/scheme")
expect(find("#case-log-scheme-id-field").all("option").count).to eq(3)
end
it "does not display the schemes with a location with a startdate in the future" do
location.update!(startdate: Time.utc(2022, 7, 4))
visit("/logs/#{case_log.id}/scheme")
expect(find("#case-log-scheme-id-field").all("option").count).to eq(2)
end
it "does display the schemes with a location with a startdate in the past" do
location.update!(startdate: Time.utc(2022, 5, 2))
visit("/logs/#{case_log.id}/scheme")
expect(find("#case-log-scheme-id-field").all("option").count).to eq(3)
end
it "does display the schemes with a location with a startdate being today" do
location.update!(startdate: Time.utc(2022, 6, 3))
visit("/logs/#{case_log.id}/scheme")
expect(find("#case-log-scheme-id-field").all("option").count).to eq(3)
end
end
end end

12
spec/models/form/setup/questions/scheme_id_spec.rb

@ -50,9 +50,21 @@ RSpec.describe Form::Setup::Questions::SchemeId, type: :model do
FactoryBot.create(:scheme, owning_organisation: organisation_2) FactoryBot.create(:scheme, owning_organisation: organisation_2)
end end
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 it "has the correct answer_options based on the schemes the user's organisation owns or manages" do
expected_answer = { scheme.id.to_s => scheme } expected_answer = { scheme.id.to_s => scheme }
expect(question.displayed_answer_options(case_log)).to eq(expected_answer) expect(question.displayed_answer_options(case_log)).to eq(expected_answer)
end end
end end
context "when there are no schemes with locations" do
it "returns an empty hash" do
expect(question.displayed_answer_options(case_log)).to eq({})
end
end
end
end end

Loading…
Cancel
Save