|
| 1 | +/* eslint-disable @typescript-eslint/no-use-before-define */ |
| 2 | +import {jest, describe, test, expect} from '@jest/globals' |
| 3 | +import {createRenderStream} from '@testing-library/react-render-stream' |
| 4 | +import {userEvent} from '@testing-library/user-event' |
| 5 | +import * as React from 'react' |
| 6 | +import {ErrorBoundary} from 'react-error-boundary' |
| 7 | + |
| 8 | +function CounterForm({ |
| 9 | + value, |
| 10 | + onIncrement, |
| 11 | +}: { |
| 12 | + value: number |
| 13 | + onIncrement: () => void |
| 14 | +}) { |
| 15 | + return ( |
| 16 | + <form> |
| 17 | + <button type="button" onClick={() => onIncrement()}> |
| 18 | + Increment |
| 19 | + </button> |
| 20 | + <label> |
| 21 | + Value |
| 22 | + <input type="number" value={value} readOnly /> |
| 23 | + </label> |
| 24 | + </form> |
| 25 | + ) |
| 26 | +} |
| 27 | + |
| 28 | +describe('snapshotDOM', () => { |
| 29 | + test('basic functionality', async () => { |
| 30 | + function Counter() { |
| 31 | + const [value, setValue] = React.useState(0) |
| 32 | + return ( |
| 33 | + <CounterForm value={value} onIncrement={() => setValue(v => v + 1)} /> |
| 34 | + ) |
| 35 | + } |
| 36 | + |
| 37 | + const {takeRender, render} = createRenderStream({ |
| 38 | + snapshotDOM: true, |
| 39 | + }) |
| 40 | + const utils = render(<Counter />) |
| 41 | + const incrementButton = utils.getByText('Increment') |
| 42 | + await userEvent.click(incrementButton) |
| 43 | + await userEvent.click(incrementButton) |
| 44 | + { |
| 45 | + const {withinDOM} = await takeRender() |
| 46 | + const input = withinDOM().getByLabelText<HTMLInputElement>('Value') |
| 47 | + expect(input.value).toBe('0') |
| 48 | + } |
| 49 | + { |
| 50 | + const {withinDOM} = await takeRender() |
| 51 | + const input = withinDOM().getByLabelText<HTMLInputElement>('Value') |
| 52 | + expect(input.value).toBe('1') |
| 53 | + } |
| 54 | + { |
| 55 | + const {withinDOM} = await takeRender() |
| 56 | + const input = withinDOM().getByLabelText<HTMLInputElement>('Value') |
| 57 | + expect(input.value).toBe('2') |
| 58 | + } |
| 59 | + }) |
| 60 | + |
| 61 | + test('errors when triggering events on rendered elemenst', async () => { |
| 62 | + function Counter() { |
| 63 | + const [value, setValue] = React.useState(0) |
| 64 | + return ( |
| 65 | + <CounterForm value={value} onIncrement={() => setValue(v => v + 1)} /> |
| 66 | + ) |
| 67 | + } |
| 68 | + |
| 69 | + const {takeRender, render} = createRenderStream({ |
| 70 | + snapshotDOM: true, |
| 71 | + }) |
| 72 | + render(<Counter />) |
| 73 | + { |
| 74 | + const {withinDOM} = await takeRender() |
| 75 | + const snapshotIncrementButton = withinDOM().getByText('Increment') |
| 76 | + try { |
| 77 | + await userEvent.click(snapshotIncrementButton) |
| 78 | + } catch (error) { |
| 79 | + expect(error).toMatchInlineSnapshot(` |
| 80 | +[Error: Uncaught [Error: |
| 81 | + DOM interaction with a snapshot detected in test. |
| 82 | + Please don't interact with the DOM you get from \`withinDOM\`, |
| 83 | + but still use \`screen\` to get elements for simulating user interaction. |
| 84 | + ]] |
| 85 | +`) |
| 86 | + } |
| 87 | + } |
| 88 | + }) |
| 89 | +}) |
| 90 | + |
| 91 | +describe('replaceSnapshot', () => { |
| 92 | + test('basic functionality', async () => { |
| 93 | + function Counter() { |
| 94 | + const [value, setValue] = React.useState(0) |
| 95 | + replaceSnapshot({value}) |
| 96 | + return ( |
| 97 | + <CounterForm value={value} onIncrement={() => setValue(v => v + 1)} /> |
| 98 | + ) |
| 99 | + } |
| 100 | + |
| 101 | + const {takeRender, replaceSnapshot, render} = createRenderStream<{ |
| 102 | + value: number |
| 103 | + }>() |
| 104 | + const utils = render(<Counter />) |
| 105 | + const incrementButton = utils.getByText('Increment') |
| 106 | + await userEvent.click(incrementButton) |
| 107 | + await userEvent.click(incrementButton) |
| 108 | + { |
| 109 | + const {snapshot} = await takeRender() |
| 110 | + expect(snapshot).toEqual({value: 0}) |
| 111 | + } |
| 112 | + { |
| 113 | + const {snapshot} = await takeRender() |
| 114 | + expect(snapshot).toEqual({value: 1}) |
| 115 | + } |
| 116 | + { |
| 117 | + const {snapshot} = await takeRender() |
| 118 | + expect(snapshot).toEqual({value: 2}) |
| 119 | + } |
| 120 | + }) |
| 121 | + describe('callback notation', () => { |
| 122 | + test('basic functionality', async () => { |
| 123 | + function Counter() { |
| 124 | + const [value, setValue] = React.useState(0) |
| 125 | + replaceSnapshot(oldSnapshot => ({...oldSnapshot, value})) |
| 126 | + return ( |
| 127 | + <CounterForm value={value} onIncrement={() => setValue(v => v + 1)} /> |
| 128 | + ) |
| 129 | + } |
| 130 | + |
| 131 | + const {takeRender, replaceSnapshot, render} = createRenderStream({ |
| 132 | + initialSnapshot: {unrelatedValue: 'unrelated', value: -1}, |
| 133 | + }) |
| 134 | + const utils = render(<Counter />) |
| 135 | + const incrementButton = utils.getByText('Increment') |
| 136 | + await userEvent.click(incrementButton) |
| 137 | + await userEvent.click(incrementButton) |
| 138 | + { |
| 139 | + const {snapshot} = await takeRender() |
| 140 | + expect(snapshot).toEqual({unrelatedValue: 'unrelated', value: 0}) |
| 141 | + } |
| 142 | + { |
| 143 | + const {snapshot} = await takeRender() |
| 144 | + expect(snapshot).toEqual({unrelatedValue: 'unrelated', value: 1}) |
| 145 | + } |
| 146 | + { |
| 147 | + const {snapshot} = await takeRender() |
| 148 | + expect(snapshot).toEqual({unrelatedValue: 'unrelated', value: 2}) |
| 149 | + } |
| 150 | + }) |
| 151 | + test('requires initialSnapshot', async () => { |
| 152 | + function Counter() { |
| 153 | + const [value, setValue] = React.useState(0) |
| 154 | + replaceSnapshot(() => ({value})) |
| 155 | + return ( |
| 156 | + <CounterForm value={value} onIncrement={() => setValue(v => v + 1)} /> |
| 157 | + ) |
| 158 | + } |
| 159 | + |
| 160 | + const {replaceSnapshot, render} = createRenderStream<{ |
| 161 | + value: number |
| 162 | + }>() |
| 163 | + let caughtError: Error |
| 164 | + |
| 165 | + const spy = jest.spyOn(console, 'error') |
| 166 | + spy.mockImplementation(() => {}) |
| 167 | + render( |
| 168 | + <ErrorBoundary |
| 169 | + fallbackRender={({error}) => { |
| 170 | + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
| 171 | + caughtError = error |
| 172 | + return null |
| 173 | + }} |
| 174 | + > |
| 175 | + <Counter /> |
| 176 | + </ErrorBoundary>, |
| 177 | + ) |
| 178 | + spy.mockRestore() |
| 179 | + |
| 180 | + expect(caughtError!).toMatchInlineSnapshot( |
| 181 | + `[Error: Cannot use a function to update the snapshot if no initial snapshot was provided.]`, |
| 182 | + ) |
| 183 | + }) |
| 184 | + }) |
| 185 | +}) |
0 commit comments