File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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
223244end
Original file line number Diff line number Diff 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
114145end
You can’t perform that action at this time.
0 commit comments