Skip to content

Commit

Permalink
undo renderToRenderStream changes
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Dec 2, 2024
1 parent 200d9d2 commit 0cf8f41
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,16 @@ const utils = await render(<Component />, options)
you can also call

```js
const {takeRender, utils} = await renderToRenderStream(
<Component />,
combinedOptions,
)
const renderStream = renderToRenderStream(<Component />, combinedOptions)
// if required
const utils = await renderStream.renderResultPromise
```

This might be shorter (especially in cases where you don't need to access
`utils`), but keep in mind that the render is executed **asynchronously** after
calling `renderToRenderStream`, and that you need to `await renderResultPromise`
if you need access to `utils` as returned by `render`.

### `renderHookToSnapshotStream`

Usage is very similar to RTL's `renderHook`, but you get a `snapshotStream`
Expand Down
23 changes: 15 additions & 8 deletions src/__tests__/renderToRenderStream.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ 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,
Expand Down Expand Up @@ -33,9 +32,13 @@ describe('snapshotDOM', () => {
)
}

const {takeRender, utils} = await renderToRenderStream(<Counter />, {
snapshotDOM: true,
})
const {takeRender, renderResultPromise} = renderToRenderStream(
<Counter />,
{
snapshotDOM: true,
},
)
const utils = await renderResultPromise
const incrementButton = utils.getByText('Increment')
await userEvent.click(incrementButton)
await userEvent.click(incrementButton)
Expand Down Expand Up @@ -65,10 +68,14 @@ describe('snapshotDOM', () => {
return null
},
}
const {takeRender, utils} = await renderToRenderStream(<Component />, {
queries,
snapshotDOM: true,
})
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)
Expand Down
17 changes: 8 additions & 9 deletions src/renderToRenderStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ export interface RenderStreamWithRenderResult<
Snapshot extends ValidSnapshot = void,
Q extends Queries = SyncQueries,
> extends RenderStream<Snapshot, Q> {
utils: AsyncRenderResult<Q>
renderResultPromise: Promise<AsyncRenderResult<Q>>
}

/**
* Render into a container which is appended to document.body. It should be used with cleanup.
*/
export async function renderToRenderStream<
export function renderToRenderStream<
Snapshot extends ValidSnapshot = void,
Q extends Queries = SyncQueries,
>(
Expand All @@ -37,20 +37,19 @@ export async function renderToRenderStream<
queries,
...options
}: RenderOptions<Snapshot, Q> = {},
): Promise<RenderStreamWithRenderResult<Snapshot, Q>> {
): RenderStreamWithRenderResult<Snapshot, Q> {
const {render, ...stream} = createRenderStream<Snapshot, Q>({
onRender,
snapshotDOM,
initialSnapshot,
skipNonTrackingRenders,
queries,
})

// We need to wait a tick before calling `render` here, because the definition of `ui`
// `render` needs to be called asynchronously here, because the definition of `ui`
// might contain components that reference the return value of `renderToRenderStream`
// itself, e.g. `replaceSnapshot` or `mergeSnapshot`.
await Promise.resolve()
const utils = await render<Q>(ui, {...options, queries})

return {...stream, utils}
const renderResultPromise = Promise.resolve().then(() =>
render<Q>(ui, {...options, queries}),
)
return {...stream, renderResultPromise}
}

0 comments on commit 0cf8f41

Please sign in to comment.