Browse Source

Cldc 1503 lead nationality (#909)

* Add nationality and other nationality questions

* Add nationality page

* Add national columns

* Add hidden_in_check_answers

* Update conditional question controller to work with both log types

* update tests

* lint

* Remove console.log

* tests and migration

* typo

* tests

* tests

* feat: update schema and tests

* Add nationality and other nationality questions

* Add nationality page

* Add national columns

* Add hidden_in_check_answers

* Update conditional question controller to work with both log types

* update tests

* lint

* Remove console.log

* tests and migration

* typo

* tests

* feat: update schema and tests

* tests: update tests

* feat: update schema.rb

Co-authored-by: natdeanlewissoftwire <nat.dean-lewis@softwire.com>
pull/931/head
kosiakkatrina 2 years ago committed by GitHub
parent
commit
b22c6c961a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      app/frontend/controllers/conditional_question_controller.js
  2. 2
      app/helpers/question_attribute_helper.rb
  3. 16
      app/models/form/sales/pages/nationality1.rb
  4. 30
      app/models/form/sales/questions/nationality1.rb
  5. 10
      app/models/form/sales/questions/other_nationality1.rb
  6. 5
      app/models/form/sales/subsections/household_characteristics.rb
  7. 8
      db/migrate/20220927082602_add_national_column.rb
  8. 14
      db/schema.rb
  9. 1
      spec/factories/sales_log.rb
  10. 18
      spec/features/form/conditional_questions_spec.rb
  11. 4
      spec/helpers/question_attribute_helper_spec.rb
  12. 33
      spec/models/form/sales/pages/nationality1_spec.rb
  13. 63
      spec/models/form/sales/questions/nationality1_spec.rb
  14. 37
      spec/models/form/sales/questions/other_nationality1_spec.rb
  15. 5
      spec/models/form/sales/subsections/household_characteristics_spec.rb
  16. 4
      spec/models/form_handler_spec.rb

8
app/frontend/controllers/conditional_question_controller.js

@ -8,14 +8,16 @@ export default class extends Controller {
displayConditional () { displayConditional () {
if (this.element.checked) { if (this.element.checked) {
const selectedValue = this.element.value const selectedValue = this.element.value
const conditionalFor = JSON.parse(this.element.dataset.info) const dataInfo = JSON.parse(this.element.dataset.info)
const conditionalFor = dataInfo.conditional_questions
const logType = dataInfo.log_type
Object.entries(conditionalFor).forEach(([targetQuestion, conditions]) => { Object.entries(conditionalFor).forEach(([targetQuestion, conditions]) => {
if (!conditions.map(String).includes(String(selectedValue))) { if (!conditions.map(String).includes(String(selectedValue))) {
const textNumericInput = document.getElementById(`lettings-log-${targetQuestion.replaceAll('_', '-')}-field`) const textNumericInput = document.getElementById(`${logType}-log-${targetQuestion.replaceAll('_', '-')}-field`)
if (textNumericInput == null) { if (textNumericInput == null) {
const dateInputs = [1, 2, 3].map((idx) => { const dateInputs = [1, 2, 3].map((idx) => {
return document.getElementById(`lettings_log_${targetQuestion}_${idx}i`) return document.getElementById(`${logType}_log_${targetQuestion}_${idx}i`)
}) })
this.clearDateInputs(dateInputs) this.clearDateInputs(dateInputs)
} else { } else {

2
app/helpers/question_attribute_helper.rb

@ -27,7 +27,7 @@ private
{ {
"data-controller": "conditional-question", "data-controller": "conditional-question",
"data-action": "click->conditional-question#displayConditional", "data-action": "click->conditional-question#displayConditional",
"data-info": question.conditional_for.to_json, "data-info": { conditional_questions: question.conditional_for, log_type: question.form.type }.to_json,
} }
end end
end end

16
app/models/form/sales/pages/nationality1.rb

@ -0,0 +1,16 @@
class Form::Sales::Pages::Nationality1 < ::Form::Page
def initialize(id, hsh, subsection)
super
@id = "buyer_1_nationality"
@header = ""
@description = ""
@subsection = subsection
end
def questions
@questions ||= [
Form::Sales::Questions::Nationality1.new(nil, nil, self),
Form::Sales::Questions::OtherNationality1.new(nil, nil, self),
]
end
end

30
app/models/form/sales/questions/nationality1.rb

@ -0,0 +1,30 @@
class Form::Sales::Questions::Nationality1 < ::Form::Question
def initialize(id, hsh, page)
super
@id = "national"
@check_answer_label = "Buyer 1’s nationality"
@header = "What is buyer 1’s nationality?"
@type = "radio"
@hint_text = "Buyer 1 is the person in the household who does the most paid work. If it’s a joint purchase and the buyers do the same amount of paid work, buyer 1 is whoever is the oldest."
@page = page
@answer_options = ANSWER_OPTIONS
@conditional_for = {
"othernational" => [12],
}
@hidden_in_check_answers = {
"depends_on" => [
{
"national" => 12,
},
],
}
end
ANSWER_OPTIONS = {
"18" => { "value" => "United Kingdom" },
"17" => { "value" => "Republic of Ireland" },
"19" => { "value" => "European Economic Area (EEA), excluding ROI" },
"12" => { "value" => "Other" },
"13" => { "value" => "Buyer prefers not to say" },
}.freeze
end

10
app/models/form/sales/questions/other_nationality1.rb

@ -0,0 +1,10 @@
class Form::Sales::Questions::OtherNationality1 < ::Form::Question
def initialize(id, hsh, page)
super
@id = "othernational"
@check_answer_label = "Buyer 1’s nationality"
@header = "Nationality"
@type = "text"
@page = page
end
end

5
app/models/form/sales/subsections/household_characteristics.rb

@ -12,14 +12,15 @@ class Form::Sales::Subsections::HouseholdCharacteristics < ::Form::Subsection
Form::Sales::Pages::BuyerInterview.new(nil, nil, self), Form::Sales::Pages::BuyerInterview.new(nil, nil, self),
Form::Sales::Pages::Age1.new(nil, nil, self), Form::Sales::Pages::Age1.new(nil, nil, self),
Form::Sales::Pages::GenderIdentity1.new(nil, nil, self), Form::Sales::Pages::GenderIdentity1.new(nil, nil, self),
Form::Sales::Pages::Buyer1LiveInProperty.new(nil, nil, self),
Form::Sales::Pages::Buyer2RelationshipToBuyer1.new(nil, nil, self),
Form::Sales::Pages::Buyer1EthnicGroup.new(nil, nil, self), Form::Sales::Pages::Buyer1EthnicGroup.new(nil, nil, self),
Form::Sales::Pages::Buyer1EthnicBackgroundBlack.new(nil, nil, self), Form::Sales::Pages::Buyer1EthnicBackgroundBlack.new(nil, nil, self),
Form::Sales::Pages::Buyer1EthnicBackgroundAsian.new(nil, nil, self), Form::Sales::Pages::Buyer1EthnicBackgroundAsian.new(nil, nil, self),
Form::Sales::Pages::Buyer1EthnicBackgroundArab.new(nil, nil, self), Form::Sales::Pages::Buyer1EthnicBackgroundArab.new(nil, nil, self),
Form::Sales::Pages::Buyer1EthnicBackgroundMixed.new(nil, nil, self), Form::Sales::Pages::Buyer1EthnicBackgroundMixed.new(nil, nil, self),
Form::Sales::Pages::Buyer1EthnicBackgroundWhite.new(nil, nil, self), Form::Sales::Pages::Buyer1EthnicBackgroundWhite.new(nil, nil, self),
Form::Sales::Pages::Nationality1.new(nil, nil, self),
Form::Sales::Pages::Buyer1LiveInProperty.new(nil, nil, self),
Form::Sales::Pages::Buyer2RelationshipToBuyer1.new(nil, nil, self),
Form::Sales::Pages::Age2.new(nil, nil, self), Form::Sales::Pages::Age2.new(nil, nil, self),
Form::Sales::Pages::GenderIdentity2.new(nil, nil, self), Form::Sales::Pages::GenderIdentity2.new(nil, nil, self),
Form::Sales::Pages::Buyer2WorkingSituation.new(nil, nil, self), Form::Sales::Pages::Buyer2WorkingSituation.new(nil, nil, self),

8
db/migrate/20220927082602_add_national_column.rb

@ -0,0 +1,8 @@
class AddNationalColumn < ActiveRecord::Migration[7.0]
def change
change_table :sales_logs, bulk: true do |t|
t.column :national, :integer
t.column :othernational, :string
end
end
end

14
db/schema.rb

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2022_10_03_150610) do ActiveRecord::Schema[7.0].define(version: 2022_10_04_095132) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -329,22 +329,24 @@ ActiveRecord::Schema[7.0].define(version: 2022_10_03_150610) do
t.string "purchid" t.string "purchid"
t.integer "type" t.integer "type"
t.integer "ownershipsch" t.integer "ownershipsch"
t.string "othtype"
t.integer "jointmore"
t.integer "jointpur" t.integer "jointpur"
t.string "othtype"
t.integer "beds" t.integer "beds"
t.integer "companybuy" t.integer "jointmore"
t.integer "age1" t.integer "age1"
t.integer "age1_known" t.integer "age1_known"
t.string "sex1" t.string "sex1"
t.integer "national"
t.string "othernational"
t.integer "buy1livein" t.integer "buy1livein"
t.integer "companybuy"
t.integer "buylivein" t.integer "buylivein"
t.integer "builtype" t.integer "builtype"
t.integer "proptype" t.integer "proptype"
t.string "relat2"
t.string "otherrelat2"
t.integer "age2" t.integer "age2"
t.integer "age2_known" t.integer "age2_known"
t.string "relat2"
t.string "otherrelat2"
t.integer "ethnic" t.integer "ethnic"
t.integer "ethnic_group" t.integer "ethnic_group"
t.string "sex2" t.string "sex2"

1
spec/factories/sales_log.rb

@ -24,6 +24,7 @@ FactoryBot.define do
age1_known { 0 } age1_known { 0 }
age1 { 30 } age1 { 30 }
sex1 { "X" } sex1 { "X" }
national { 18 }
buy1livein { 1 } buy1livein { 1 }
relat2 { "P" } relat2 { "P" }
proptype { 1 } proptype { 1 }

18
spec/features/form/conditional_questions_spec.rb

@ -12,6 +12,14 @@ RSpec.describe "Form Conditional Questions" do
managing_organisation: user.organisation, managing_organisation: user.organisation,
) )
end end
let(:sales_log) do
FactoryBot.create(
:sales_log,
:completed,
owning_organisation: user.organisation,
managing_organisation: user.organisation,
)
end
let(:id) { lettings_log.id } let(:id) { lettings_log.id }
let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") } let(:fake_2021_2022_form) { Form.new("spec/fixtures/forms/2021_2022.json") }
@ -44,5 +52,15 @@ RSpec.describe "Form Conditional Questions" do
visit("/lettings-logs/#{id}/property-postcode") visit("/lettings-logs/#{id}/property-postcode")
expect(page).to have_field("lettings-log-postcode-full-field", with: "NW1 6RT") expect(page).to have_field("lettings-log-postcode-full-field", with: "NW1 6RT")
end end
it "gets cleared if the conditional question is hidden after editing the answer" do
sales_log.update!(national: 12, othernational: "other")
visit("/sales-logs/#{sales_log.id}/buyer-1-nationality")
expect(page).to have_field("sales-log-othernational-field", with: "other")
choose("sales-log-national-18-field", allow_label_click: true)
choose("sales-log-national-12-field", allow_label_click: true)
expect(page).to have_field("sales-log-othernational-field", with: "")
end
end end
end end

4
spec/helpers/question_attribute_helper_spec.rb

@ -40,7 +40,7 @@ RSpec.describe QuestionAttributeHelper do
"conditional_for" => { "conditional_for" => {
"next_question": ">1", "next_question": ">1",
}, },
}, nil) }, form.get_page("rent"))
end end
let(:expected_attribs) do let(:expected_attribs) do
{ {
@ -48,7 +48,7 @@ RSpec.describe QuestionAttributeHelper do
"data-action": "input->numeric-question#calculateFields click->conditional-question#displayConditional", "data-action": "input->numeric-question#calculateFields click->conditional-question#displayConditional",
"data-target": "lettings-log-#{question.result_field.to_s.dasherize}-field", "data-target": "lettings-log-#{question.result_field.to_s.dasherize}-field",
"data-calculated": question.fields_to_add.to_json, "data-calculated": question.fields_to_add.to_json,
"data-info": question.conditional_for.to_json, "data-info": { conditional_questions: question.conditional_for, log_type: "lettings" }.to_json,
} }
end end

33
spec/models/form/sales/pages/nationality1_spec.rb

@ -0,0 +1,33 @@
require "rails_helper"
RSpec.describe Form::Sales::Pages::Nationality1, 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[national othernational])
end
it "has the correct id" do
expect(page.id).to eq("buyer_1_nationality")
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 correct depends_on" do
expect(page.depends_on).to be_nil
end
end

63
spec/models/form/sales/questions/nationality1_spec.rb

@ -0,0 +1,63 @@
require "rails_helper"
RSpec.describe Form::Sales::Questions::Nationality1, 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("national")
end
it "has the correct header" do
expect(question.header).to eq("What is buyer 1’s nationality?")
end
it "has the correct check_answer_label" do
expect(question.check_answer_label).to eq("Buyer 1’s nationality")
end
it "has the correct type" do
expect(question.type).to eq("radio")
end
it "is not marked as derived" do
expect(question.derived?).to be false
end
it "has the correct hint" do
expect(question.hint_text).to eq("Buyer 1 is the person in the household who does the most paid work. If it’s a joint purchase and the buyers do the same amount of paid work, buyer 1 is whoever is the oldest.")
end
it "has the correct answer_options" do
expect(question.answer_options).to eq({
"18" => { "value" => "United Kingdom" },
"17" => { "value" => "Republic of Ireland" },
"19" => { "value" => "European Economic Area (EEA), excluding ROI" },
"12" => { "value" => "Other" },
"13" => { "value" => "Buyer prefers not to say" },
})
end
it "has correct conditional for" do
expect(question.conditional_for).to eq({
"othernational" => [12],
})
end
it "has correct hidden in check answers" do
expect(question.hidden_in_check_answers).to eq({
"depends_on" => [
{
"national" => 12,
},
],
})
end
end

37
spec/models/form/sales/questions/other_nationality1_spec.rb

@ -0,0 +1,37 @@
require "rails_helper"
RSpec.describe Form::Sales::Questions::OtherNationality1, 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("othernational")
end
it "has the correct header" do
expect(question.header).to eq("Nationality")
end
it "has the correct check_answer_label" do
expect(question.check_answer_label).to eq("Buyer 1’s nationality")
end
it "has the correct type" do
expect(question.type).to eq("text")
end
it "is not marked as derived" do
expect(question.derived?).to be false
end
it "has the correct hint" do
expect(question.hint_text).to be_nil
end
end

5
spec/models/form/sales/subsections/household_characteristics_spec.rb

@ -17,14 +17,15 @@ RSpec.describe Form::Sales::Subsections::HouseholdCharacteristics, type: :model
buyer_interview buyer_interview
buyer_1_age buyer_1_age
buyer_1_gender_identity buyer_1_gender_identity
buyer_1_live_in_property
buyer_2_relationship_to_buyer_1
buyer_1_ethnic_group buyer_1_ethnic_group
buyer_1_ethnic_background_black buyer_1_ethnic_background_black
buyer_1_ethnic_background_asian buyer_1_ethnic_background_asian
buyer_1_ethnic_background_arab buyer_1_ethnic_background_arab
buyer_1_ethnic_background_mixed buyer_1_ethnic_background_mixed
buyer_1_ethnic_background_white buyer_1_ethnic_background_white
buyer_1_nationality
buyer_1_live_in_property
buyer_2_relationship_to_buyer_1
buyer_2_age buyer_2_age
buyer_2_gender_identity buyer_2_gender_identity
buyer_2_working_situation buyer_2_working_situation

4
spec/models/form_handler_spec.rb

@ -61,14 +61,14 @@ RSpec.describe FormHandler do
it "is able to load a current sales form" do it "is able to load a current sales form" do
form = form_handler.get_form("current_sales") form = form_handler.get_form("current_sales")
expect(form).to be_a(Form) expect(form).to be_a(Form)
expect(form.pages.count).to eq(30) expect(form.pages.count).to eq(31)
expect(form.name).to eq("2022_2023_sales") expect(form.name).to eq("2022_2023_sales")
end end
it "is able to load a previous sales form" do it "is able to load a previous sales form" do
form = form_handler.get_form("previous_sales") form = form_handler.get_form("previous_sales")
expect(form).to be_a(Form) expect(form).to be_a(Form)
expect(form.pages.count).to eq(30) expect(form.pages.count).to eq(31)
expect(form.name).to eq("2021_2022_sales") expect(form.name).to eq("2021_2022_sales")
end end
end end

Loading…
Cancel
Save