|
| 1 | +import {expect, jest, test} from '@jest/globals' |
| 2 | +import {createRenderStream} from '@testing-library/react-render-stream' |
| 3 | +import * as React from 'react' |
| 4 | +import {flushSync} from 'react-dom' |
| 5 | + |
| 6 | +test.each([false, true])( |
| 7 | + 'a completed wait cannot clear a later waiter (snapshot error: %s)', |
| 8 | + async snapshotError => { |
| 9 | + const error = new Error('snapshot failed') |
| 10 | + const stream = createRenderStream<{value: number}>({ |
| 11 | + onRender({snapshot}) { |
| 12 | + if (snapshotError && snapshot.value === 1) throw error |
| 13 | + }, |
| 14 | + }) |
| 15 | + let update: React.Dispatch<React.SetStateAction<number>> |
| 16 | + function Component() { |
| 17 | + const [value, setValue] = React.useState(0) |
| 18 | + update = setValue |
| 19 | + stream.replaceSnapshot({value}) |
| 20 | + return null |
| 21 | + } |
| 22 | + await stream.render(<Component />) |
| 23 | + await stream.takeRender() |
| 24 | + |
| 25 | + jest.useFakeTimers() |
| 26 | + try { |
| 27 | + const first = stream.takeRender({timeout: 100}).then( |
| 28 | + () => undefined, |
| 29 | + failure => failure, |
| 30 | + ) |
| 31 | + flushSync(() => update!(1)) |
| 32 | + await jest.advanceTimersByTimeAsync(0) |
| 33 | + expect(await first).toBe(snapshotError ? error : undefined) |
| 34 | + |
| 35 | + const second = stream.takeRender({timeout: 200}).catch(failure => failure) |
| 36 | + // Cross the first wait's original deadline while the second is pending. |
| 37 | + await jest.advanceTimersByTimeAsync(100) |
| 38 | + flushSync(() => update!(2)) |
| 39 | + await jest.advanceTimersByTimeAsync(100) |
| 40 | + expect(await second).toMatchObject({snapshot: {value: 2}}) |
| 41 | + } finally { |
| 42 | + jest.useRealTimers() |
| 43 | + } |
| 44 | + }, |
| 45 | +) |
0 commit comments