Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion app/services/routes/build_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,18 @@ def build_routes
def options_for_goto_page(page, selected = nil)
next_page = form.next_page_after(page)

return [] unless next_page
# If there's no next page then it's the last page of the form.
# We only need options for this page if there's an error that needs correcting.
if next_page.nil?
selected_page = if selected && selected != Forms::RouteInput::DEFAULT_VALUE
option_for_select(form.pages.find { |page| page.id == selected })
end

return [
selected_page,
[END_OF_FORM_OPTION.first, Forms::RouteInput::DEFAULT_VALUE],
].compact
end

# Don't include the current page or pages before in the options,
# unless the goto page for the existing condition is before the current page,
Expand Down
2 changes: 1 addition & 1 deletion app/views/routes/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
row.with_value do
capture do %>
<p> <%= page.question_text %> </p>
<% if @routes_input.form.next_page_after(page).present? %>
<% if @routes_input.form.next_page_after(page).present? || routes.any?(&:invalid?) %>
<ul class="govuk-list">
<% routes.each do |route| %>
<li>
Expand Down
19 changes: 17 additions & 2 deletions spec/services/routes/build_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,23 @@
end
end

it "returns an empty array if the page has no next page" do
expect(service.options_for_goto_page(pages.third)).to be_empty
context "when the page has no next page" do
it "returns the end of the form option" do
expected_options = [["End of the form", "default"]]

expect(service.options_for_goto_page(pages.third)).to match_array(expected_options)
end

context "when there is a selected page" do
it "returns the selected page and end of the form options" do
expected_options = [
["1. question 1", pages.first.id],
["End of the form", "default"],
]

expect(service.options_for_goto_page(pages.third, pages.first.id)).to match_array(expected_options)
end
end
end

it "returns a list of all possible goto options" do
Expand Down
39 changes: 38 additions & 1 deletion spec/views/routes/show.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,43 @@ def select_options(select_field)
end
end
end

context "when the routing page is the final page" do
let(:pages) do
[
build_stubbed(:page, id: 101),
build_stubbed(:page, id: 102),
build_stubbed(
:page,
id: 103,
routing_conditions: [
build_stubbed(
:condition,
routing_page_id: 103,
goto_page_id: 101,
answer_value: nil,
),
],
),
]
end

let(:routes_input) do
build(:routes_input, form:).assign_form_values.tap do |routes_input|
routes_input.routes.each { |route| allow(route).to receive(:invalid?).and_return(true) }
end
end

it "shows the selected goto page for the route" do
render_page

expect(rendered).to have_selector('.govuk-select[name="forms_routes_input[routes_attributes][2][goto]"]') do |field|
expect(field).to have_selector("option[selected]") do |option|
expect(option["value"]).to eq "101"
end
end
end
end
end

context "when a page is a select from a list question" do
Expand Down Expand Up @@ -247,7 +284,7 @@ def select_options(select_field)
end
end

context "when there are not enoough pages" do
context "when there are not enough pages" do
let(:pages) do
[
build_stubbed(:page, id: 101),
Expand Down