diff --git a/app/frontend/controllers/conditional_question_controller.js b/app/frontend/controllers/conditional_question_controller.js index 974deeb2e..fb52d07c1 100644 --- a/app/frontend/controllers/conditional_question_controller.js +++ b/app/frontend/controllers/conditional_question_controller.js @@ -8,14 +8,16 @@ export default class extends Controller { displayConditional () { if (this.element.checked) { 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]) => { 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) { 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) } else { diff --git a/app/helpers/question_attribute_helper.rb b/app/helpers/question_attribute_helper.rb index f2f148568..857ce5eb1 100644 --- a/app/helpers/question_attribute_helper.rb +++ b/app/helpers/question_attribute_helper.rb @@ -27,7 +27,7 @@ private { "data-controller": "conditional-question", "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 diff --git a/app/models/form/sales/pages/nationality1.rb b/app/models/form/sales/pages/nationality1.rb new file mode 100644 index 000000000..740aa694f --- /dev/null +++ b/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 diff --git a/app/models/form/sales/questions/nationality1.rb b/app/models/form/sales/questions/nationality1.rb new file mode 100644 index 000000000..737bfc01e --- /dev/null +++ b/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 diff --git a/app/models/form/sales/questions/other_nationality1.rb b/app/models/form/sales/questions/other_nationality1.rb new file mode 100644 index 000000000..a779d4fc8 --- /dev/null +++ b/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 diff --git a/app/models/form/sales/subsections/household_characteristics.rb b/app/models/form/sales/subsections/household_characteristics.rb index 93d084332..e389d5e72 100644 --- a/app/models/form/sales/subsections/household_characteristics.rb +++ b/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::Age1.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::Buyer1EthnicBackgroundBlack.new(nil, nil, self), Form::Sales::Pages::Buyer1EthnicBackgroundAsian.new(nil, nil, self), Form::Sales::Pages::Buyer1EthnicBackgroundArab.new(nil, nil, self), Form::Sales::Pages::Buyer1EthnicBackgroundMixed.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::GenderIdentity2.new(nil, nil, self), Form::Sales::Pages::Buyer2WorkingSituation.new(nil, nil, self), diff --git a/db/migrate/20220927082602_add_national_column.rb b/db/migrate/20220927082602_add_national_column.rb new file mode 100644 index 000000000..d794bc858 --- /dev/null +++ b/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 diff --git a/db/schema.rb b/db/schema.rb index b30200da0..269091a77 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_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 enable_extension "plpgsql" @@ -329,22 +329,24 @@ ActiveRecord::Schema[7.0].define(version: 2022_10_03_150610) do t.string "purchid" t.integer "type" t.integer "ownershipsch" - t.string "othtype" - t.integer "jointmore" t.integer "jointpur" + t.string "othtype" t.integer "beds" - t.integer "companybuy" + t.integer "jointmore" t.integer "age1" t.integer "age1_known" t.string "sex1" + t.integer "national" + t.string "othernational" t.integer "buy1livein" + t.integer "companybuy" t.integer "buylivein" t.integer "builtype" t.integer "proptype" - t.string "relat2" - t.string "otherrelat2" t.integer "age2" t.integer "age2_known" + t.string "relat2" + t.string "otherrelat2" t.integer "ethnic" t.integer "ethnic_group" t.string "sex2" diff --git a/spec/factories/sales_log.rb b/spec/factories/sales_log.rb index 4f9aa07ed..2936d0d06 100644 --- a/spec/factories/sales_log.rb +++ b/spec/factories/sales_log.rb @@ -24,6 +24,7 @@ FactoryBot.define do age1_known { 0 } age1 { 30 } sex1 { "X" } + national { 18 } buy1livein { 1 } relat2 { "P" } proptype { 1 } diff --git a/spec/features/form/conditional_questions_spec.rb b/spec/features/form/conditional_questions_spec.rb index 4f3f44b5a..33d99955e 100644 --- a/spec/features/form/conditional_questions_spec.rb +++ b/spec/features/form/conditional_questions_spec.rb @@ -12,6 +12,14 @@ RSpec.describe "Form Conditional Questions" do managing_organisation: user.organisation, ) 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(: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") expect(page).to have_field("lettings-log-postcode-full-field", with: "NW1 6RT") 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 diff --git a/spec/helpers/question_attribute_helper_spec.rb b/spec/helpers/question_attribute_helper_spec.rb index 96f2f7ef6..2be903535 100644 --- a/spec/helpers/question_attribute_helper_spec.rb +++ b/spec/helpers/question_attribute_helper_spec.rb @@ -40,7 +40,7 @@ RSpec.describe QuestionAttributeHelper do "conditional_for" => { "next_question": ">1", }, - }, nil) + }, form.get_page("rent")) end let(:expected_attribs) do { @@ -48,7 +48,7 @@ RSpec.describe QuestionAttributeHelper do "data-action": "input->numeric-question#calculateFields click->conditional-question#displayConditional", "data-target": "lettings-log-#{question.result_field.to_s.dasherize}-field", "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 diff --git a/spec/models/form/sales/pages/nationality1_spec.rb b/spec/models/form/sales/pages/nationality1_spec.rb new file mode 100644 index 000000000..5d156e041 --- /dev/null +++ b/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 diff --git a/spec/models/form/sales/questions/nationality1_spec.rb b/spec/models/form/sales/questions/nationality1_spec.rb new file mode 100644 index 000000000..819d41ae4 --- /dev/null +++ b/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 diff --git a/spec/models/form/sales/questions/other_nationality1_spec.rb b/spec/models/form/sales/questions/other_nationality1_spec.rb new file mode 100644 index 000000000..b01928efc --- /dev/null +++ b/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 diff --git a/spec/models/form/sales/subsections/household_characteristics_spec.rb b/spec/models/form/sales/subsections/household_characteristics_spec.rb index 364895cc8..21e496efd 100644 --- a/spec/models/form/sales/subsections/household_characteristics_spec.rb +++ b/spec/models/form/sales/subsections/household_characteristics_spec.rb @@ -17,14 +17,15 @@ RSpec.describe Form::Sales::Subsections::HouseholdCharacteristics, type: :model buyer_interview buyer_1_age buyer_1_gender_identity - buyer_1_live_in_property - buyer_2_relationship_to_buyer_1 buyer_1_ethnic_group buyer_1_ethnic_background_black buyer_1_ethnic_background_asian buyer_1_ethnic_background_arab buyer_1_ethnic_background_mixed buyer_1_ethnic_background_white + buyer_1_nationality + buyer_1_live_in_property + buyer_2_relationship_to_buyer_1 buyer_2_age buyer_2_gender_identity buyer_2_working_situation diff --git a/spec/models/form_handler_spec.rb b/spec/models/form_handler_spec.rb index 831502602..32b6fbc7d 100644 --- a/spec/models/form_handler_spec.rb +++ b/spec/models/form_handler_spec.rb @@ -61,14 +61,14 @@ RSpec.describe FormHandler do it "is able to load a current sales form" do form = form_handler.get_form("current_sales") 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") end it "is able to load a previous sales form" do form = form_handler.get_form("previous_sales") 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") end end