-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathscreen.js
More file actions
132 lines (118 loc) · 3.95 KB
/
screen.js
File metadata and controls
132 lines (118 loc) · 3.95 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import jestSnapshotSerializerAnsi from 'jest-snapshot-serializer-ansi'
import {screen} from '..'
import {render, renderIntoDocument} from './helpers/test-utils'
expect.addSnapshotSerializer(jestSnapshotSerializerAnsi)
// Since screen.debug internally calls getUserCodeFrame, we mock it so it doesn't affect these tests
jest.mock('../get-user-code-frame', () => ({
getUserCodeFrame: () => '',
}))
beforeEach(() => {
jest.spyOn(console, 'log').mockImplementation(() => {})
})
afterEach(() => {
console.log.mockRestore()
})
test('exposes queries that are attached to document.body', async () => {
renderIntoDocument(`<div>hello world</div>`)
screen.getByText(/hello world/i)
await screen.findByText(/hello world/i)
expect(screen.queryByText(/hello world/i)).not.toBeNull()
})
test('logs Playground URL that are attached to document.body', () => {
renderIntoDocument(`<div>hello world</div>`)
screen.logTestingPlaygroundURL()
expect(console.log).toHaveBeenCalledTimes(1)
expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(`
Open this URL in your browser
https://testing-playground.com/#markup=DwEwlgbgfAFgpgGwQewAQHdkCcEmAenGiA
`)
})
test('logs messsage when element is empty', () => {
screen.logTestingPlaygroundURL(document.createElement('div'))
expect(console.log).toHaveBeenCalledTimes(1)
expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(
`The provided element doesn't have any children.`,
)
})
test('logs messsage when element is not a valid HTML', () => {
screen.logTestingPlaygroundURL(null)
expect(console.log).toHaveBeenCalledTimes(1)
expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(
`The element you're providing isn't a valid DOM element.`,
)
console.log.mockClear()
screen.logTestingPlaygroundURL({})
expect(console.log).toHaveBeenCalledTimes(1)
expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(
`The element you're providing isn't a valid DOM element.`,
)
})
test('logs Playground URL that are passed as element', () => {
screen.logTestingPlaygroundURL(render(`<h1>Sign <em>up</em></h1>`).container)
expect(console.log).toHaveBeenCalledTimes(1)
expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(`
Open this URL in your browser
https://testing-playground.com/#markup=DwCwjAfAyglg5gOwATAKYFsIFcAOwD0GEB4EQA
`)
})
test('returns Playground URL that are passed as element', () => {
const playGroundUrl = screen.logTestingPlaygroundURL(
render(`<h1>Sign <em>up</em></h1>`).container,
)
expect(playGroundUrl).toMatchInlineSnapshot(
'https://testing-playground.com/#markup=DwCwjAfAyglg5gOwATAKYFsIFcAOwD0GEB4EQA',
)
})
test('exposes debug method', () => {
renderIntoDocument(
`<button>test</button><span>multi-test</span><div>multi-test</div>`,
)
// log document
screen.debug()
expect(console.log).toHaveBeenCalledTimes(1)
expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(`
<body>
<button>
test
</button>
<span>
multi-test
</span>
<div>
multi-test
</div>
</body>
`)
console.log.mockClear()
// log single element
screen.debug(screen.getByText('test', {selector: 'button'}))
expect(console.log).toHaveBeenCalledTimes(1)
expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(`
<button>
test
</button>
`)
console.log.mockClear()
// log multiple elements
screen.debug(screen.getAllByText('multi-test'))
expect(console.log).toHaveBeenCalledTimes(2)
expect(console.log.mock.calls[0][0]).toMatchInlineSnapshot(`
<span>
multi-test
</span>
`)
expect(console.log.mock.calls[1][0]).toMatchInlineSnapshot(`
<div>
multi-test
</div>
`)
console.log.mockClear()
})
test('queries after replaceWith', async () => {
const newBody = document.createElement('body')
newBody.innerHTML = '<div>replaceWith element</div>'
document.body.replaceWith(newBody)
screen.getByText('replaceWith element')
await screen.findByText('replaceWith element')
expect(screen.queryByText('replaceWith element')).not.toBeNull()
})