Skip to content

Commit

Permalink
Track component function instead of component name for rendered compo…
Browse files Browse the repository at this point in the history
…nents
  • Loading branch information
jerelmiller committed Nov 22, 2023
1 parent c39cba0 commit 1e64614
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
12 changes: 6 additions & 6 deletions src/react/hooks/__tests__/useLoadableQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,15 @@ it("loads a query and suspends when the load query function is called", async ()
{
const { renderedComponents } = await Profiler.takeRender();

expect(renderedComponents).toStrictEqual(["App"]);
expect(renderedComponents).toStrictEqual([App]);
}

await act(() => user.click(screen.getByText("Load query")));

{
const { renderedComponents } = await Profiler.takeRender();

expect(renderedComponents).toStrictEqual(["App", "SuspenseFallback"]);
expect(renderedComponents).toStrictEqual([App, SuspenseFallback]);
}

{
Expand All @@ -283,7 +283,7 @@ it("loads a query and suspends when the load query function is called", async ()
networkStatus: NetworkStatus.ready,
});

expect(renderedComponents).toStrictEqual(["ReadQueryHook"]);
expect(renderedComponents).toStrictEqual([ReadQueryHook]);
}
});

Expand Down Expand Up @@ -322,20 +322,20 @@ it("loads a query with variables and suspends by passing variables to the loadQu

{
const { renderedComponents } = await Profiler.takeRender();
expect(renderedComponents).toStrictEqual(["App"]);
expect(renderedComponents).toStrictEqual([App]);
}

await act(() => user.click(screen.getByText("Load query")));

{
const { renderedComponents } = await Profiler.takeRender();
expect(renderedComponents).toStrictEqual(["App", "SuspenseFallback"]);
expect(renderedComponents).toStrictEqual([App, SuspenseFallback]);
}

{
const { snapshot, renderedComponents } = await Profiler.takeRender();

expect(renderedComponents).toStrictEqual(["ReadQueryHook"]);
expect(renderedComponents).toStrictEqual([ReadQueryHook]);
expect(snapshot.result).toEqual({
data: { character: { id: "1", name: "Spider-Man" } },
networkStatus: NetworkStatus.ready,
Expand Down
4 changes: 2 additions & 2 deletions src/testing/internal/profile/Render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export interface Render<Snapshot> extends BaseRender {
*/
withinDOM: () => SyncScreen;

renderedComponents: string[];
renderedComponents: React.ComponentType[];
}

/** @internal */
Expand All @@ -80,7 +80,7 @@ export class RenderInstance<Snapshot> implements Render<Snapshot> {
baseRender: BaseRender,
public snapshot: Snapshot,
private stringifiedDOM: string | undefined,
public renderedComponents: string[]
public renderedComponents: React.ComponentType[]
) {
this.id = baseRender.id;
this.phase = baseRender.phase;
Expand Down
15 changes: 5 additions & 10 deletions src/testing/internal/profile/profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ interface ProfiledComponentFields<Snapshot> {
}

interface ProfilerContextValue {
renderedComponents: string[];
renderedComponents: React.ComponentType[];
}
const ProfilerContext = React.createContext<ProfilerContextValue | undefined>(
undefined
Expand Down Expand Up @@ -388,22 +388,17 @@ export function profileHook<ReturnValue extends ValidSnapshot, Props>(
);
}

function getCurrentComponentName() {
export function useTrackComponentRender() {
const owner: React.ComponentType | undefined = (React as any)
.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED?.ReactCurrentOwner
?.current?.elementType;
if (owner) return owner?.displayName || owner?.name;

try {
throw new Error();
} catch (e) {
return (e as Error).stack?.split("\n")[1].split(":")[0] || "";
if (!owner) {
throw new Error("useTrackComponentRender: Unable to determine hook owner");
}
}

export function useTrackComponentRender(name = getCurrentComponentName()) {
const ctx = React.useContext(ProfilerContext);
React.useLayoutEffect(() => {
ctx?.renderedComponents.unshift(name);
ctx?.renderedComponents.unshift(owner);
});
}

0 comments on commit 1e64614

Please sign in to comment.