-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrenderToRenderStream.test.tsx
94 lines (90 loc) · 2.54 KB
/
renderToRenderStream.test.tsx
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
/* eslint-disable @typescript-eslint/no-use-before-define */
import {describe, test, expect} from '@jest/globals'
import {renderToRenderStream} from '@testing-library/react-render-stream'
import {userEvent} from '@testing-library/user-event'
import * as React from 'react'
function CounterForm({
value,
onIncrement,
}: {
value: number
onIncrement: () => void
}) {
return (
<form>
<button type="button" onClick={() => onIncrement()}>
Increment
</button>
<label>
Value
<input type="number" value={value} readOnly />
</label>
</form>
)
}
describe('snapshotDOM', () => {
test('basic functionality', async () => {
function Counter() {
const [value, setValue] = React.useState(0)
return (
<CounterForm value={value} onIncrement={() => setValue(v => v + 1)} />
)
}
const {takeRender, renderResultPromise} = renderToRenderStream(
<Counter />,
{
snapshotDOM: true,
},
)
const utils = await renderResultPromise
const incrementButton = utils.getByText('Increment')
await userEvent.click(incrementButton)
await userEvent.click(incrementButton)
{
const {withinDOM} = await takeRender()
const input = withinDOM().getByLabelText<HTMLInputElement>('Value')
expect(input.value).toBe('0')
}
{
const {withinDOM} = await takeRender()
const input = withinDOM().getByLabelText<HTMLInputElement>('Value')
expect(input.value).toBe('1')
}
{
const {withinDOM} = await takeRender()
const input = withinDOM().getByLabelText<HTMLInputElement>('Value')
expect(input.value).toBe('2')
}
})
test('queries option', async () => {
function Component() {
return null
}
const queries = {
foo: (_: any) => {
return null
},
}
const {takeRender, renderResultPromise} = renderToRenderStream(
<Component />,
{
queries,
snapshotDOM: true,
},
)
const utils = await renderResultPromise
expect(utils.foo()).toBe(null)
const {withinDOM} = await takeRender()
expect(withinDOM().foo()).toBe(null)
function _typeTest() {
// @ts-expect-error should not be present
utils.getByText
// @ts-expect-error should not be present
withinDOM().getByText
utils.debug()
withinDOM().debug()
const _str: string = withinDOM().logTestingPlaygroundURL()
}
})
})
// for more tests, see the `createRenderStream` test suite, as `renderToRenderStream` is just a wrapper around that