Skip to content
Open
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
8 changes: 8 additions & 0 deletions src/__tests__/role-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,14 @@ test('getImplicitAriaRoles returns expected roles for various dom nodes', () =>
expect(getImplicitAriaRoles(input)).toEqual(['textbox'])
})

test('getImplicitAriaRoles returns search role for a <search> element', () => {
const {container} = render('<search>content</search>')

expect(getImplicitAriaRoles(container.querySelector('search'))).toEqual([
'search',
])
})

test.each([
['<div />', false],
['<div aria-hidden="false" />', false],
Expand Down
7 changes: 7 additions & 0 deletions src/__tests__/role.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ test('when hidden: true logs available roles when it fails', () => {
`)
})

test('can be found by its implicit role when using a <search> element', () => {
const {getByRole, container} = render(
'<search><input type="search" /></search>',
)
expect(getByRole('search')).toBe(container.querySelector('search'))
})

test('logs error when there are no accessible roles', () => {
const {getByRole} = render('<div />')
expect(() => getByRole('article')).toThrowErrorMatchingInlineSnapshot(`
Expand Down
9 changes: 9 additions & 0 deletions src/queries/role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,15 @@ function makeRoleSelector(role: ByRoleMatcher) {
Array.from(roleRelations).map(({name}) => name),
)

// aria-query 5.3.0 does not map the `<search>` element to its implicit
// `search` role, so it is missing from `roleElements`. Add it here to keep
// the candidate selector in sync with `getImplicitAriaRoles`. This can be
// removed once aria-query maps `<search>`.
// https://github.com/testing-library/dom-testing-library/issues/1359
if (role === 'search') {
implicitRoleSelectors.add('search')
}

// Current transpilation config sometimes assumes `...` is always applied to arrays.
// `...` is equivalent to `Array.prototype.concat` for arrays.
// If you replace this code with `[explicitRoleSelector, ...implicitRoleSelectors]`, make sure every transpilation target retains the `...` in favor of `Array.prototype.concat`.
Expand Down
8 changes: 8 additions & 0 deletions src/role-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ function getImplicitAriaRoles(currentNode) {
}
}

// The `<search>` element has an implicit `search` role per the HTML-ARIA
// spec, but aria-query 5.3.0 does not include it in `elementRoles`, so it
// never matches above. This can be removed once aria-query maps `<search>`.
// https://github.com/testing-library/dom-testing-library/issues/1359
if (currentNode.tagName === 'SEARCH') {
return ['search']
}

return []
}

Expand Down
Loading