Skip to content

Commit a81376c

Browse files
committed
Add multiple branches error summary to page list
Add multiple branch error summary to page list.
1 parent 0a65d8d commit a81376c

4 files changed

Lines changed: 54 additions & 20 deletions

File tree

app/components/page_list_component/error_summary/view.rb

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,28 @@ def error_object(error_name:, condition:, page:)
4848
end
4949

5050
def errors_for_summary
51-
@form.conditions.map { |condition|
52-
condition.validation_errors.map do |error|
53-
error_object(
54-
error_name: error.name,
55-
page: condition.check_page,
56-
condition: condition,
51+
if FeatureService.new(group: @form.group).enabled?(:multiple_branches)
52+
@form.pages.map { |page|
53+
error_message = self.class.multiple_branch_error_message(page)
54+
55+
next if error_message.blank?
56+
57+
OpenStruct.new(
58+
message: error_message,
59+
link: "##{self.class.page_error_id(page)}",
5760
)
58-
end
59-
}.flatten
61+
}.compact
62+
else
63+
@form.conditions.map { |condition|
64+
condition.validation_errors.map do |error|
65+
error_object(
66+
error_name: error.name,
67+
page: condition.check_page,
68+
condition: condition,
69+
)
70+
end
71+
}.flatten
72+
end
6073
end
6174
end
6275
end

app/views/pages/index.html.erb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
<div class="govuk-grid-row">
66
<div class="govuk-grid-column-two-thirds">
7-
<% unless FeatureService.new(group: current_form.group).enabled?(:multiple_branches) %>
8-
<%= render PageListComponent::ErrorSummary::View.new(current_form) %>
9-
<% end %>
7+
<%= render PageListComponent::ErrorSummary::View.new(current_form) %>
108

119
<h1 class="govuk-heading-l govuk-!-margin-bottom-2">
1210
<span class="govuk-caption-l"><%= current_form.name %></span><span class="govuk-visually-hidden"> - </span>

spec/components/page_list_component/error_summary/error_summary_component_preview.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class PageListComponent::ErrorSummary::ErrorSummaryComponentPreview < ViewCompon
22
include FactoryBot::Syntax::Methods
33

44
def default
5-
form = build(:form, id: 1, pages: [])
5+
form = build(:form, :with_group, id: 1, pages: [])
66

77
render(PageListComponent::ErrorSummary::View.new(form))
88
end
@@ -13,7 +13,7 @@ def error_component_without_errors
1313
pages = [(build :page, id: 1, position: 1, question_text: "Enter your name", routing_conditions:),
1414
(build :page, id: 2, position: 2, question_text: "What is your pet's phone number?", routing_conditions: []),
1515
(build :page, id: 3, position: 3, question_text: "How many pets do you own?", routing_conditions: [])]
16-
form = build(:form, id: 0, pages:)
16+
form = build(:form, :with_group, id: 0, pages:)
1717

1818
# We need to build the records rather than create them so that we don't save them to the database when we view the
1919
# preview. However, this means that the associations aren't available so we need to manually set the associations
@@ -35,7 +35,7 @@ def error_component_with_errors
3535
pages = [(build :page, id: 1, position: 1, question_text: "Enter your name", routing_conditions: routing_conditions_page_1),
3636
(build :page, id: 2, position: 2, question_text: "What is your pet's phone number?", routing_conditions: routing_conditions_page_2),
3737
(build :page, id: 3, position: 3, question_text: "How many pets do you own?", routing_conditions: [])]
38-
form = build(:form, id: 0, pages:)
38+
form = build(:form, :with_group, id: 0, pages:)
3939

4040
# We need to build the records rather than create them so that we don't save them to the database when we view the
4141
# preview. However, this means that the associations aren't available so we need to manually set the associations

spec/components/page_list_component/error_summary/view_spec.rb

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
let(:pages) { form.reload.pages }
66
let(:error_summary_component) { described_class.new(form) }
77

8-
describe "rendering component" do
8+
describe "rendering component", feature_multiple_branches: false do
99
context "when there are no pages" do
1010
it "is blank" do
1111
render_inline(error_summary_component)
@@ -138,11 +138,34 @@
138138
end
139139

140140
describe "#errors_for_summary" do
141-
it "returns all of the routing errors for a form with their respective positions and links" do
142-
expect(error_summary_component.errors_for_summary).to eq [
143-
OpenStruct.new(message: I18n.t("errors.page_conditions.answer_value_doesnt_exist", question_number: pages.first.position, route_number: 1), link: "#condition_#{condition_with_answer_value_missing.id}"),
144-
OpenStruct.new(message: I18n.t("errors.page_conditions.goto_page_doesnt_exist", question_number: pages.second.position, route_number: 1), link: "#condition_#{condition_with_goto_page_missing.id}"),
145-
]
141+
context "when the multiple_branches feature is disabled", feature_multiple_branches: false do
142+
it "returns all of the routing errors for a form with their respective positions and links" do
143+
expect(error_summary_component.errors_for_summary).to eq [
144+
OpenStruct.new(message: I18n.t("errors.page_conditions.answer_value_doesnt_exist", question_number: pages.first.position, route_number: 1), link: "#condition_#{condition_with_answer_value_missing.id}"),
145+
OpenStruct.new(message: I18n.t("errors.page_conditions.goto_page_doesnt_exist", question_number: pages.second.position, route_number: 1), link: "#condition_#{condition_with_goto_page_missing.id}"),
146+
]
147+
end
148+
end
149+
150+
context "when the multiple_branches feature is enabled", :feature_multiple_branches do
151+
it "returns an empty array" do
152+
expect(error_summary_component.errors_for_summary).to eq []
153+
end
154+
155+
context "when there is a backward route" do
156+
let(:expected_page_field_id) { "page-#{form.pages.second.id}" }
157+
158+
before do
159+
create :condition, routing_page_id: form.pages.second.id, check_page_id: form.pages.second.id, goto_page_id: form.pages.first.id
160+
form.pages.second.reload
161+
end
162+
163+
it "returns only the backward route error" do
164+
expect(error_summary_component.errors_for_summary).to eq [
165+
OpenStruct.new(message: "A route from question 2 is going to a previous question - edit this route", link: "##{expected_page_field_id}"),
166+
]
167+
end
168+
end
146169
end
147170
end
148171
end

0 commit comments

Comments
 (0)