Skip to content

Commit

Permalink
allow any kind of hook return value for renderHookToSnapshotStream
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Oct 10, 2024
1 parent ed33bd3 commit 14a87fa
Showing 1 changed file with 14 additions and 21 deletions.
35 changes: 14 additions & 21 deletions src/renderHookToSnapshotStream.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import { RenderHookOptions } from "@testing-library/react";
import {
createRenderStream,
NextRenderOptions,
ValidSnapshot,
} from "./profile/profile.js";
import { createRenderStream, NextRenderOptions } from "./profile/profile.js";
import { Render } from "./profile/Render.js";
import { createElement } from "react";
import { Assertable, assertableSymbol, markAssertable } from "./assertable.js";

export interface SnapshotStream<Snapshot extends ValidSnapshot, Props>
extends Assertable {
export interface SnapshotStream<Snapshot, Props> extends Assertable {
/**
* An array of all renders that have happened so far.
* Errors thrown during component render will be captured here, too.
*/
renders: Array<
Render<Snapshot> | { phase: "snapshotError"; count: number; error: unknown }
| Render<{ value: Snapshot }>
| { phase: "snapshotError"; count: number; error: unknown }
>;
/**
* Peeks the next render from the current iterator position, without advancing the iterator.
Expand Down Expand Up @@ -48,44 +44,41 @@ export interface SnapshotStream<Snapshot extends ValidSnapshot, Props>
unmount: () => void;
}

export function renderHookToSnapshotStream<
ReturnValue extends ValidSnapshot,
Props extends {},
>(
export function renderHookToSnapshotStream<ReturnValue, Props extends {}>(
renderCallback: (props: Props) => ReturnValue,
{ initialProps, ...options }: RenderHookOptions<Props> = {}
): SnapshotStream<ReturnValue, Props> {
const { render, ...stream } = createRenderStream<ReturnValue>();
const { render, ...stream } = createRenderStream<{ value: ReturnValue }>();

const ProfiledHook: React.FC<Props> = (props) => {
stream.replaceSnapshot(renderCallback(props));
const HookComponent: React.FC<Props> = (props) => {
stream.replaceSnapshot({ value: renderCallback(props) });
return null;
};

const { rerender: baseRerender, unmount } = render(
createElement(ProfiledHook, initialProps),
createElement(HookComponent, initialProps),
options
);

function rerender(rerenderCallbackProps: Props) {
return baseRerender(createElement(ProfiledHook, rerenderCallbackProps));
return baseRerender(createElement(HookComponent, rerenderCallbackProps));
}

return {
[assertableSymbol]: stream,
renders: stream.renders,
totalSnapshotCount: stream.totalRenderCount,
async peekSnapshot(options) {
return (await stream.peekRender(options)).snapshot;
return (await stream.peekRender(options)).snapshot.value;
},
takeSnapshot: markAssertable(async function takeSnapshot(options) {
return (await stream.takeRender(options)).snapshot;
return (await stream.takeRender(options)).snapshot.value;
}, stream),
getCurrentSnapshot() {
return stream.getCurrentRender().snapshot;
return stream.getCurrentRender().snapshot.value;
},
async waitForNextSnapshot(options) {
return (await stream.waitForNextRender(options)).snapshot;
return (await stream.waitForNextRender(options)).snapshot.value;
},
rerender,
unmount,
Expand Down

0 comments on commit 14a87fa

Please sign in to comment.