Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: testing-library/react-render-stream-testing-library
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.0.0
Choose a base ref
...
head repository: testing-library/react-render-stream-testing-library
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v2.0.1
Choose a head ref
  • 6 commits
  • 2 files changed
  • 2 contributors

Commits on Jan 13, 2025

  1. fix: Make props optional in rerender function returned from `render…

    …HookToSnapshotStream`
    jerelmiller committed Jan 13, 2025
    Copy the full SHA
    0167414 View commit details

Commits on Jan 16, 2025

  1. less intrusive method

    phryneas committed Jan 16, 2025
    Copy the full SHA
    ca944cd View commit details
  2. remove eslint hint

    phryneas committed Jan 16, 2025
    Copy the full SHA
    125098a View commit details
  3. also handle optional arguments

    phryneas committed Jan 16, 2025
    Copy the full SHA
    d5588de View commit details
  4. fix last type errors

    phryneas committed Jan 16, 2025
    Copy the full SHA
    ac0b317 View commit details
  5. Merge pull request #14 from testing-library/jerel/fix-non-optional-props

    fix: Make props optional in `rerender` function returned from `renderHookToSnapshotStream`
    jerelmiller authored Jan 16, 2025

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    6003495 View commit details
Showing with 64 additions and 5 deletions.
  1. +49 −1 src/__tests__/renderHookToSnapshotStream.test.tsx
  2. +15 −4 src/renderHookToSnapshotStream.tsx
50 changes: 49 additions & 1 deletion src/__tests__/renderHookToSnapshotStream.test.tsx
Original file line number Diff line number Diff line change
@@ -3,7 +3,10 @@
import {EventEmitter} from 'node:events'
import {scheduler} from 'node:timers/promises'
import {test, expect} from '@jest/globals'
import {renderHookToSnapshotStream} from '@testing-library/react-render-stream'
import {
renderHookToSnapshotStream,
SnapshotStream,
} from '@testing-library/react-render-stream'
import * as React from 'react'

const testEvents = new EventEmitter<{
@@ -72,3 +75,48 @@ test.each<[type: string, initialValue: unknown, ...nextValues: unknown[]]>([
expect(await takeSnapshot()).toBe(nextValue)
}
})

test.skip('type test: render function without an argument -> no argument required for `rerender`', async () => {
{
// prop type has nothing to infer on - defaults to `void`
const stream = await renderHookToSnapshotStream(() => {})
const _test1: SnapshotStream<void, void> = stream
// @ts-expect-error should not be assignable
const _test2: SnapshotStream<void, string> = stream
await stream.rerender()
// @ts-expect-error invalid argument
await stream.rerender('foo')
}
{
// prop type is implicitly set via the render function argument
const stream = await renderHookToSnapshotStream((_arg1: string) => {})
// @ts-expect-error should not be assignable
const _test1: SnapshotStream<void, void> = stream
const _test2: SnapshotStream<void, string> = stream
// @ts-expect-error missing argument
await stream.rerender()
await stream.rerender('foo')
}
{
// prop type is implicitly set via the initialProps argument
const stream = await renderHookToSnapshotStream(() => {}, {
initialProps: 'initial',
})
// @ts-expect-error should not be assignable
const _test1: SnapshotStream<void, void> = stream
const _test2: SnapshotStream<void, string> = stream
// @ts-expect-error missing argument
await stream.rerender()
await stream.rerender('foo')
}
{
// argument is optional
const stream = await renderHookToSnapshotStream((_arg1?: string) => {})

const _test1: SnapshotStream<void, void> = stream
const _test2: SnapshotStream<void, string> = stream
const _test3: SnapshotStream<void, string | undefined> = stream
await stream.rerender()
await stream.rerender('foo')
}
})
19 changes: 15 additions & 4 deletions src/renderHookToSnapshotStream.tsx
Original file line number Diff line number Diff line change
@@ -41,11 +41,21 @@ export interface SnapshotStream<Snapshot, Props> extends Assertable {
* Does not advance the render iterator.
*/
waitForNextSnapshot(options?: NextRenderOptions): Promise<Snapshot>
rerender: (rerenderCallbackProps: Props) => Promise<void>
rerender: (rerenderCallbackProps: VoidOptionalArg<Props>) => Promise<void>
unmount: () => void
}

export async function renderHookToSnapshotStream<ReturnValue, Props>(
/**
* if `Arg` can be `undefined`, replace it with `void` to make type represent an optional argument in a function argument position
*/
type VoidOptionalArg<Arg> = Arg extends any // distribute members of a potential `Props` union
? undefined extends Arg
? // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
void
: Arg
: Arg

export async function renderHookToSnapshotStream<ReturnValue, Props = void>(
renderCallback: (props: Props) => ReturnValue,
{initialProps, ...renderOptions}: RenderHookOptions<Props> = {},
): Promise<SnapshotStream<ReturnValue, Props>> {
@@ -61,8 +71,9 @@ export async function renderHookToSnapshotStream<ReturnValue, Props>(
renderOptions,
)

function rerender(rerenderCallbackProps: Props) {
return baseRerender(<HookComponent arg={rerenderCallbackProps} />)
function rerender(rerenderCallbackProps: VoidOptionalArg<Props>) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
return baseRerender(<HookComponent arg={rerenderCallbackProps as any} />)
}

return {