From 805107c41332cce772622907a09a95802254f759 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Meny?= Date: Mon, 4 Jul 2022 17:27:52 +0100 Subject: [PATCH] CLDC-1249 supported housing scheme (#705) * Introduce scheme and location pages * Derive single scheme location * Reset location when a scheme is changed Co-authored-by: Dushan Despotovic Co-authored-by: Katrina Kosiak --- .../styles/_accessible-autocomplete.scss | 27 ++++++++- app/models/case_log.rb | 6 ++ .../derived_variables/case_log_variables.rb | 10 ++++ app/models/form/setup/pages/location.rb | 19 ++++++ app/models/form/setup/pages/rent_type.rb | 6 +- app/models/form/setup/pages/scheme.rb | 18 ++++++ .../form/setup/questions/location_id.rb | 38 ++++++++++++ app/models/form/setup/questions/needs_type.rb | 2 +- app/models/form/setup/questions/scheme_id.rb | 40 +++++++++++++ app/models/form/setup/subsections/setup.rb | 2 + app/views/schemes/locations.html.erb | 2 +- ...20220630154441_add_location_to_case_log.rb | 5 ++ .../20220704135746_rename_location_name.rb | 8 +++ db/schema.rb | 12 ++-- db/seeds.rb | 9 +-- spec/factories/location.rb | 3 +- spec/features/log_spec.rb | 4 +- spec/helpers/tasklist_helper_spec.rb | 2 +- spec/models/case_log_spec.rb | 17 ++++++ spec/models/form/setup/pages/location_spec.rb | 37 ++++++++++++ spec/models/form/setup/pages/scheme_spec.rb | 33 +++++++++++ .../form/setup/questions/location_id_spec.rb | 37 ++++++++++++ .../form/setup/questions/needs_type_spec.rb | 4 +- .../form/setup/questions/scheme_id_spec.rb | 58 +++++++++++++++++++ .../form/setup/subsections/setup_spec.rb | 2 + spec/models/form_handler_spec.rb | 2 +- spec/models/form_spec.rb | 8 ++- spec/requests/schemes_controller_spec.rb | 6 +- 28 files changed, 382 insertions(+), 35 deletions(-) create mode 100644 app/models/form/setup/pages/location.rb create mode 100644 app/models/form/setup/pages/scheme.rb create mode 100644 app/models/form/setup/questions/location_id.rb create mode 100644 app/models/form/setup/questions/scheme_id.rb create mode 100644 db/migrate/20220630154441_add_location_to_case_log.rb create mode 100644 db/migrate/20220704135746_rename_location_name.rb create mode 100644 spec/models/form/setup/pages/location_spec.rb create mode 100644 spec/models/form/setup/pages/scheme_spec.rb create mode 100644 spec/models/form/setup/questions/location_id_spec.rb create mode 100644 spec/models/form/setup/questions/scheme_id_spec.rb diff --git a/app/frontend/styles/_accessible-autocomplete.scss b/app/frontend/styles/_accessible-autocomplete.scss index 3ffd9adaf..28e1b7e25 100644 --- a/app/frontend/styles/_accessible-autocomplete.scss +++ b/app/frontend/styles/_accessible-autocomplete.scss @@ -1,9 +1,27 @@ -.autocomplete__wrapper, -.autocomplete__input, -.autocomplete__hint { +// Ensure the autocomplete uses the correct typeface +.autocomplete__wrapper { font-family: $govuk-font-family; } +.autocomplete__input { + font-family: inherit; +} + +.autocomplete__option__append { + font-weight: bold; +} + +.autocomplete__option__hint { + display: block; + color: $govuk-secondary-text-colour; + + .autocomplete__option--focused &, + .autocomplete__option:hover & { + color: govuk-colour("white"); + } +} + +// Style the autocomplete if there’s an error .govuk-form-group--error { .autocomplete__input { border-color: $govuk-error-colour; @@ -15,6 +33,9 @@ } .autocomplete__dropdown-arrow-down { + // Ensure dropdown arrow can be clicked + // https://github.com/alphagov/accessible-autocomplete/issues/202 pointer-events: none; + // Ensure dropdown arrow can be seen z-index: 0; } diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 038df317f..49c3a8a73 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -24,6 +24,7 @@ class CaseLog < ApplicationRecord validates_with CaseLogValidator before_validation :recalculate_start_year!, if: :startdate_changed? + before_validation :reset_scheme_location!, if: :scheme_changed? before_validation :process_postcode_changes!, if: :postcode_full_changed? before_validation :process_previous_postcode_changes!, if: :ppostcode_full_changed? before_validation :reset_invalidated_dependent_fields! @@ -36,6 +37,7 @@ class CaseLog < ApplicationRecord belongs_to :managing_organisation, class_name: "Organisation", optional: true belongs_to :created_by, class_name: "User", optional: true belongs_to :scheme, optional: true + belongs_to :location, optional: true scope :filter_by_organisation, ->(org, _user = nil) { where(owning_organisation: org).or(where(managing_organisation: org)) } scope :filter_by_status, ->(status, _user = nil) { where status: } @@ -688,4 +690,8 @@ private def upcase_and_remove_whitespace(string) string.present? ? string.upcase.gsub(/\s+/, "") : string end + + def reset_scheme_location! + self.location = nil + end end diff --git a/app/models/derived_variables/case_log_variables.rb b/app/models/derived_variables/case_log_variables.rb index a49f0cce1..8a63727b8 100644 --- a/app/models/derived_variables/case_log_variables.rb +++ b/app/models/derived_variables/case_log_variables.rb @@ -5,6 +5,12 @@ module DerivedVariables::CaseLogVariables FeatureToggle.supported_housing_schemes_enabled? end + def scheme_has_multiple_locations? + return false unless scheme + + scheme.locations.size > 1 + end + def set_derived_fields! # TODO: Remove once we support parent/child relationships self.managing_organisation_id ||= owning_organisation_id @@ -60,6 +66,10 @@ module DerivedVariables::CaseLogVariables self.hhtype = household_type self.new_old = new_or_existing_tenant self.vacdays = property_vacant_days + + if is_supported_housing? && (scheme && scheme.locations.size == 1) + self.location = scheme.locations.first + end end private diff --git a/app/models/form/setup/pages/location.rb b/app/models/form/setup/pages/location.rb new file mode 100644 index 000000000..5b22df634 --- /dev/null +++ b/app/models/form/setup/pages/location.rb @@ -0,0 +1,19 @@ +class Form::Setup::Pages::Location < ::Form::Page + def initialize(_id, hsh, subsection) + super("location", hsh, subsection) + @header = "" + @description = "" + @questions = questions + @depends_on = [{ + "supported_housing_schemes_enabled?" => true, + "needstype" => 2, + "scheme_has_multiple_locations?" => true, + }] + end + + def questions + [ + Form::Setup::Questions::LocationId.new(nil, nil, self), + ] + end +end diff --git a/app/models/form/setup/pages/rent_type.rb b/app/models/form/setup/pages/rent_type.rb index c467efff5..317fa0e0c 100644 --- a/app/models/form/setup/pages/rent_type.rb +++ b/app/models/form/setup/pages/rent_type.rb @@ -1,13 +1,11 @@ class Form::Setup::Pages::RentType < ::Form::Page - def initialize(id, hsh, subsection) - super - @id = "rent_type" + def initialize(_id, hsh, subsection) + super("rent_type", hsh, subsection) @header = "" @description = "" @questions = questions @depends_on = [{ "supported_housing_schemes_enabled?" => true }] @derived = true - @subsection = subsection end def questions diff --git a/app/models/form/setup/pages/scheme.rb b/app/models/form/setup/pages/scheme.rb new file mode 100644 index 000000000..b60d07964 --- /dev/null +++ b/app/models/form/setup/pages/scheme.rb @@ -0,0 +1,18 @@ +class Form::Setup::Pages::Scheme < ::Form::Page + def initialize(_id, hsh, subsection) + super("scheme", hsh, subsection) + @header = "" + @description = "" + @questions = questions + @depends_on = [{ + "supported_housing_schemes_enabled?" => true, + "needstype" => 2, + }] + end + + def questions + [ + Form::Setup::Questions::SchemeId.new(nil, nil, self), + ] + end +end diff --git a/app/models/form/setup/questions/location_id.rb b/app/models/form/setup/questions/location_id.rb new file mode 100644 index 000000000..4105c98f1 --- /dev/null +++ b/app/models/form/setup/questions/location_id.rb @@ -0,0 +1,38 @@ +class Form::Setup::Questions::LocationId < ::Form::Question + def initialize(_id, hsh, page) + super("location_id", hsh, page) + @check_answer_label = "Location" + @header = "Which location is this log for?" + @hint_text = "" + @type = "radio" + @derived = true unless FeatureToggle.supported_housing_schemes_enabled? + @answer_options = answer_options + end + + def answer_options + answer_opts = {} + return answer_opts unless ActiveRecord::Base.connected? + + Location.select(:id, :postcode, :name).each_with_object(answer_opts) do |location, hsh| + hsh[location.id.to_s] = { "value" => location.postcode, "hint" => location.name } + hsh + end + end + + def displayed_answer_options(case_log) + return {} unless case_log.scheme + + scheme_location_ids = Location.where(scheme_id: case_log.scheme.id).map(&:id).map(&:to_s) + answer_options.select { |k, _v| scheme_location_ids.include?(k) } + end + + def hidden_in_check_answers?(case_log, _current_user = nil) + !supported_housing_selected?(case_log) + end + +private + + def supported_housing_selected?(case_log) + case_log.needstype == 2 + end +end diff --git a/app/models/form/setup/questions/needs_type.rb b/app/models/form/setup/questions/needs_type.rb index eb59fa675..4222cbb37 100644 --- a/app/models/form/setup/questions/needs_type.rb +++ b/app/models/form/setup/questions/needs_type.rb @@ -7,7 +7,7 @@ class Form::Setup::Questions::NeedsType < ::Form::Question @hint_text = "General needs housing includes both self-contained and shared housing without support or specific adaptations. Supported housing can include direct access hostels, group homes, residential care and nursing homes." @type = "radio" @answer_options = ANSWER_OPTIONS - @derived = true + @derived = true unless FeatureToggle.supported_housing_schemes_enabled? @page = page end diff --git a/app/models/form/setup/questions/scheme_id.rb b/app/models/form/setup/questions/scheme_id.rb new file mode 100644 index 000000000..d1ba79dcd --- /dev/null +++ b/app/models/form/setup/questions/scheme_id.rb @@ -0,0 +1,40 @@ +class Form::Setup::Questions::SchemeId < ::Form::Question + def initialize(_id, hsh, page) + super("scheme_id", hsh, page) + @check_answer_label = "Scheme name" + @header = "What scheme is this log for?" + @hint_text = "Enter scheme name or postcode" + @type = "select" + @answer_options = answer_options + @derived = true unless FeatureToggle.supported_housing_schemes_enabled? + end + + def answer_options + answer_opts = {} + return answer_opts unless ActiveRecord::Base.connected? + + Scheme.select(:id, :service_name).each_with_object(answer_opts) do |scheme, hsh| + hsh[scheme.id.to_s] = scheme.service_name + hsh + end + end + + def displayed_answer_options(case_log) + return {} unless case_log.created_by + + user_org_scheme_ids = Scheme.select(:id).where(organisation_id: case_log.created_by.organisation_id).map(&:id) + answer_options.select do |k, _v| + user_org_scheme_ids.include?(k.to_i) + end + end + + def hidden_in_check_answers?(case_log, _current_user = nil) + !supported_housing_selected?(case_log) + end + +private + + def supported_housing_selected?(case_log) + case_log.needstype == 2 + end +end diff --git a/app/models/form/setup/subsections/setup.rb b/app/models/form/setup/subsections/setup.rb index 04974c44d..0f481af6b 100644 --- a/app/models/form/setup/subsections/setup.rb +++ b/app/models/form/setup/subsections/setup.rb @@ -12,6 +12,8 @@ class Form::Subsections::Setup < ::Form::Subsection Form::Setup::Pages::Organisation.new(nil, nil, self), Form::Setup::Pages::CreatedBy.new(nil, nil, self), Form::Setup::Pages::NeedsType.new(nil, nil, self), + Form::Setup::Pages::Scheme.new(nil, nil, self), + Form::Setup::Pages::Location.new(nil, nil, self), Form::Setup::Pages::Renewal.new(nil, nil, self), Form::Setup::Pages::TenancyStartDate.new(nil, nil, self), Form::Setup::Pages::RentType.new(nil, nil, self), diff --git a/app/views/schemes/locations.html.erb b/app/views/schemes/locations.html.erb index 1aaeaf8c5..0b29024c7 100644 --- a/app/views/schemes/locations.html.erb +++ b/app/views/schemes/locations.html.erb @@ -18,7 +18,7 @@

- <%= "#{location.address_line1}, #{location.address_line2}" %> + <%= location.name %>

diff --git a/db/migrate/20220630154441_add_location_to_case_log.rb b/db/migrate/20220630154441_add_location_to_case_log.rb new file mode 100644 index 000000000..e53b8d83a --- /dev/null +++ b/db/migrate/20220630154441_add_location_to_case_log.rb @@ -0,0 +1,5 @@ +class AddLocationToCaseLog < ActiveRecord::Migration[7.0] + def change + add_reference :case_logs, :location, foreign_key: true + end +end diff --git a/db/migrate/20220704135746_rename_location_name.rb b/db/migrate/20220704135746_rename_location_name.rb new file mode 100644 index 000000000..a46f82e66 --- /dev/null +++ b/db/migrate/20220704135746_rename_location_name.rb @@ -0,0 +1,8 @@ +class RenameLocationName < ActiveRecord::Migration[7.0] + def change + change_table :locations, bulk: true do |t| + t.rename :address_line1, :name + t.remove :address_line2, type: :string + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 280bd3b59..b38a76b1a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2022_06_29_105452) do +ActiveRecord::Schema[7.0].define(version: 2022_07_04_135746) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -191,15 +191,17 @@ ActiveRecord::Schema[7.0].define(version: 2022_06_29_105452) do t.integer "joint" t.bigint "created_by_id" t.integer "illness_type_0" - t.integer "retirement_value_check" t.integer "tshortfall_known" t.integer "sheltered" + t.integer "retirement_value_check" t.integer "pregnancy_value_check" t.integer "hhtype" t.integer "new_old" t.integer "vacdays" t.bigint "scheme_id" + t.bigint "location_id" t.index ["created_by_id"], name: "index_case_logs_on_created_by_id" + t.index ["location_id"], name: "index_case_logs_on_location_id" t.index ["managing_organisation_id"], name: "index_case_logs_on_managing_organisation_id" t.index ["old_id"], name: "index_case_logs_on_old_id", unique: true t.index ["owning_organisation_id"], name: "index_case_logs_on_owning_organisation_id" @@ -241,8 +243,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_06_29_105452) do t.string "type_of_building" t.integer "wheelchair_adaptation" t.bigint "scheme_id", null: false - t.string "address_line1" - t.string "address_line2" + t.string "name" t.string "county" t.datetime "created_at", null: false t.datetime "updated_at", null: false @@ -251,7 +252,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_06_29_105452) do create_table "logs_exports", force: :cascade do |t| t.datetime "created_at", default: -> { "CURRENT_TIMESTAMP" } - t.datetime "started_at", null: false + t.datetime "started_at", precision: nil, null: false t.integer "base_number", default: 1, null: false t.integer "increment_number", default: 1, null: false t.boolean "empty_export", default: false, null: false @@ -369,6 +370,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_06_29_105452) do t.index ["item_type", "item_id"], name: "index_versions_on_item_type_and_item_id" end + add_foreign_key "case_logs", "locations" add_foreign_key "case_logs", "schemes" add_foreign_key "locations", "schemes" add_foreign_key "schemes", "organisations" diff --git a/db/seeds.rb b/db/seeds.rb index 654f1c777..0da986c48 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -116,8 +116,7 @@ unless Rails.env.test? scheme: scheme1, location_code: "S254-CU193AA", postcode: "CU19 3AA", - address_line1: "Rectory Road", - address_line2: "North Chaim", + name: "Rectory Road", type_of_unit: "Self-contained flat or bedsit", type_of_building: "Purpose-built", county: "Mid Sussex", @@ -128,8 +127,7 @@ unless Rails.env.test? scheme: scheme1, location_code: "S254-DM250DC", postcode: "DM25 0DC", - address_line1: "Smithy Lane", - address_line2: "North Kellieworth", + name: "Smithy Lane", type_of_unit: "Self-contained flat or bedsit with common facilities", type_of_building: "Converted from previous residential or non-residential property", county: "Fife", @@ -140,8 +138,7 @@ unless Rails.env.test? scheme: scheme2, location_code: "S254-YX130WP", postcode: "YX13 0WP", - address_line1: "Smithy Lane", - address_line2: "East Darwin", + name: "Smithy Lane", type_of_unit: "Shared house or hostel", type_of_building: "Converted from previous residential or non-residential property", county: "Rochford", diff --git a/spec/factories/location.rb b/spec/factories/location.rb index e4eaf43a0..e547e0bd2 100644 --- a/spec/factories/location.rb +++ b/spec/factories/location.rb @@ -2,8 +2,7 @@ FactoryBot.define do factory :location do location_code { Faker::Name.initials(number: 10) } postcode { Faker::Address.postcode.delete(" ") } - address_line1 { Faker::Address.street_name } - address_line2 { Faker::Address.city } + name { Faker::Address.street_name } type_of_unit { Faker::Lorem.word } type_of_building { Faker::Lorem.word } wheelchair_adaptation { 0 } diff --git a/spec/features/log_spec.rb b/spec/features/log_spec.rb index f88196d6f..69623aff8 100644 --- a/spec/features/log_spec.rb +++ b/spec/features/log_spec.rb @@ -90,7 +90,7 @@ RSpec.describe "Log Features" do visit("logs/#{log_id}/setup/check-answers") expect(page).to have_content("Owning organisation #{support_user.organisation.name}") expect(page).to have_content("User #{support_user.name}") - expect(page).to have_content("You have answered 2 of 7 questions") + expect(page).to have_content("You have answered 2 of 8 questions") end end end @@ -120,7 +120,7 @@ RSpec.describe "Log Features" do visit("logs/#{log_id}/setup/check-answers") expect(page).not_to have_content("Owning organisation #{support_user.organisation.name}") expect(page).not_to have_content("User #{support_user.name}") - expect(page).to have_content("You have answered 0 of 5 questions") + expect(page).to have_content("You have answered 0 of 6 questions") end end end diff --git a/spec/helpers/tasklist_helper_spec.rb b/spec/helpers/tasklist_helper_spec.rb index e0e6e94ef..0e13fa805 100644 --- a/spec/helpers/tasklist_helper_spec.rb +++ b/spec/helpers/tasklist_helper_spec.rb @@ -2,7 +2,7 @@ require "rails_helper" RSpec.describe TasklistHelper do let(:empty_case_log) { FactoryBot.create(:case_log) } - let(:case_log) { FactoryBot.create(:case_log, :in_progress) } + let(:case_log) { FactoryBot.create(:case_log, :in_progress, needstype: 1) } describe "get next incomplete section" do it "returns the first subsection name if it is not completed" do diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index b07b913fe..23176a005 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -1678,6 +1678,23 @@ RSpec.describe CaseLog do expect(case_log["tshortfall_known"]).to eq(0) end end + + context "when a case log is a supported housing log" do + before { case_log.needstype = 2 } + + context "and a scheme with a single log is selected" do + let(:scheme) { FactoryBot.create(:scheme) } + let!(:location) { FactoryBot.create(:location, scheme:) } + + before { case_log.update!(scheme:) } + + it "derives the scheme location" do + record_from_db = ActiveRecord::Base.connection.execute("select location_id from case_logs where id=#{case_log.id}").to_a[0] + expect(record_from_db["location_id"]).to eq(location.id) + expect(case_log["location_id"]).to eq(location.id) + end + end + end end describe "optional fields" do diff --git a/spec/models/form/setup/pages/location_spec.rb b/spec/models/form/setup/pages/location_spec.rb new file mode 100644 index 000000000..b3c1d06a9 --- /dev/null +++ b/spec/models/form/setup/pages/location_spec.rb @@ -0,0 +1,37 @@ +require "rails_helper" + +RSpec.describe Form::Setup::Pages::Location, type: :model do + subject(:page) { described_class.new(page_id, page_definition, subsection) } + + let(:page_id) { nil } + let(:page_definition) { nil } + let(:subsection) { instance_double(Form::Subsection) } + + 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[location_id]) + end + + it "has the correct id" do + expect(page.id).to eq("location") + 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 eq([{ + "supported_housing_schemes_enabled?" => true, + "needstype" => 2, + "scheme_has_multiple_locations?" => true, + }]) + end +end diff --git a/spec/models/form/setup/pages/scheme_spec.rb b/spec/models/form/setup/pages/scheme_spec.rb new file mode 100644 index 000000000..841516fe5 --- /dev/null +++ b/spec/models/form/setup/pages/scheme_spec.rb @@ -0,0 +1,33 @@ +require "rails_helper" + +RSpec.describe Form::Setup::Pages::Scheme, type: :model do + subject(:page) { described_class.new(page_id, page_definition, subsection) } + + let(:page_id) { nil } + let(:page_definition) { nil } + let(:subsection) { instance_double(Form::Subsection) } + + 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[scheme_id]) + end + + it "has the correct id" do + expect(page.id).to eq("scheme") + 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 eq([{ "needstype" => 2, "supported_housing_schemes_enabled?" => true }]) + end +end diff --git a/spec/models/form/setup/questions/location_id_spec.rb b/spec/models/form/setup/questions/location_id_spec.rb new file mode 100644 index 000000000..dd1bd9735 --- /dev/null +++ b/spec/models/form/setup/questions/location_id_spec.rb @@ -0,0 +1,37 @@ +require "rails_helper" + +RSpec.describe Form::Setup::Questions::LocationId, type: :model do + 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("location_id") + end + + it "has the correct header" do + expect(question.header).to eq("Which location is this log for?") + end + + it "has the correct check_answer_label" do + expect(question.check_answer_label).to eq("Location") + end + + it "has the correct type" do + expect(question.type).to eq("radio") + end + + it "is marked as derived" do + expect(question).not_to be_derived + end + + it "has the correct answer_options" do + expect(question.answer_options).to eq({}) + end +end diff --git a/spec/models/form/setup/questions/needs_type_spec.rb b/spec/models/form/setup/questions/needs_type_spec.rb index 485ac4164..c10fcb3b1 100644 --- a/spec/models/form/setup/questions/needs_type_spec.rb +++ b/spec/models/form/setup/questions/needs_type_spec.rb @@ -27,8 +27,8 @@ RSpec.describe Form::Setup::Questions::NeedsType, type: :model do expect(question.type).to eq("radio") end - it "is marked as derived" do - expect(question.derived?).to be true + it "is not marked as derived" do + expect(question).not_to be_derived end it "has the correct answer_options" do diff --git a/spec/models/form/setup/questions/scheme_id_spec.rb b/spec/models/form/setup/questions/scheme_id_spec.rb new file mode 100644 index 000000000..2a71fb2a5 --- /dev/null +++ b/spec/models/form/setup/questions/scheme_id_spec.rb @@ -0,0 +1,58 @@ +require "rails_helper" + +RSpec.describe Form::Setup::Questions::SchemeId, type: :model do + 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) } + let(:user) { FactoryBot.create(:user, organisation_id: organisation.id) } + let(:scheme) { FactoryBot.create(:scheme, organisation_id: organisation.id) } + let(:case_log) { FactoryBot.create(:case_log, created_by: user) } + + before do + FactoryBot.create(:scheme, organisation_id: organisation_2.id) + end + + 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.service_name } + expect(question.displayed_answer_options(case_log)).to eq(expected_answer) + end + end +end diff --git a/spec/models/form/setup/subsections/setup_spec.rb b/spec/models/form/setup/subsections/setup_spec.rb index 8da7123a1..08744eadc 100644 --- a/spec/models/form/setup/subsections/setup_spec.rb +++ b/spec/models/form/setup/subsections/setup_spec.rb @@ -16,6 +16,8 @@ RSpec.describe Form::Setup::Subsections::Setup, type: :model do %w[organisation created_by needs_type + scheme + location renewal tenancy_start_date rent_type diff --git a/spec/models/form_handler_spec.rb b/spec/models/form_handler_spec.rb index b8959ca88..2222d737e 100644 --- a/spec/models/form_handler_spec.rb +++ b/spec/models/form_handler_spec.rb @@ -17,7 +17,7 @@ RSpec.describe FormHandler do form_handler = described_class.instance form = form_handler.get_form(test_form_name) expect(form).to be_a(Form) - expect(form.pages.count).to eq(42) + expect(form.pages.count).to eq(44) end end diff --git a/spec/models/form_spec.rb b/spec/models/form_spec.rb index 5b1cb1924..b1870d709 100644 --- a/spec/models/form_spec.rb +++ b/spec/models/form_spec.rb @@ -177,8 +177,10 @@ RSpec.describe Form, type: :model do end describe "invalidated_page_questions" do + let(:case_log) { FactoryBot.create(:case_log, :in_progress, needstype: 1) } + context "when dependencies are not met" do - let(:expected_invalid) { %w[condition_effects cbl conditional_question_no_second_question net_income_value_check dependent_question offered layear declaration] } + let(:expected_invalid) { %w[scheme_id location_id condition_effects cbl conditional_question_no_second_question net_income_value_check dependent_question offered layear declaration] } it "returns an array of question keys whose pages conditions are not met" do expect(form.invalidated_page_questions(case_log).map(&:id).uniq).to eq(expected_invalid) @@ -186,7 +188,7 @@ RSpec.describe Form, type: :model do end context "with two pages having the same question and only one has dependencies met" do - let(:expected_invalid) { %w[condition_effects cbl conditional_question_no_second_question net_income_value_check dependent_question offered layear declaration] } + let(:expected_invalid) { %w[scheme_id location_id condition_effects cbl conditional_question_no_second_question net_income_value_check dependent_question offered layear declaration] } it "returns an array of question keys whose pages conditions are not met" do case_log["preg_occ"] = "No" @@ -195,7 +197,7 @@ RSpec.describe Form, type: :model do end context "when a question is marked as `derived` and `depends_on: false`" do - let(:case_log) { FactoryBot.build(:case_log, :in_progress, startdate: Time.utc(2023, 2, 2, 10, 36, 49)) } + let(:case_log) { FactoryBot.build(:case_log, :in_progress, startdate: Time.utc(2022, 4, 2, 10, 36, 49)) } it "does not count it's questions as invalidated" do expect(form.enabled_page_questions(case_log).map(&:id).uniq).to include("tshortfall_known") diff --git a/spec/requests/schemes_controller_spec.rb b/spec/requests/schemes_controller_spec.rb index 30cba5cf2..4dc88fdcb 100644 --- a/spec/requests/schemes_controller_spec.rb +++ b/spec/requests/schemes_controller_spec.rb @@ -316,8 +316,7 @@ RSpec.describe SchemesController, type: :request do expect(page).to have_content(location.type_of_unit) expect(page).to have_content(location.type_of_building) expect(page).to have_content(location.wheelchair_adaptation) - expect(page).to have_content(location.address_line1) - expect(page).to have_content(location.address_line2) + expect(page).to have_content(location.name) end end @@ -398,8 +397,7 @@ RSpec.describe SchemesController, type: :request do expect(page).to have_content(location.type_of_unit) expect(page).to have_content(location.type_of_building) expect(page).to have_content(location.wheelchair_adaptation) - expect(page).to have_content(location.address_line1) - expect(page).to have_content(location.address_line2) + expect(page).to have_content(location.name) end end