Skip to content
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

Include roles from shadowDOM in Error message #999

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
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