Skip to content

Commit a7d5aae

Browse files
committed
Don't allow goto page to be before routing page in show routes page
1 parent 3736a13 commit a7d5aae

3 files changed

Lines changed: 72 additions & 9 deletions

File tree

app/services/routes/build_service.rb

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,26 @@ def options_for_goto_page(page, selected = nil)
3030

3131
return [] unless next_page
3232

33-
# Don't include the current page in the options
34-
options_without_page = all_goto_options.reject { |_, value| value == page.id }
35-
36-
# Replace the next page with the default option
37-
options_without_page.map do |name, value|
38-
if value == next_page.id
39-
["Go to question #{page.position.next}", Forms::RouteInput::DEFAULT_VALUE]
33+
# Don't include the current page or pages before in the options,
34+
# unless the goto page for the existing condition is before the current page,
35+
# in which case do include that one. Also, change the option for the next page
36+
# to a different default option.
37+
next_page_id = next_page.id
38+
drop = true
39+
40+
all_goto_options.filter_map do |option|
41+
_, value = option
42+
43+
if drop
44+
if selected && value == selected
45+
option
46+
elsif value == next_page_id
47+
drop = false
48+
["Go to question #{page.position.next}", Forms::RouteInput::DEFAULT_VALUE]
49+
end
50+
# return nil
4051
else
41-
[name, value]
52+
option
4253
end
4354
end
4455
end

spec/services/routes/build_service_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,5 +219,26 @@
219219

220220
expect(page_ids).not_to include(pages.first.id)
221221
end
222+
223+
it "does not include pages before the current page in the options" do
224+
options = service.options_for_goto_page(pages.second)
225+
226+
expect(options).to eq [
227+
["Go to question 3", Forms::RouteInput::DEFAULT_VALUE],
228+
["End of the form", "end_of_form"],
229+
]
230+
end
231+
232+
context "when there is a selected goto page and the goto page is before the current page" do
233+
it "includes the goto page in the options" do
234+
options = service.options_for_goto_page(pages.second, pages.first.id)
235+
236+
expect(options).to eq [
237+
["1. #{pages.first.question_text}", pages.first.id],
238+
["Go to question 3", Forms::RouteInput::DEFAULT_VALUE],
239+
["End of the form", "end_of_form"],
240+
]
241+
end
242+
end
222243
end
223244
end

spec/views/routes/show.html.erb_spec.rb

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def select_options(select_field)
8888
end
8989

9090
expect(rendered).to have_selector('.govuk-select[name="forms_routes_input[routes_attributes][1][goto]"]') do |field|
91-
expect(select_options(field)).to end_with [
91+
expect(select_options(field)).to eq [
9292
["default", "Go to question 3"],
9393
["end_of_form", "End of the form"],
9494
]
@@ -110,5 +110,36 @@ def select_options(select_field)
110110
end
111111
end
112112
end
113+
114+
context "when the route goes to a page before the routing page" do
115+
let(:pages) do
116+
[
117+
build_stubbed(:page, id: 101),
118+
build_stubbed(
119+
:page,
120+
id: 102,
121+
routing_conditions: [
122+
build_stubbed(
123+
:condition,
124+
routing_page_id: 102,
125+
goto_page_id: 101,
126+
answer_value: nil,
127+
),
128+
],
129+
),
130+
build_stubbed(:page, id: 103),
131+
]
132+
end
133+
134+
it "shows the selected goto page for the route" do
135+
render_page
136+
137+
expect(rendered).to have_selector('.govuk-select[name="forms_routes_input[routes_attributes][1][goto]"]') do |field|
138+
expect(field).to have_selector("option[selected]") do |option|
139+
expect(option["value"]).to eq "101"
140+
end
141+
end
142+
end
143+
end
113144
end
114145
end

0 commit comments

Comments
 (0)