|
1 | 1 | import { RenderHookOptions } from "@testing-library/react"; |
2 | | -import { |
3 | | - createRenderStream, |
4 | | - NextRenderOptions, |
5 | | - ValidSnapshot, |
6 | | -} from "./profile/profile.js"; |
| 2 | +import { createRenderStream, NextRenderOptions } from "./profile/profile.js"; |
7 | 3 | import { Render } from "./profile/Render.js"; |
8 | 4 | import { createElement } from "react"; |
9 | 5 | import { Assertable, assertableSymbol, markAssertable } from "./assertable.js"; |
10 | 6 |
|
11 | | -export interface SnapshotStream<Snapshot extends ValidSnapshot, Props> |
12 | | - extends Assertable { |
| 7 | +export interface SnapshotStream<Snapshot, Props> extends Assertable { |
13 | 8 | /** |
14 | 9 | * An array of all renders that have happened so far. |
15 | 10 | * Errors thrown during component render will be captured here, too. |
16 | 11 | */ |
17 | 12 | renders: Array< |
18 | | - Render<Snapshot> | { phase: "snapshotError"; count: number; error: unknown } |
| 13 | + | Render<{ value: Snapshot }> |
| 14 | + | { phase: "snapshotError"; count: number; error: unknown } |
19 | 15 | >; |
20 | 16 | /** |
21 | 17 | * Peeks the next render from the current iterator position, without advancing the iterator. |
@@ -48,44 +44,41 @@ export interface SnapshotStream<Snapshot extends ValidSnapshot, Props> |
48 | 44 | unmount: () => void; |
49 | 45 | } |
50 | 46 |
|
51 | | -export function renderHookToSnapshotStream< |
52 | | - ReturnValue extends ValidSnapshot, |
53 | | - Props extends {}, |
54 | | ->( |
| 47 | +export function renderHookToSnapshotStream<ReturnValue, Props extends {}>( |
55 | 48 | renderCallback: (props: Props) => ReturnValue, |
56 | 49 | { initialProps, ...options }: RenderHookOptions<Props> = {} |
57 | 50 | ): SnapshotStream<ReturnValue, Props> { |
58 | | - const { render, ...stream } = createRenderStream<ReturnValue>(); |
| 51 | + const { render, ...stream } = createRenderStream<{ value: ReturnValue }>(); |
59 | 52 |
|
60 | | - const ProfiledHook: React.FC<Props> = (props) => { |
61 | | - stream.replaceSnapshot(renderCallback(props)); |
| 53 | + const HookComponent: React.FC<Props> = (props) => { |
| 54 | + stream.replaceSnapshot({ value: renderCallback(props) }); |
62 | 55 | return null; |
63 | 56 | }; |
64 | 57 |
|
65 | 58 | const { rerender: baseRerender, unmount } = render( |
66 | | - createElement(ProfiledHook, initialProps), |
| 59 | + createElement(HookComponent, initialProps), |
67 | 60 | options |
68 | 61 | ); |
69 | 62 |
|
70 | 63 | function rerender(rerenderCallbackProps: Props) { |
71 | | - return baseRerender(createElement(ProfiledHook, rerenderCallbackProps)); |
| 64 | + return baseRerender(createElement(HookComponent, rerenderCallbackProps)); |
72 | 65 | } |
73 | 66 |
|
74 | 67 | return { |
75 | 68 | [assertableSymbol]: stream, |
76 | 69 | renders: stream.renders, |
77 | 70 | totalSnapshotCount: stream.totalRenderCount, |
78 | 71 | async peekSnapshot(options) { |
79 | | - return (await stream.peekRender(options)).snapshot; |
| 72 | + return (await stream.peekRender(options)).snapshot.value; |
80 | 73 | }, |
81 | 74 | takeSnapshot: markAssertable(async function takeSnapshot(options) { |
82 | | - return (await stream.takeRender(options)).snapshot; |
| 75 | + return (await stream.takeRender(options)).snapshot.value; |
83 | 76 | }, stream), |
84 | 77 | getCurrentSnapshot() { |
85 | | - return stream.getCurrentRender().snapshot; |
| 78 | + return stream.getCurrentRender().snapshot.value; |
86 | 79 | }, |
87 | 80 | async waitForNextSnapshot(options) { |
88 | | - return (await stream.waitForNextRender(options)).snapshot; |
| 81 | + return (await stream.waitForNextRender(options)).snapshot.value; |
89 | 82 | }, |
90 | 83 | rerender, |
91 | 84 | unmount, |
|
0 commit comments