Browse Source

Extract and fix answer selected method (#755)

* Extract and fix answer selected method

* update method name

* add guard clause for answer_selected?

* lint 🤦‍♀️
pull/757/head
kosiakkatrina 2 years ago committed by GitHub
parent
commit
50f64f911d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      app/models/form/question.rb
  2. 8
      app/models/form/setup/questions/scheme_id.rb
  3. 2
      app/views/form/_select_question.html.erb
  4. 8
      spec/features/schemes_spec.rb
  5. 29
      spec/models/form/setup/questions/owning_organisation_id_spec.rb
  6. 5
      spec/models/form/setup/questions/property_reference_spec.rb
  7. 36
      spec/models/form/setup/questions/scheme_id_spec.rb

6
app/models/form/question.rb

@ -228,6 +228,12 @@ class Form::Question
resource.hint
end
def answer_selected?(case_log, answer)
return false unless type == "select"
case_log[id].to_s == answer.id.to_s
end
private
def selected_answer_option_is_derived?(case_log)

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

@ -10,7 +10,7 @@ class Form::Setup::Questions::SchemeId < ::Form::Question
end
def answer_options
answer_opts = {}
answer_opts = { "" => "Select an option" }
return answer_opts unless ActiveRecord::Base.connected?
Scheme.select(:id, :service_name, :primary_client_group, :secondary_client_group).each_with_object(answer_opts) do |scheme, hsh|
@ -24,7 +24,7 @@ class Form::Setup::Questions::SchemeId < ::Form::Question
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.today)).map(&:id)
answer_options.select do |k, _v|
user_org_scheme_ids.include?(k.to_i)
user_org_scheme_ids.include?(k.to_i) || k.blank?
end
end
@ -32,6 +32,10 @@ class Form::Setup::Questions::SchemeId < ::Form::Question
!supported_housing_selected?(case_log)
end
def answer_selected?(case_log, answer)
case_log[id] == answer.name || case_log[id] == answer.resource
end
private
def supported_housing_selected?(case_log)

2
app/views/form/_select_question.html.erb

@ -12,7 +12,7 @@
data-synonyms="<%= question.answer_option_synonyms(answer.resource) %>"
data-append="<%= question.answer_option_append(answer.resource) %>"
data-hint="<%= question.answer_option_hint(answer.resource) %>"
<%= @case_log[question.id] == answer.name || @case_log[question.id] == answer.resource || @case_log[question.id].to_s == answer.id ? "selected" : "" %>
<%= question.answer_selected?(@case_log, answer) ? "selected" : "" %>
<%= answer.id == "" ? "disabled" : "" %>><%= answer.name || answer.resource %></option>
<% end %>
<% end %>

8
spec/features/schemes_spec.rb

@ -962,25 +962,25 @@ RSpec.describe "Schemes scheme Features" do
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)
expect(find("#case-log-scheme-id-field").all("option").count).to eq(4)
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)
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 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)
expect(find("#case-log-scheme-id-field").all("option").count).to eq(4)
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)
expect(find("#case-log-scheme-id-field").all("option").count).to eq(4)
end
end
end

29
spec/models/form/setup/questions/owning_organisation_id_spec.rb

@ -10,6 +10,7 @@ RSpec.describe Form::Setup::Questions::OwningOrganisationId, type: :model do
let(:form) { instance_double(Form) }
let!(:organisation_1) { FactoryBot.create(:organisation, name: "first test org") }
let!(:organisation_2) { FactoryBot.create(:organisation, name: "second test org") }
let(:case_log) { FactoryBot.create(:case_log) }
let(:expected_answer_options) do
{
"" => "Select an option",
@ -65,4 +66,32 @@ RSpec.describe Form::Setup::Questions::OwningOrganisationId, type: :model do
expect(question.hidden_in_check_answers?(nil, user)).to be true
end
end
context "when the question is not answered" do
it "returns 'select an option' as selected answer" do
case_log.update!(owning_organisation: nil)
answers = question.displayed_answer_options(case_log).map { |key, value| OpenStruct.new(id: key, name: nil, resource: value) }
answers.each do |answer|
if answer.resource == "Select an option"
expect(question.answer_selected?(case_log, answer)).to eq(true)
else
expect(question.answer_selected?(case_log, answer)).to eq(false)
end
end
end
end
context "when the question is answered" do
it "returns 'select an option' as selected answer" do
case_log.update!(owning_organisation: organisation_1)
answers = question.displayed_answer_options(case_log).map { |key, value| OpenStruct.new(id: key, name: value.respond_to?(:service_name) ? value.service_name : nil, resource: value) }
answers.each do |answer|
if answer.id == organisation_1.id
expect(question.answer_selected?(case_log, answer)).to eq(true)
else
expect(question.answer_selected?(case_log, answer)).to eq(false)
end
end
end
end
end

5
spec/models/form/setup/questions/property_reference_spec.rb

@ -6,6 +6,7 @@ RSpec.describe Form::Setup::Questions::PropertyReference, type: :model do
let(:question_id) { nil }
let(:question_definition) { nil }
let(:page) { instance_double(Form::Page) }
let(:case_log) { FactoryBot.create(:case_log) }
it "has correct page" do
expect(question.page).to eq(page)
@ -38,4 +39,8 @@ RSpec.describe Form::Setup::Questions::PropertyReference, type: :model do
it "is not marked as derived" do
expect(question.derived?).to be false
end
it "returns false for answer_selected" do
expect(question.answer_selected?(case_log, {})).to be false
end
end

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

@ -44,7 +44,7 @@ RSpec.describe Form::Setup::Questions::SchemeId, type: :model do
let(:organisation_2) { FactoryBot.create(:organisation) }
let(:user) { FactoryBot.create(:user, organisation:) }
let(:scheme) { FactoryBot.create(:scheme, owning_organisation: organisation) }
let(:case_log) { FactoryBot.create(:case_log, created_by: user) }
let(:case_log) { FactoryBot.create(:case_log, created_by: user, needstype: 2) }
before do
FactoryBot.create(:scheme, owning_organisation: organisation_2)
@ -56,14 +56,42 @@ 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.to_s => scheme }
expected_answer = { "" => "Select an option", scheme.id.to_s => scheme }
expect(question.displayed_answer_options(case_log)).to eq(expected_answer)
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({})
it "returns a hash with one empty option" do
expect(question.displayed_answer_options(case_log)).to eq({ "" => "Select an option" })
end
end
context "when the question is not answered" do
it "returns 'select an option' as selected answer" do
case_log.update!(scheme: nil)
answers = question.displayed_answer_options(case_log).map { |key, value| OpenStruct.new(id: key, name: value.respond_to?(:service_name) ? value.service_name : nil, resource: value) }
answers.each do |answer|
if answer.resource == "Select an option"
expect(question.answer_selected?(case_log, answer)).to eq(true)
else
expect(question.answer_selected?(case_log, answer)).to eq(false)
end
end
end
end
context "when the question is answered" do
it "returns 'select an option' as selected answer" do
case_log.update!(scheme:)
answers = question.displayed_answer_options(case_log).map { |key, value| OpenStruct.new(id: key, name: value.respond_to?(:service_name) ? value.service_name : nil, resource: value) }
answers.each do |answer|
if answer.id == scheme.id
expect(question.answer_selected?(case_log, answer)).to eq(true)
else
expect(question.answer_selected?(case_log, answer)).to eq(false)
end
end
end
end
end

Loading…
Cancel
Save