-
Notifications
You must be signed in to change notification settings - Fork 470
/
Copy pathhelpers.js
58 lines (55 loc) · 2.75 KB
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import {screen} from '../'
import {getDocument, getWindowFromNode, checkContainerType} from '../helpers'
test('returns global document if exists', () => {
expect(getDocument()).toBe(document)
})
describe('window retrieval throws when given something other than a node', () => {
// we had an issue when user insert screen instead of query
// actually here should be another more clear error output
test('screen as node', () => {
expect(() => getWindowFromNode(screen)).toThrowErrorMatchingInlineSnapshot(
`"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..., \`?"`,
)
})
test('Promise as node', () => {
expect(() =>
getWindowFromNode(new Promise(jest.fn())),
).toThrowErrorMatchingInlineSnapshot(
`"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...\`?"`,
)
})
test('Array as node', () => {
expect(() => getWindowFromNode([])).toThrowErrorMatchingInlineSnapshot(
`"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...\`?"`,
)
})
test('unknown as node', () => {
expect(() => getWindowFromNode({})).toThrowErrorMatchingInlineSnapshot(
`"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"`,
)
})
})
describe('query container validation throws when validation fails', () => {
test('undefined as container', () => {
expect(() =>
checkContainerType(undefined),
).toThrowErrorMatchingInlineSnapshot(
`"Expected container to be an Element, a Document or a DocumentFragment but got undefined."`,
)
})
test('null as container', () => {
expect(() => checkContainerType(null)).toThrowErrorMatchingInlineSnapshot(
`"Expected container to be an Element, a Document or a DocumentFragment but got null."`,
)
})
test('array as container', () => {
expect(() => checkContainerType([])).toThrowErrorMatchingInlineSnapshot(
`"Expected container to be an Element, a Document or a DocumentFragment but got Array."`,
)
})
test('object as container', () => {
expect(() => checkContainerType({})).toThrowErrorMatchingInlineSnapshot(
`"Expected container to be an Element, a Document or a DocumentFragment but got Object."`,
)
})
})