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
41 changes: 41 additions & 0 deletions src/__tests__/role.js
Original file line number Diff line number Diff line change
Expand Up @@ -572,3 +572,44 @@ test('should find the input using type property instead of attribute', () => {
const {getByRole} = render('<input type="124">')
expect(getByRole('textbox')).not.toBeNull()
})

describe('Web Component Custom Elements', () => {
beforeAll(() => {
customElements.define(
'custom-button',
class CustomButton extends HTMLElement {
constructor() {
super()

this.attachShadow({mode: 'open'})

const button = document.createElement('button')
button.innerHTML = 'Button text'

this.shadowRoot.append(button)
}
},
)
})

test('should find accessible roles in elements that contain a shadowRoot', () => {
const {getByRole} = render(`<custom-button />`)
expect(() => getByRole('article')).toThrowErrorMatchingInlineSnapshot(`
"Unable to find an accessible element with the role "article"

Here are the accessible roles:

button:

Name "Button text":
<button />

--------------------------------------------------

Ignored nodes: comments, <script />, <style />
<div>
<custom-button />
</div>"
`)
})
})
7 changes: 3 additions & 4 deletions src/role-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,9 @@ function getRoles(container, {hidden = false} = {}) {
function flattenDOM(node) {
return [
node,
...Array.from(node.children).reduce(
(acc, child) => [...acc, ...flattenDOM(child)],
[],
),
...Array.from(
node.shadowRoot ? node.shadowRoot.children : node.children,
).reduce((acc, child) => [...acc, ...flattenDOM(child)], []),
]
}

Expand Down