Skip to content

Commit c509fd3

Browse files
committed
fix: Various implicit roles in ByRole
1 parent 3dfa297 commit c509fd3

File tree

5 files changed

+28
-36
lines changed

5 files changed

+28
-36
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
"@babel/runtime": "^7.6.2",
4444
"@sheerun/mutationobserver-shim": "^0.3.2",
4545
"@types/testing-library__dom": "^6.0.0",
46-
"aria-query": "3.0.0",
46+
"aria-query": "^4.0.1",
4747
"pretty-format": "^24.9.0",
4848
"wait-for-expect": "^3.0.0"
4949
},

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

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

33
exports[`logRoles calls console.log with output from prettyRoles 1`] = `
4-
"region:
5-
6-
<section
7-
data-testid="a-section"
8-
/>
9-
10-
--------------------------------------------------
11-
link:
4+
"link:
125
136
<a
147
data-testid="a-link"
@@ -132,13 +125,6 @@ cell:
132125
data-testid="a-cell-3"
133126
/>
134127
135-
--------------------------------------------------
136-
form:
137-
138-
<form
139-
data-testid="a-form"
140-
/>
141-
142128
--------------------------------------------------
143129
radio:
144130

src/__tests__/element-queries.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -427,23 +427,25 @@ test('queryAllByRole returns semantic html elements', () => {
427427
expect(queryAllByRole(/columnheader/i)).toHaveLength(1)
428428
expect(queryAllByRole(/rowheader/i)).toHaveLength(1)
429429
expect(queryAllByRole(/grid/i)).toHaveLength(1)
430-
expect(queryAllByRole(/form/i)).toHaveLength(1)
430+
expect(queryAllByRole(/form/i)).toHaveLength(0)
431431
expect(queryAllByRole(/button/i)).toHaveLength(1)
432432
expect(queryAllByRole(/heading/i)).toHaveLength(6)
433433
expect(queryAllByRole('list')).toHaveLength(2)
434434
expect(queryAllByRole(/listitem/i)).toHaveLength(3)
435-
expect(queryAllByRole(/textbox/i)).toHaveLength(2)
435+
// TODO: fix upstream in `aria-query`
436+
expect(queryAllByRole(/textbox/i)).toHaveLength(1)
436437
expect(queryAllByRole(/checkbox/i)).toHaveLength(1)
437438
expect(queryAllByRole(/radio/i)).toHaveLength(1)
438439
expect(queryAllByRole('row')).toHaveLength(3)
439440
expect(queryAllByRole(/rowgroup/i)).toHaveLength(2)
440-
expect(queryAllByRole(/(table)|(textbox)/i)).toHaveLength(3)
441+
// TODO: fix upstream in `aria-query`
442+
expect(queryAllByRole(/(table)|(textbox)/i)).toHaveLength(2)
441443
expect(queryAllByRole(/img/i)).toHaveLength(1)
442444
expect(queryAllByRole('meter')).toHaveLength(1)
443445
expect(queryAllByRole('progressbar')).toHaveLength(0)
444446
expect(queryAllByRole('progressbar', {queryFallbacks: true})).toHaveLength(1)
445-
expect(queryAllByRole('combobox')).toHaveLength(2)
446-
expect(queryAllByRole('listbox')).toHaveLength(2)
447+
expect(queryAllByRole('combobox')).toHaveLength(1)
448+
expect(queryAllByRole('listbox')).toHaveLength(1)
447449
})
448450

449451
test('getAll* matchers return an array', () => {

src/__tests__/role-helpers.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ function setup() {
6767
`)
6868

6969
return {
70-
section: getByTestId('a-section'),
70+
unnamedSection: getByTestId('a-section'),
7171
anchor: getByTestId('a-link'),
7272
h1: getByTestId('a-h1'),
7373
h2: getByTestId('a-h2'),
@@ -88,7 +88,7 @@ function setup() {
8888
td1: getByTestId('a-cell-1'),
8989
td2: getByTestId('a-cell-2'),
9090
td3: getByTestId('a-cell-3'),
91-
form: getByTestId('a-form'),
91+
unnamedForm: getByTestId('a-form'),
9292
radio: getByTestId('a-radio-1'),
9393
radio2: getByTestId('a-radio-2'),
9494
input: getByTestId('a-input-1'),
@@ -99,7 +99,7 @@ function setup() {
9999

100100
test('getRoles returns expected roles for various dom nodes', () => {
101101
const {
102-
section,
102+
unnamedSection,
103103
anchor,
104104
h1,
105105
h2,
@@ -120,17 +120,15 @@ test('getRoles returns expected roles for various dom nodes', () => {
120120
td1,
121121
td2,
122122
td3,
123-
form,
124123
radio,
125124
radio2,
126125
input,
127126
input2,
128127
textarea,
129128
} = setup()
130129

131-
expect(getRoles(section)).toEqual({
130+
expect(getRoles(unnamedSection)).toEqual({
132131
link: [anchor],
133-
region: [section],
134132
heading: [h1, h2, h3],
135133
navigation: [nav],
136134
radio: [radio, radio2],
@@ -140,7 +138,6 @@ test('getRoles returns expected roles for various dom nodes', () => {
140138
table: [table],
141139
row: [tr],
142140
cell: [td1, td2, td3],
143-
form: [form],
144141
textbox: [input, input2, textarea],
145142
rowgroup: [tbody],
146143
command: [menuItem, menuItem2],
@@ -149,18 +146,18 @@ test('getRoles returns expected roles for various dom nodes', () => {
149146
})
150147

151148
test('logRoles calls console.log with output from prettyRoles', () => {
152-
const {section} = setup()
153-
logRoles(section)
149+
const {unnamedSection} = setup()
150+
logRoles(unnamedSection)
154151
expect(console.log).toHaveBeenCalledTimes(1)
155152
expect(console.log.mock.calls[0][0]).toMatchSnapshot()
156153
})
157154

158155
test('getImplicitAriaRoles returns expected roles for various dom nodes', () => {
159-
const {section, h1, form, radio, input} = setup()
156+
const {unnamedSection, h1, unnamedForm, radio, input} = setup()
160157

161-
expect(getImplicitAriaRoles(section)).toEqual(['region'])
158+
expect(getImplicitAriaRoles(unnamedSection)).toEqual([])
162159
expect(getImplicitAriaRoles(h1)).toEqual(['heading'])
163-
expect(getImplicitAriaRoles(form)).toEqual(['form'])
160+
expect(getImplicitAriaRoles(unnamedForm)).toEqual([])
164161
expect(getImplicitAriaRoles(radio)).toEqual(['radio'])
165162
expect(getImplicitAriaRoles(input)).toEqual(['textbox'])
166163
})

src/role-helpers.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,16 @@ function getImplicitAriaRoles(currentNode) {
7575
function buildElementRoleList(elementRolesMap) {
7676
function makeElementSelector({name, attributes = []}) {
7777
return `${name}${attributes
78-
.map(({name: attributeName, value}) =>
79-
value ? `[${attributeName}=${value}]` : `[${attributeName}]`,
80-
)
78+
.map(({name: attributeName, value, constraints = []}) => {
79+
const shouldNotExist = constraints.indexOf('undefined') !== -1
80+
if (shouldNotExist) {
81+
return `:not([${attributeName}])`
82+
} else if (value) {
83+
return `[${attributeName}="${value}"]`
84+
} else {
85+
return `[${attributeName}]`
86+
}
87+
})
8188
.join('')}`
8289
}
8390

0 commit comments

Comments
 (0)