|
| 1 | +/* eslint-disable @typescript-eslint/no-use-before-define */ |
| 2 | +import {describe, test, expect} from '@jest/globals' |
| 3 | +import {renderToRenderStream} from '@testing-library/react-render-stream' |
| 4 | +import {userEvent} from '@testing-library/user-event' |
| 5 | +import * as React from 'react' |
| 6 | +function CounterForm({ |
| 7 | + value, |
| 8 | + onIncrement, |
| 9 | +}: { |
| 10 | + value: number |
| 11 | + onIncrement: () => void |
| 12 | +}) { |
| 13 | + return ( |
| 14 | + <form> |
| 15 | + <button type="button" onClick={() => onIncrement()}> |
| 16 | + Increment |
| 17 | + </button> |
| 18 | + <label> |
| 19 | + Value |
| 20 | + <input type="number" value={value} readOnly /> |
| 21 | + </label> |
| 22 | + </form> |
| 23 | + ) |
| 24 | +} |
| 25 | + |
| 26 | +describe('snapshotDOM', () => { |
| 27 | + test('basic functionality', async () => { |
| 28 | + function Counter() { |
| 29 | + const [value, setValue] = React.useState(0) |
| 30 | + return ( |
| 31 | + <CounterForm value={value} onIncrement={() => setValue(v => v + 1)} /> |
| 32 | + ) |
| 33 | + } |
| 34 | + |
| 35 | + const {takeRender, renderResultPromise} = renderToRenderStream( |
| 36 | + <Counter />, |
| 37 | + { |
| 38 | + snapshotDOM: true, |
| 39 | + }, |
| 40 | + ) |
| 41 | + const utils = await renderResultPromise |
| 42 | + const incrementButton = utils.getByText('Increment') |
| 43 | + await userEvent.click(incrementButton) |
| 44 | + await userEvent.click(incrementButton) |
| 45 | + { |
| 46 | + const {withinDOM} = await takeRender() |
| 47 | + const input = withinDOM().getByLabelText<HTMLInputElement>('Value') |
| 48 | + expect(input.value).toBe('0') |
| 49 | + } |
| 50 | + { |
| 51 | + const {withinDOM} = await takeRender() |
| 52 | + const input = withinDOM().getByLabelText<HTMLInputElement>('Value') |
| 53 | + expect(input.value).toBe('1') |
| 54 | + } |
| 55 | + { |
| 56 | + const {withinDOM} = await takeRender() |
| 57 | + const input = withinDOM().getByLabelText<HTMLInputElement>('Value') |
| 58 | + expect(input.value).toBe('2') |
| 59 | + } |
| 60 | + }) |
| 61 | +}) |
| 62 | + |
| 63 | +// for more tests, see the `createRenderStream` test suite, as `renderToRenderStream` is just a wrapper around that |
0 commit comments