Skip to content

Commit 73cfab8

Browse files
committed
Only show aria-current attribute for current and active links
Fixes #621
1 parent 03365a4 commit 73cfab8

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

app/components/govuk_component/service_navigation_component/navigation_item_component.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,13 @@ def default_attributes
7676
end
7777

7878
def aria_current
79-
current = (current?) ? 'page' : true
79+
{ aria: { current: aria_current_value } }
80+
end
81+
82+
def aria_current_value
83+
return 'page' if current?
84+
return true if active?
8085

81-
{ aria: { current: } }
86+
nil
8287
end
8388
end

spec/components/govuk_component/service_navigation_component_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@
136136
specify %(the current link has aria-current='page') do
137137
expect(rendered_content).to have_tag('a', text: 'Item two', with: { href: '/item-two', 'aria-current' => 'page' })
138138
end
139+
140+
specify %(only current links have an aria-current attribute) do
141+
expect(rendered_content).to have_tag('a', with: { 'aria-current' => 'page' }, count: 1)
142+
expect(rendered_content).not_to have_tag('a', with: { 'aria-current' => true })
143+
end
139144
end
140145

141146
describe 'matching the current page' do
@@ -155,6 +160,11 @@
155160
end
156161
end
157162
end
163+
164+
specify %(only current links have an aria-current attribute) do
165+
expect(rendered_content).to have_tag('a', with: { 'aria-current' => 'page' }, count: 1)
166+
expect(rendered_content).not_to have_tag('a', with: { 'aria-current' => true })
167+
end
158168
end
159169
end
160170

@@ -179,6 +189,11 @@
179189
specify %(the active link has aria-current='true') do
180190
expect(rendered_content).to have_tag('a', text: 'Item two', with: { href: '/item-two', 'aria-current' => 'true' })
181191
end
192+
193+
specify %(only active links have an aria-current attribute) do
194+
expect(rendered_content).to have_tag('a', with: { 'aria-current' => true }, count: 1)
195+
expect(rendered_content).not_to have_tag('a', with: { 'aria-current' => 'page' })
196+
end
182197
end
183198

184199
context 'when active_when is set with a string' do
@@ -201,6 +216,11 @@
201216
specify %(the active link has aria-current='true') do
202217
expect(rendered_content).to have_tag('a', text: 'Admin', with: { href: '/admin', 'aria-current' => 'true' })
203218
end
219+
220+
specify %(only active links have an aria-current attribute) do
221+
expect(rendered_content).to have_tag('a', with: { 'aria-current' => true }, count: 1)
222+
expect(rendered_content).not_to have_tag('a', with: { 'aria-current' => 'page' })
223+
end
204224
end
205225

206226
context 'when active_when is set with an array' do
@@ -223,6 +243,11 @@
223243
specify %(the active link has aria-current='true') do
224244
expect(rendered_content).to have_tag('a', text: 'Sales', with: { href: '/sales', 'aria-current' => 'true' })
225245
end
246+
247+
specify %(only active links have an aria-current attribute) do
248+
expect(rendered_content).to have_tag('a', with: { 'aria-current' => true }, count: 1)
249+
expect(rendered_content).not_to have_tag('a', with: { 'aria-current' => 'page' })
250+
end
226251
end
227252

228253
context 'when active_when is set with a regular expression' do
@@ -245,6 +270,11 @@
245270
specify %(the active link has aria-current='true') do
246271
expect(rendered_content).to have_tag('a', text: 'Finance', with: { href: '/finance', 'aria-current' => 'true' })
247272
end
273+
274+
specify %(only active links have an aria-current attribute) do
275+
expect(rendered_content).to have_tag('a', with: { 'aria-current' => true }, count: 1)
276+
expect(rendered_content).not_to have_tag('a', with: { 'aria-current' => 'page' })
277+
end
248278
end
249279
end
250280

0 commit comments

Comments
 (0)