Skip to content

Commit 5bf63e8

Browse files
committed
Fix role assignment for imgs with alt attribute
1 parent d09b3c2 commit 5bf63e8

File tree

3 files changed

+45
-29
lines changed

3 files changed

+45
-29
lines changed

src/__tests__/__snapshots__/role-helpers.js.snap

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,40 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`logRoles calls console.log with output from prettyRoles 1`] = `
4-
region:
4+
section:
55
66
Name "a region":
77
<section
88
aria-label="a region"
99
data-testid="named-section"
1010
/>
1111
12+
--------------------------------------------------
13+
img:
14+
15+
Name "404":
16+
<img
17+
alt="404"
18+
data-testid="img-alt"
19+
src="http://imgsrc.com"
20+
/>
21+
22+
Name "":
23+
<img
24+
data-testid="img-no-alt"
25+
src="http://imgsrc.com"
26+
/>
27+
28+
--------------------------------------------------
29+
presentation:
30+
31+
Name "":
32+
<img
33+
alt=""
34+
data-testid="img-empty-alt"
35+
src="http://imgsrc.com"
36+
/>
37+
1238
--------------------------------------------------
1339
link:
1440
@@ -52,32 +78,6 @@ Name "":
5278
data-testid="a-article"
5379
/>
5480
55-
--------------------------------------------------
56-
command:
57-
58-
Name "":
59-
<menuitem
60-
data-testid="a-menuitem-1"
61-
/>
62-
63-
Name "":
64-
<menuitem
65-
data-testid="a-menuitem-2"
66-
/>
67-
68-
--------------------------------------------------
69-
menuitem:
70-
71-
Name "":
72-
<menuitem
73-
data-testid="a-menuitem-1"
74-
/>
75-
76-
Name "":
77-
<menuitem
78-
data-testid="a-menuitem-2"
79-
/>
80-
8181
--------------------------------------------------
8282
list:
8383

src/__tests__/role-helpers.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ function setup() {
1818
const {getByTestId} = render(`
1919
<header data-testid="a-header">Banner header</header>
2020
<section aria-label="a region" data-testid='named-section'>
21+
<img src="http://imgsrc.com" alt="404" data-testid="img-alt" />
22+
<img src="http://imgsrc.com" alt="" data-testid="img-empty-alt" />
23+
<img src="http://imgsrc.com" data-testid="img-no-alt" />
24+
2125
<a href="http://whatever.com" data-testid="a-link">link</a>
2226
<a>invalid link</a>
2327
@@ -77,6 +81,9 @@ function setup() {
7781
return {
7882
unnamedSection: getByTestId('a-section'),
7983
namedSection: getByTestId('named-section'),
84+
imgAlt: getByTestId('img-alt'),
85+
imgEmptyAlt: getByTestId('img-empty-alt'),
86+
imgNoAlt: getByTestId('img-no-alt'),
8087
anchor: getByTestId('a-link'),
8188
h1: getByTestId('a-h1'),
8289
h2: getByTestId('a-h2'),
@@ -142,6 +149,9 @@ test('getRoles returns expected roles for various dom nodes', () => {
142149
dd,
143150
dt,
144151
header,
152+
imgAlt,
153+
imgEmptyAlt,
154+
imgNoAlt,
145155
} = setup()
146156

147157
expect(getRoles(namedSection)).toEqual({
@@ -163,6 +173,8 @@ test('getRoles returns expected roles for various dom nodes', () => {
163173
region: [namedSection],
164174
term: [dt],
165175
definition: [dd],
176+
img: [imgAlt, imgNoAlt],
177+
presentation: [imgEmptyAlt],
166178
})
167179
expect(getRoles(header)).toEqual({
168180
banner: [header],
@@ -177,9 +189,13 @@ test('logRoles calls console.log with output from prettyRoles', () => {
177189
})
178190

179191
test('getImplicitAriaRoles returns expected roles for various dom nodes', () => {
180-
const {namedSection, h1, unnamedForm, radio, input} = setup()
192+
const {namedSection, imgAlt, imgEmptyAlt, imgNoAlt,
193+
h1, unnamedForm, radio, input} = setup()
181194

182195
expect(getImplicitAriaRoles(namedSection)).toEqual(['region'])
196+
expect(getImplicitAriaRoles(imgAlt)).toEqual(['img'])
197+
expect(getImplicitAriaRoles(imgEmptyAlt)).toEqual(['presentation'])
198+
expect(getImplicitAriaRoles(imgNoAlt)).toEqual(['img'])
183199
expect(getImplicitAriaRoles(h1)).toEqual(['heading'])
184200
expect(getImplicitAriaRoles(unnamedForm)).toEqual([])
185201
expect(getImplicitAriaRoles(radio)).toEqual(['radio'])

src/role-helpers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function buildElementRoleList(elementRolesMap) {
8484
const shouldNotExist = constraints.indexOf('undefined') !== -1
8585
if (shouldNotExist) {
8686
return `:not([${attributeName}])`
87-
} else if (value) {
87+
} else if (value || value === "") {
8888
return `[${attributeName}="${value}"]`
8989
} else {
9090
return `[${attributeName}]`

0 commit comments

Comments
 (0)