Skip to content

Commit bb3c145

Browse files
eps1lonKent C. Dodds
authored and
Kent C. Dodds
committed
fix(byRole): Don't assume window context (#378)
* fix(shouldExcludeFromA11yTree): Don't assume window contextg * Add test
1 parent 13543a9 commit bb3c145

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/__node_tests__/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,23 @@ test('works without a browser context on a dom node (JSDOM Fragment)', () => {
7676
/>
7777
`)
7878
})
79+
80+
test('byRole works without a global DOM', () => {
81+
const {
82+
window: {
83+
document: {body: container},
84+
},
85+
} = new JSDOM(`
86+
<html>
87+
<body>
88+
<button>Say "Hello, Dave!"</button>
89+
</body>
90+
</html>
91+
`)
92+
93+
expect(dtl.getByRole(container, 'button')).toMatchInlineSnapshot(`
94+
<button>
95+
Say "Hello, Dave!"
96+
</button>
97+
`)
98+
})

src/role-helpers.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const elementRoleList = buildElementRoleList(elementRoles)
1515
* @returns {boolean} true if excluded, otherwise false
1616
*/
1717
function shouldExcludeFromA11yTree(element) {
18+
const window = element.ownerDocument.defaultView
1819
const computedStyle = window.getComputedStyle(element)
1920
// since visibility is inherited we can exit early
2021
if (computedStyle.visibility === 'hidden') {

0 commit comments

Comments
 (0)