Skip to content

Fix role assignment for imgs with alt attribute #1236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 27 additions & 27 deletions src/__tests__/__snapshots__/role-helpers.js.snap
Original file line number Diff line number Diff line change
@@ -1,14 +1,40 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`logRoles calls console.log with output from prettyRoles 1`] = `
region:
section:

Name "a region":
<section
aria-label="a region"
data-testid="named-section"
/>

--------------------------------------------------
img:

Name "404":
<img
alt="404"
data-testid="img-alt"
src="http://imgsrc.com"
/>

Name "":
<img
data-testid="img-no-alt"
src="http://imgsrc.com"
/>

--------------------------------------------------
presentation:

Name "":
<img
alt=""
data-testid="img-empty-alt"
src="http://imgsrc.com"
/>

--------------------------------------------------
link:

Expand Down Expand Up @@ -52,32 +78,6 @@ Name "":
data-testid="a-article"
/>

--------------------------------------------------
command:

Name "":
<menuitem
data-testid="a-menuitem-1"
/>

Name "":
<menuitem
data-testid="a-menuitem-2"
/>

--------------------------------------------------
menuitem:

Name "":
<menuitem
data-testid="a-menuitem-1"
/>

Name "":
<menuitem
data-testid="a-menuitem-2"
/>

--------------------------------------------------
list:

Expand Down
26 changes: 25 additions & 1 deletion src/__tests__/role-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ function setup() {
const {getByTestId} = render(`
<header data-testid="a-header">Banner header</header>
<section aria-label="a region" data-testid='named-section'>
<img src="http://imgsrc.com" alt="404" data-testid="img-alt" />
<img src="http://imgsrc.com" alt="" data-testid="img-empty-alt" />
<img src="http://imgsrc.com" data-testid="img-no-alt" />

<a href="http://whatever.com" data-testid="a-link">link</a>
<a>invalid link</a>

Expand Down Expand Up @@ -77,6 +81,9 @@ function setup() {
return {
unnamedSection: getByTestId('a-section'),
namedSection: getByTestId('named-section'),
imgAlt: getByTestId('img-alt'),
imgEmptyAlt: getByTestId('img-empty-alt'),
imgNoAlt: getByTestId('img-no-alt'),
anchor: getByTestId('a-link'),
h1: getByTestId('a-h1'),
h2: getByTestId('a-h2'),
Expand Down Expand Up @@ -142,6 +149,9 @@ test('getRoles returns expected roles for various dom nodes', () => {
dd,
dt,
header,
imgAlt,
imgEmptyAlt,
imgNoAlt,
} = setup()

expect(getRoles(namedSection)).toEqual({
Expand All @@ -163,6 +173,8 @@ test('getRoles returns expected roles for various dom nodes', () => {
region: [namedSection],
term: [dt],
definition: [dd],
img: [imgAlt, imgNoAlt],
presentation: [imgEmptyAlt],
})
expect(getRoles(header)).toEqual({
banner: [header],
Expand All @@ -177,9 +189,21 @@ test('logRoles calls console.log with output from prettyRoles', () => {
})

test('getImplicitAriaRoles returns expected roles for various dom nodes', () => {
const {namedSection, h1, unnamedForm, radio, input} = setup()
const {
namedSection,
imgAlt,
imgEmptyAlt,
imgNoAlt,
h1,
unnamedForm,
radio,
input,
} = setup()

expect(getImplicitAriaRoles(namedSection)).toEqual(['region'])
expect(getImplicitAriaRoles(imgAlt)).toEqual(['img'])
expect(getImplicitAriaRoles(imgEmptyAlt)).toEqual(['presentation'])
expect(getImplicitAriaRoles(imgNoAlt)).toEqual(['img'])
expect(getImplicitAriaRoles(h1)).toEqual(['heading'])
expect(getImplicitAriaRoles(unnamedForm)).toEqual([])
expect(getImplicitAriaRoles(radio)).toEqual(['radio'])
Expand Down
2 changes: 1 addition & 1 deletion src/role-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function buildElementRoleList(elementRolesMap) {
const shouldNotExist = constraints.indexOf('undefined') !== -1
if (shouldNotExist) {
return `:not([${attributeName}])`
} else if (value) {
} else if (value || value === '') {
return `[${attributeName}="${value}"]`
} else {
return `[${attributeName}]`
Expand Down