Skip to content

Commit 8cbde6b

Browse files
committed
add a check for unavailable window
1 parent 440a846 commit 8cbde6b

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

src/__tests__/helpers.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,22 @@ describe('window retrieval throws when given something other than a node', () =>
2525
`It looks like you passed an Array instead of a DOM node. Did you do something like \`fireEvent.click(screen.getAllBy...\` when you meant to use a \`getBy\` query \`fireEvent.click(screen.getBy...\`?`,
2626
)
2727
})
28-
test('HTMLElement as node', () => {
28+
test('window is not available for node', () => {
2929
const elem = document.createElement('div')
30-
Object.defineProperty(elem, 'ownerDocument', {
30+
Object.defineProperty(elem.ownerDocument, 'defaultView', {
3131
get: function get() {
3232
return null
3333
},
3434
})
3535

3636
expect(() => getWindowFromNode(elem)).toThrowErrorMatchingInlineSnapshot(
37-
`Unable to find the "window" object for the given node. Please file an issue with the code that's causing you to see this error: https://github.com/testing-library/dom-testing-library/issues/new`,
37+
`It looks like the window object is not available for the provided node.`,
3838
)
3939
})
4040

4141
test('unknown as node', () => {
4242
expect(() => getWindowFromNode({})).toThrowErrorMatchingInlineSnapshot(
43-
`The given node is not an HTMLElement, the node type is: object`,
43+
`The given node is not an Element, the node type is: object.`,
4444
)
4545
})
4646
})

src/helpers.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ function getWindowFromNode(node) {
3333
} else if (node.window) {
3434
// node is window
3535
return node.window
36+
} else if (node.ownerDocument && !node.ownerDocument.defaultValue) {
37+
throw new Error(
38+
`It looks like the window object is not available for the provided node.`,
39+
)
3640
} else if (node.then instanceof Function) {
3741
throw new Error(
3842
`It looks like you passed a Promise object instead of a DOM node. Did you do something like \`fireEvent.click(screen.findBy...\` when you meant to use a \`getBy\` query \`fireEvent.click(screen.getBy...\`, or await the findBy query \`fireEvent.click(await screen.findBy...\`?`,
@@ -48,15 +52,10 @@ function getWindowFromNode(node) {
4852
throw new Error(
4953
`It looks like you passed a \`screen\` object. Did you do something like \`fireEvent.click(screen, ...\` when you meant to use a query, e.g. \`fireEvent.click(screen.getBy..., \`?`,
5054
)
51-
} else if (node instanceof HTMLElement) {
52-
// The user passed something unusual to a calling function
53-
throw new Error(
54-
`Unable to find the "window" object for the given node. Please file an issue with the code that's causing you to see this error: https://github.com/testing-library/dom-testing-library/issues/new`,
55-
)
5655
} else {
5756
// The user passed something unusual to a calling function
5857
throw new Error(
59-
`The given node is not an HTMLElement, the node type is: ${typeof node}`,
58+
`The given node is not an Element, the node type is: ${typeof node}.`,
6059
)
6160
}
6261
}

0 commit comments

Comments
 (0)