Skip to content

Commit 95160e3

Browse files
authored
Merge pull request #2784 from govuk-forms/ldeb-multiple-branches-limit-long-lists
Limit branching to only lists with 10 or fewer options (for now)
2 parents 3e3650d + 9a34019 commit 95160e3

6 files changed

Lines changed: 107 additions & 1 deletion

File tree

app/input_objects/forms/routes_input.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
class Forms::RoutesInput < BaseInput
22
attr_accessor :form, :routes
33

4+
def self.too_many_selection_options?(page)
5+
page.answer_settings["selection_options"].length > 10
6+
end
7+
48
def initialize(attributes = {})
59
@form = attributes.delete(:form)
610
super

app/services/routes/build_service.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ def build_routes
1717
end
1818

1919
form.pages.flat_map do |page|
20-
if page.answer_type == "selection" && page.answer_settings.only_one_option == "true"
20+
if page.answer_type == "selection" &&
21+
page.answer_settings.only_one_option == "true" &&
22+
!Forms::RoutesInput.too_many_selection_options?(page)
2123
build_routes_for_selection_page(page, conditions_by_key)
2224
else
2325
build_route_for_generic_page(page, conditions_by_key)

app/views/routes/show.html.erb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
capture do %>
2424
<p> <%= page.question_text %> </p>
2525
<% if @routes_input.form.next_page_after(page).present? %>
26+
<% if page.answer_type == "selection" &&
27+
page.answer_settings.only_one_option == "true" &&
28+
@routes_input.class.too_many_selection_options?(page) %>
29+
<%= t(".too_many_options_html") %>
30+
<% end %>
2631
<ul class="govuk-list">
2732
<% routes.each do |route| %>
2833
<li>

config/locales/en.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,6 +1915,12 @@ en:
19151915
total_number_of_users: Total Number of Users
19161916
user_count: User count
19171917
title: Number of users per organisation
1918+
routes:
1919+
show:
1920+
too_many_options_html: |
1921+
<p>This question has a list with more than 10 options.</p>
1922+
1923+
<p>You cannot add routes from answers if the question has more than 10 options.</p>
19181924
routing_page:
19191925
body_html: |
19201926
<p> You can add a route to a question so if someone selects one specific answer, they’ll be skipped to: </p>

spec/services/routes/build_service_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,22 @@
107107
expect(route_for_no.label).to eq({ text: "Option 2: No" })
108108
end
109109

110+
context "when the selection page has more than 10 options" do
111+
let(:selection_options) do
112+
(1..11).map do |i|
113+
{ name: "Option #{i}", value: "Option #{i}" }
114+
end
115+
end
116+
117+
it "builds a single route input for the selection page" do
118+
routes = service.build_routes
119+
120+
expect(routes.length).to eq(3)
121+
expect(routes).to all be_a(Forms::RouteInput)
122+
expect(routes.filter { it.page_id == pages.first.id }.length).to eq(1)
123+
end
124+
end
125+
110126
context "when the selection page is optional" do
111127
let(:pages) do
112128
[

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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,5 +141,78 @@ def select_options(select_field)
141141
end
142142
end
143143
end
144+
145+
context "when a page is a select from a list question" do
146+
let(:pages) do
147+
[
148+
build_stubbed(:page, :with_selection_settings, id: 101, selection_options:),
149+
build_stubbed(:page, id: 102),
150+
build_stubbed(:page, id: 103),
151+
]
152+
end
153+
154+
let(:selection_options) do
155+
[{ name: "Yes", value: "Yes" }, { name: "No", value: "No" }]
156+
end
157+
158+
it "has inputs for each answer option" do
159+
render_page
160+
161+
expect(rendered).to have_selector(".govuk-summary-list") do |summary_list|
162+
rows = summary_list.find_all(".govuk-summary-list__row")
163+
164+
expect(rows[0]).to have_selector('.govuk-select[name="forms_routes_input[routes_attributes][0][goto]"]')
165+
expect(rows[0]).to have_selector('input[name="forms_routes_input[routes_attributes][0][page_id]"][value="101"]', visible: :hidden)
166+
expect(rows[0]).to have_selector('input[name="forms_routes_input[routes_attributes][0][answer_value]"][value="Yes"]', visible: :hidden)
167+
168+
expect(rows[0]).to have_selector('.govuk-select[name="forms_routes_input[routes_attributes][1][goto]"]')
169+
expect(rows[0]).to have_selector('input[name="forms_routes_input[routes_attributes][1][page_id]"][value="101"]', visible: :hidden)
170+
expect(rows[0]).to have_selector('input[name="forms_routes_input[routes_attributes][1][answer_value]"][value="No"]', visible: :hidden)
171+
172+
expect(rows[1]).to have_selector('.govuk-select[name="forms_routes_input[routes_attributes][2][goto]"]')
173+
expect(rows[1]).to have_selector('input[name="forms_routes_input[routes_attributes][2][page_id]"][value="102"]', visible: :hidden)
174+
175+
expect(rows[2]).not_to have_selector(".govuk-select")
176+
end
177+
end
178+
179+
context "with more than 10 options" do
180+
let(:selection_options) do
181+
(1..11).map do |i|
182+
{ name: "Option #{i}", value: "Option #{i}" }
183+
end
184+
end
185+
186+
it "has one route input for that question" do
187+
render_page
188+
189+
expect(rendered).to have_selector(".govuk-summary-list") do |summary_list|
190+
rows = summary_list.find_all(".govuk-summary-list__row")
191+
192+
expect(rows[0]).to have_selector(".govuk-select", count: 1)
193+
expect(rows[1]).to have_selector(".govuk-select", count: 1)
194+
expect(rows[2]).not_to have_selector(".govuk-select")
195+
end
196+
end
197+
198+
it "has content explaining that routes cannot be added" do
199+
render_page
200+
201+
expected_content = <<~TEXT
202+
This question has a list with more than 10 options.
203+
204+
You cannot add routes from answers if the question has more than 10 options.
205+
TEXT
206+
207+
expect(rendered).to have_selector(".govuk-summary-list") do |summary_list|
208+
rows = summary_list.find_all(".govuk-summary-list__row")
209+
210+
expect(rows[0]).to have_text(expected_content)
211+
expect(rows[1]).not_to have_text(expected_content)
212+
expect(rows[2]).not_to have_text(expected_content)
213+
end
214+
end
215+
end
216+
end
144217
end
145218
end

0 commit comments

Comments
 (0)