diff --git a/app/forms/journeys/get_a_teacher_relocation_payment/application_route_form.rb b/app/forms/journeys/get_a_teacher_relocation_payment/application_route_form.rb index b4b139d2c4..b2b8813792 100644 --- a/app/forms/journeys/get_a_teacher_relocation_payment/application_route_form.rb +++ b/app/forms/journeys/get_a_teacher_relocation_payment/application_route_form.rb @@ -15,12 +15,24 @@ def available_options def save return false unless valid? + if application_route_changed? + journey_session.answers.assign_attributes( + state_funded_secondary_school: nil + ) + end + journey_session.answers.assign_attributes( application_route: application_route ) journey_session.save! end + + private + + def application_route_changed? + journey_session.answers.application_route != application_route + end end end end diff --git a/app/views/get_a_teacher_relocation_payment/claims/trainee_details.html.erb b/app/views/get_a_teacher_relocation_payment/claims/trainee_details.html.erb index 0969bad3ac..c0115b68e1 100644 --- a/app/views/get_a_teacher_relocation_payment/claims/trainee_details.html.erb +++ b/app/views/get_a_teacher_relocation_payment/claims/trainee_details.html.erb @@ -23,7 +23,7 @@ -> (option) { option }, -> (option) { t("get_a_teacher_relocation_payment.forms.trainee_details.answers.#{option}.answer") }, legend: { text: t("get_a_teacher_relocation_payment.forms.trainee_details.question") }, - hint: { html: t("get_a_teacher_relocation_payment.forms.trainee_details.hint_html") } + hint: { text: t("get_a_teacher_relocation_payment.forms.trainee_details.hint_html") } ) %> <%= f.govuk_submit %> diff --git a/spec/forms/journeys/get_a_teacher_relocation_payment/application_route_form_spec.rb b/spec/forms/journeys/get_a_teacher_relocation_payment/application_route_form_spec.rb index b6fbabed07..9c10f57f1a 100644 --- a/spec/forms/journeys/get_a_teacher_relocation_payment/application_route_form_spec.rb +++ b/spec/forms/journeys/get_a_teacher_relocation_payment/application_route_form_spec.rb @@ -37,5 +37,37 @@ change { journey_session.reload.answers.application_route }.to("teacher") ) end + + describe "reseting dependent answers" do + before do + journey_session.answers.assign_attributes( + application_route: existing_option, + state_funded_secondary_school: true + ) + journey_session.save! + end + + context "when the value has changed" do + let(:existing_option) { "salaried_trainee" } + + it "resets dependent answers" do + expect { expect(form.save).to be(true) }.to( + change { journey_session.reload.answers.state_funded_secondary_school } + .from(true) + .to(nil) + ) + end + end + + context "when the value hasn't changed" do + let(:existing_option) { option } + + it "doesn't reset dependent answers if the value hasn't changed" do + expect { expect(form.save).to be(true) }.not_to( + change { journey_session.reload.answers.state_funded_secondary_school } + ) + end + end + end end end