Skip to content

Commit 3d38d1b

Browse files
committed
test: document how to name elements
1 parent 29d9b47 commit 3d38d1b

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`logRoles calls console.log with output from prettyRoles 1`] = `
4-
"link:
4+
"region:
5+
6+
Name "a region":
7+
<section
8+
aria-label="a region"
9+
data-testid="named-section"
10+
/>
11+
12+
--------------------------------------------------
13+
link:
514
615
Name "link":
716
<a
@@ -147,6 +156,15 @@ Name "Cell 3":
147156
data-testid="a-cell-3"
148157
/>
149158
159+
--------------------------------------------------
160+
form:
161+
162+
Name "a form":
163+
<form
164+
aria-label="a form"
165+
data-testid="named-form"
166+
/>
167+
150168
--------------------------------------------------
151169
radio:
152170

src/__tests__/role-helpers.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ afterEach(() => {
1616

1717
function setup() {
1818
const {getByTestId} = render(`
19-
<section data-testid='a-section'>
19+
<section aria-label="a region" data-testid='named-section'>
2020
<a href="http://whatever.com" data-testid="a-link">link</a>
2121
<a>invalid link</a>
2222
@@ -50,7 +50,7 @@ function setup() {
5050
</tbody>
5151
</table>
5252
53-
<form data-testid='a-form'>
53+
<form aria-label="a form" data-testid='named-form'>
5454
<input type='radio' data-testid='a-radio-1' />
5555
<input type='radio' data-testid='a-radio-2' />
5656
<input type='text' data-testid='a-input-1' />
@@ -62,12 +62,16 @@ function setup() {
6262
<li data-testid='b-list-item-1'>Item 1</li>
6363
<li data-testid='b-list-item-2'>Item 2</li>
6464
</ul>
65+
66+
<form data-testid="a-form" />
67+
<section data-testid="a-section" />
6568
</article>
6669
</section>
6770
`)
6871

6972
return {
7073
unnamedSection: getByTestId('a-section'),
74+
namedSection: getByTestId('named-section'),
7175
anchor: getByTestId('a-link'),
7276
h1: getByTestId('a-h1'),
7377
h2: getByTestId('a-h2'),
@@ -89,6 +93,7 @@ function setup() {
8993
td2: getByTestId('a-cell-2'),
9094
td3: getByTestId('a-cell-3'),
9195
unnamedForm: getByTestId('a-form'),
96+
namedForm: getByTestId('named-form'),
9297
radio: getByTestId('a-radio-1'),
9398
radio2: getByTestId('a-radio-2'),
9499
input: getByTestId('a-input-1'),
@@ -99,7 +104,6 @@ function setup() {
99104

100105
test('getRoles returns expected roles for various dom nodes', () => {
101106
const {
102-
unnamedSection,
103107
anchor,
104108
h1,
105109
h2,
@@ -125,9 +129,11 @@ test('getRoles returns expected roles for various dom nodes', () => {
125129
input,
126130
input2,
127131
textarea,
132+
namedSection,
133+
namedForm,
128134
} = setup()
129135

130-
expect(getRoles(unnamedSection)).toEqual({
136+
expect(getRoles(namedSection)).toEqual({
131137
link: [anchor],
132138
heading: [h1, h2, h3],
133139
navigation: [nav],
@@ -142,20 +148,22 @@ test('getRoles returns expected roles for various dom nodes', () => {
142148
rowgroup: [tbody],
143149
command: [menuItem, menuItem2],
144150
menuitem: [menuItem, menuItem2],
151+
form: [namedForm],
152+
region: [namedSection],
145153
})
146154
})
147155

148156
test('logRoles calls console.log with output from prettyRoles', () => {
149-
const {unnamedSection} = setup()
150-
logRoles(unnamedSection)
157+
const {namedSection} = setup()
158+
logRoles(namedSection)
151159
expect(console.log).toHaveBeenCalledTimes(1)
152160
expect(console.log.mock.calls[0][0]).toMatchSnapshot()
153161
})
154162

155163
test('getImplicitAriaRoles returns expected roles for various dom nodes', () => {
156-
const {unnamedSection, h1, unnamedForm, radio, input} = setup()
164+
const {namedSection, h1, unnamedForm, radio, input} = setup()
157165

158-
expect(getImplicitAriaRoles(unnamedSection)).toEqual([])
166+
expect(getImplicitAriaRoles(namedSection)).toEqual(['region'])
159167
expect(getImplicitAriaRoles(h1)).toEqual(['heading'])
160168
expect(getImplicitAriaRoles(unnamedForm)).toEqual([])
161169
expect(getImplicitAriaRoles(radio)).toEqual(['radio'])

0 commit comments

Comments
 (0)