Browse Source

Fix tests and reference issues

pull/705/head
Stéphane Meny 3 years ago
parent
commit
bbe7054f1e
No known key found for this signature in database
GPG Key ID: 9D0AFEA988527923
  1. 6
      app/models/derived_variables/case_log_variables.rb
  2. 8
      app/models/form/setup/pages/location.rb
  3. 14
      app/models/form/setup/questions/location_id.rb
  4. 5
      app/models/form/setup/questions/scheme_id.rb
  5. 7
      spec/models/form/setup/pages/location_spec.rb
  6. 4
      spec/models/form/setup/questions/location_id_spec.rb
  7. 2
      spec/models/form/setup/questions/scheme_id_spec.rb
  8. 4
      spec/models/form_spec.rb

6
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

8
app/models/form/setup/pages/location.rb

@ -4,21 +4,17 @@ class Form::Setup::Pages::Location < ::Form::Page
@header = ""
@description = ""
@questions = questions
# Only display if there is more than one location
@depends_on = [{
"supported_housing_schemes_enabled?" => true,
"needstype" => 2,
"scheme.locations.size" => {
"operator" => ">",
"operand" => 1,
},
"scheme_has_multiple_locations?" => true,
}]
@derived = true
end
def questions
[
Form::Setup::Questions::Location.new(nil, nil, self),
Form::Setup::Questions::LocationId.new(nil, nil, self),
]
end
end

14
app/models/form/setup/questions/location.rb → app/models/form/setup/questions/location_id.rb

@ -1,13 +1,11 @@
class Form::Setup::Questions::Location < ::Form::Question
def initialize(id, hsh, page)
super
@id = "location"
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?
@page = page
@answer_options = answer_options
end
@ -15,8 +13,8 @@ class Form::Setup::Questions::Location < ::Form::Question
answer_opts = {}
return answer_opts unless ActiveRecord::Base.connected?
Location.select(:id, :postcode).each_with_object(answer_opts) do |location, hsh|
hsh[location.id] = location.postcode
Location.select(:id, :postcode, :address_line1).each_with_object(answer_opts) do |location, hsh|
hsh[location.id.to_s] = { "value" => location.postcode, "hint" => location.address_line1 }
hsh
end
end
@ -24,7 +22,7 @@ class Form::Setup::Questions::Location < ::Form::Question
def displayed_answer_options(case_log)
return {} unless case_log.scheme
scheme_location_ids = Location.where("scheme_id = #{case_log.scheme.id}").map(&:id)
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

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

@ -6,6 +6,7 @@ class Form::Setup::Questions::SchemeId < ::Form::Question
@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
@ -31,10 +32,6 @@ class Form::Setup::Questions::SchemeId < ::Form::Question
private
def selected_answer_option_is_derived?(_case_log)
false
end
def supported_housing_selected?(case_log)
case_log.needstype == 2
end

7
spec/models/form/setup/pages/location_spec.rb

@ -12,7 +12,7 @@ RSpec.describe Form::Setup::Pages::Location, type: :model do
end
it "has correct questions" do
expect(page.questions.map(&:id)).to eq(%w[location])
expect(page.questions.map(&:id)).to eq(%w[location_id])
end
it "has the correct id" do
@ -31,10 +31,7 @@ RSpec.describe Form::Setup::Pages::Location, type: :model do
expect(page.depends_on).to eq([{
"supported_housing_schemes_enabled?" => true,
"needstype" => 2,
"scheme.locations.size" => {
"operator" => ">",
"operand" => 1,
},
"scheme_has_multiple_locations?" => true,
}])
end
end

4
spec/models/form/setup/questions/location_spec.rb → spec/models/form/setup/questions/location_id_spec.rb

@ -1,6 +1,6 @@
require "rails_helper"
RSpec.describe Form::Setup::Questions::Location, type: :model do
RSpec.describe Form::Setup::Questions::LocationId, type: :model do
subject(:question) { described_class.new(question_id, question_definition, page) }
let(:question_id) { nil }
@ -12,7 +12,7 @@ RSpec.describe Form::Setup::Questions::Location, type: :model do
end
it "has the correct id" do
expect(question.id).to eq("location")
expect(question.id).to eq("location_id")
end
it "has the correct header" do

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

@ -51,7 +51,7 @@ RSpec.describe Form::Setup::Questions::SchemeId, type: :model do
end
it "has the correct answer_options based on the schemes the user's organisation owns or manages" do
expected_answer = { scheme.id => scheme.service_name }
expected_answer = { scheme.id.to_s => scheme.service_name }
expect(question.displayed_answer_options(case_log)).to eq(expected_answer)
end
end

4
spec/models/form_spec.rb

@ -180,7 +180,7 @@ RSpec.describe Form, type: :model do
let(:case_log) { FactoryBot.create(:case_log, :in_progress, needstype: 1) }
context "when dependencies are not met" do
let(:expected_invalid) { %w[scheme_id location 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)
@ -188,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[scheme_id location 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"

Loading…
Cancel
Save