Skip to content

Commit d5588de

Browse files
committed
also handle optional arguments
1 parent 125098a commit d5588de

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

Diff for: src/__tests__/renderHookToSnapshotStream.test.tsx

+10
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,14 @@ test.skip('type test: render function without an argument -> no argument require
109109
await stream.rerender()
110110
await stream.rerender('foo')
111111
}
112+
{
113+
// argument is optional
114+
const stream = await renderHookToSnapshotStream((_arg1?: string) => {})
115+
116+
const _test1: SnapshotStream<void, void> = stream
117+
const _test2: SnapshotStream<void, string> = stream
118+
const _test3: SnapshotStream<void, string | undefined> = stream
119+
await stream.rerender()
120+
await stream.rerender('foo')
121+
}
112122
})

Diff for: src/renderHookToSnapshotStream.tsx

+11-1
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,20 @@ export interface SnapshotStream<Snapshot, Props> extends Assertable {
4141
* Does not advance the render iterator.
4242
*/
4343
waitForNextSnapshot(options?: NextRenderOptions): Promise<Snapshot>
44-
rerender: (rerenderCallbackProps: Props) => Promise<void>
44+
rerender: (rerenderCallbackProps: VoidOptionalArg<Props>) => Promise<void>
4545
unmount: () => void
4646
}
4747

48+
/**
49+
* if `Arg` can be `undefined`, replace it with `void` to make type represent an optional argument in a function argument position
50+
*/
51+
type VoidOptionalArg<Arg> = Arg extends any // distribute members of a potential `Props` union
52+
? undefined extends Arg
53+
? // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
54+
void
55+
: Arg
56+
: Arg
57+
4858
export async function renderHookToSnapshotStream<ReturnValue, Props = void>(
4959
renderCallback: (props: Props) => ReturnValue,
5060
{initialProps, ...renderOptions}: RenderHookOptions<Props> = {},

0 commit comments

Comments
 (0)