Skip to content

Commit

Permalink
move render into createProfiler, remove Wrapper prop
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Oct 8, 2024
1 parent 9b6fdee commit 2986cde
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 45 deletions.
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export type { NextRenderOptions, RenderStream } from "./profile/profile.js";
export type {
NextRenderOptions,
RenderStream,
RenderStreamWithRenderFn,
} from "./profile/profile.js";
export {
createProfiler,
useTrackRenders,
Expand Down
32 changes: 26 additions & 6 deletions src/profile/profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { applyStackTrace, captureStackTrace } from "./traces.js";
import type { ProfilerContextValue } from "./context.js";
import { ProfilerContextProvider, useProfilerContext } from "./context.js";
import { disableActWarnings } from "./disableActWarnings.js";
import { render } from "@testing-library/react";
import { render as baseRender, RenderOptions } from "@testing-library/react";

export type ValidSnapshot =
| void
Expand Down Expand Up @@ -80,9 +80,9 @@ export interface RenderStream<Snapshot extends ValidSnapshot>
extends ProfiledComponentFields<Snapshot>,
ProfiledComponentOnlyFields<Snapshot> {}

export interface RenderStreamWithWrapper<Snapshot extends ValidSnapshot>
export interface RenderStreamWithRenderFn<Snapshot extends ValidSnapshot>
extends RenderStream<Snapshot> {
Wrapper: React.FC<{ children: React.ReactNode }>;
render: typeof baseRender;
}

export type ProfilerOptions<Snapshot extends ValidSnapshot> = {
Expand All @@ -108,7 +108,7 @@ export function createProfiler<Snapshot extends ValidSnapshot = void>({
snapshotDOM = false,
initialSnapshot,
skipNonTrackingRenders,
}: ProfilerOptions<Snapshot> = {}): RenderStreamWithWrapper<Snapshot> {
}: ProfilerOptions<Snapshot> = {}): RenderStreamWithRenderFn<Snapshot> {
let nextRender: Promise<Render<Snapshot>> | undefined;
let resolveNextRender: ((render: Render<Snapshot>) => void) | undefined;
let rejectNextRender: ((error: unknown) => void) | undefined;
Expand Down Expand Up @@ -221,7 +221,27 @@ export function createProfiler<Snapshot extends ValidSnapshot = void>({
);
}

const Profiler: RenderStreamWithWrapper<Snapshot> = Object.assign(
const render = ((
ui: React.ReactNode,
options?: RenderOptions<any, any, any>
) => {
return baseRender(ui, {
...options,
wrapper: (props) => {
let elem: React.ReactNode = React.createElement(
Wrapper,
undefined,
props.children
);
if (options?.wrapper) {
elem = React.createElement(options.wrapper, undefined, elem);
}
return elem;
},
});
}) as typeof baseRender;

const Profiler: RenderStreamWithRenderFn<Snapshot> = Object.assign(
{
replaceSnapshot,
mergeSnapshot,
Expand Down Expand Up @@ -318,7 +338,7 @@ export function createProfiler<Snapshot extends ValidSnapshot = void>({
return nextRender;
},
} satisfies ProfiledComponentFields<Snapshot>,
{ Wrapper }
{ render }
);
return Profiler;
}
Expand Down
20 changes: 3 additions & 17 deletions src/renderHookToSnapshotStream.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { render, RenderHookOptions } from "@testing-library/react";
import { RenderHookOptions } from "@testing-library/react";
import {
createProfiler,
ProfiledComponentFields,
RenderStream,
ValidSnapshot,
} from "./profile/profile.js";
import { Render } from "./profile/Render.js";
Expand Down Expand Up @@ -47,7 +46,7 @@ export function renderHookToSnapshotStream<
unmount: () => void;
},
] {
const { Wrapper, ...stream } = createProfiler<ReturnValue>();
const { render, ...stream } = createProfiler<ReturnValue>();

const ProfiledHook: React.FC<Props> = (props) => {
stream.replaceSnapshot(renderCallback(props));
Expand All @@ -56,20 +55,7 @@ export function renderHookToSnapshotStream<

const { rerender: baseRerender, unmount } = render(
createElement(ProfiledHook, initialProps),
{
...options,
wrapper(props) {
let elem: React.ReactNode = createElement(
Wrapper,
undefined,
props.children
);
if (options.wrapper) {
elem = createElement(options.wrapper, undefined, elem);
}
return elem;
},
}
options
);

function rerender(rerenderCallbackProps: Props) {
Expand Down
25 changes: 4 additions & 21 deletions src/renderToRenderStream.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
render,
type RenderOptions as BaseOptions,
type RenderResult as BaseResult,
} from "@testing-library/react";
Expand All @@ -10,15 +9,14 @@ import {
ProfilerOptions,
ValidSnapshot,
} from "./profile/profile.js";
import { createElement } from "react";

type RenderOptions<Snapshot extends ValidSnapshot = void> = BaseOptions &
ProfilerOptions<Snapshot>;

type RenderResult<Snapshot extends ValidSnapshot = void> = [
Stream: ProfiledComponentFields<Snapshot> &
ProfiledComponentOnlyFields<Snapshot>,
resultPromise: Promise<BaseResult>,
renderResultPromise: Promise<BaseResult>,
];

/**
Expand All @@ -35,27 +33,12 @@ export function renderToRenderStream<Snapshot extends ValidSnapshot = void>(
...options
}: RenderOptions<Snapshot> = {}
): RenderResult<Snapshot> {
const { Wrapper, ...stream } = createProfiler<Snapshot>({
const { render, ...stream } = createProfiler<Snapshot>({
onRender,
snapshotDOM,
initialSnapshot,
skipNonTrackingRenders,
});
const result = Promise.resolve().then(() =>
render(ui, {
...options,
wrapper(props) {
let elem: React.ReactNode = createElement(
Wrapper,
undefined,
props.children
);
if (options.wrapper) {
elem = createElement(options.wrapper, undefined, elem);
}
return elem;
},
})
);
return [stream, result];
const renderResultPromise = Promise.resolve().then(() => render(ui, options));
return [stream, renderResultPromise];
}

0 comments on commit 2986cde

Please sign in to comment.