Skip to content

Commit

Permalink
Allow error summary to contain links to other pages
Browse files Browse the repository at this point in the history
This allows for a less-common pattern where the error summary is used on a page, but to summarise errors in form fields on a separate page.
  • Loading branch information
frankieroberto committed Nov 29, 2023
1 parent 79e7270 commit 577b293
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/summarise_errors_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,21 @@ def all_error_messages_links_are_valid
link = error_message_item.all(:link).first

if link
link_fragment = link[:href].split('#').last
uri = URI(link[:href])

link_target= html.all(id: link_fragment).first || html.all(:field, name: link_fragment).first
# If the link is a fragment (href=#name) link, check that
# it actually links to a field on the page.
if uri.fragment && uri.path == ""
link_target = html.all(:field, id: uri.fragment).first || html.all(:field, name: uri.fragment).first

link_target
else
# Skip - error summary links to another page
true
end

link_target
else
# No links found
false
end
end
Expand Down
34 changes: 34 additions & 0 deletions spec/summarise_errors_matcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,38 @@
}.to fail_with("Error message ‘Enter your full name’ links to #full-name but no input field has this ID or name")
end
end

# This allows for a less-common pattern where the error summary
# is used on a page, but to summarise errors in form fields on a
# separate page.
context "where the errors are linked to a another page" do

let(:html) { '
<title>Error: Your details</title>
<div class="govuk-error-summary" data-module="govuk-error-summary">
<div role="alert">
<h2 class="govuk-error-summary__title">
There is a problem
</h2>
<div class="govuk-error-summary__body">
<ul class="govuk-list govuk-error-summary__list">
<li>
<a href="/name">Enter your full name</a>
</li>
<li>
<a href="/nationality">Select if you are British, Irish or a citizen of a different country</a>
</li>
</ul>
</div>
</div>
</div>'}

it "should pass if all errors listed" do
expect(html).to summarise_errors([
"Enter your full name",
"Select if you are British, Irish or a citizen of a different country"
])
end
end

end

0 comments on commit 577b293

Please sign in to comment.