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? FeatureToggle.supported_housing_schemes_enabled?
end end
def scheme_has_multiple_locations?
return false unless scheme
scheme.locations.size > 1
end
def set_derived_fields! def set_derived_fields!
# TODO: Remove once we support parent/child relationships # TODO: Remove once we support parent/child relationships
self.managing_organisation_id ||= owning_organisation_id 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 = "" @header = ""
@description = "" @description = ""
@questions = questions @questions = questions
# Only display if there is more than one location
@depends_on = [{ @depends_on = [{
"supported_housing_schemes_enabled?" => true, "supported_housing_schemes_enabled?" => true,
"needstype" => 2, "needstype" => 2,
"scheme.locations.size" => { "scheme_has_multiple_locations?" => true,
"operator" => ">",
"operand" => 1,
},
}] }]
@derived = true @derived = true
end end
def questions def questions
[ [
Form::Setup::Questions::Location.new(nil, nil, self), Form::Setup::Questions::LocationId.new(nil, nil, self),
] ]
end end
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 class Form::Setup::Questions::LocationId < ::Form::Question
def initialize(id, hsh, page) def initialize(_id, hsh, page)
super super("location_id", hsh, page)
@id = "location"
@check_answer_label = "Location" @check_answer_label = "Location"
@header = "Which location is this log for?" @header = "Which location is this log for?"
@hint_text = "" @hint_text = ""
@type = "radio" @type = "radio"
@derived = true unless FeatureToggle.supported_housing_schemes_enabled? @derived = true unless FeatureToggle.supported_housing_schemes_enabled?
@page = page
@answer_options = answer_options @answer_options = answer_options
end end
@ -15,8 +13,8 @@ class Form::Setup::Questions::Location < ::Form::Question
answer_opts = {} answer_opts = {}
return answer_opts unless ActiveRecord::Base.connected? return answer_opts unless ActiveRecord::Base.connected?
Location.select(:id, :postcode).each_with_object(answer_opts) do |location, hsh| Location.select(:id, :postcode, :address_line1).each_with_object(answer_opts) do |location, hsh|
hsh[location.id] = location.postcode hsh[location.id.to_s] = { "value" => location.postcode, "hint" => location.address_line1 }
hsh hsh
end end
end end
@ -24,7 +22,7 @@ class Form::Setup::Questions::Location < ::Form::Question
def displayed_answer_options(case_log) def displayed_answer_options(case_log)
return {} unless case_log.scheme 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) } answer_options.select { |k, _v| scheme_location_ids.include?(k) }
end 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" @hint_text = "Enter scheme name or postcode"
@type = "select" @type = "select"
@answer_options = answer_options @answer_options = answer_options
@derived = true unless FeatureToggle.supported_housing_schemes_enabled?
end end
def answer_options def answer_options
@ -31,10 +32,6 @@ class Form::Setup::Questions::SchemeId < ::Form::Question
private private
def selected_answer_option_is_derived?(_case_log)
false
end
def supported_housing_selected?(case_log) def supported_housing_selected?(case_log)
case_log.needstype == 2 case_log.needstype == 2
end end

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

@ -12,7 +12,7 @@ RSpec.describe Form::Setup::Pages::Location, type: :model do
end end
it "has correct questions" do 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 end
it "has the correct id" do 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([{ expect(page.depends_on).to eq([{
"supported_housing_schemes_enabled?" => true, "supported_housing_schemes_enabled?" => true,
"needstype" => 2, "needstype" => 2,
"scheme.locations.size" => { "scheme_has_multiple_locations?" => true,
"operator" => ">",
"operand" => 1,
},
}]) }])
end end
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" 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) } subject(:question) { described_class.new(question_id, question_definition, page) }
let(:question_id) { nil } let(:question_id) { nil }
@ -12,7 +12,7 @@ RSpec.describe Form::Setup::Questions::Location, type: :model do
end end
it "has the correct id" do it "has the correct id" do
expect(question.id).to eq("location") expect(question.id).to eq("location_id")
end end
it "has the correct header" do 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 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 => scheme.service_name } expected_answer = { scheme.id.to_s => scheme.service_name }
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

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) } let(:case_log) { FactoryBot.create(:case_log, :in_progress, needstype: 1) }
context "when dependencies are not met" do 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 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) 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 end
context "with two pages having the same question and only one has dependencies met" do 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 it "returns an array of question keys whose pages conditions are not met" do
case_log["preg_occ"] = "No" case_log["preg_occ"] = "No"

Loading…
Cancel
Save