Skip to content

Commit

Permalink
Reset depenent answers
Browse files Browse the repository at this point in the history
If the user has changed the application route we want to re ask the
state_funded_secondary_school question.
  • Loading branch information
rjlynch committed Jun 20, 2024
1 parent fea3664 commit 80c30bb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 80c30bb

Please sign in to comment.