Skip to content

Commit

Permalink
test: test template mount
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Jun 30, 2024
1 parent b4a8047 commit d958bfd
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 18 deletions.
26 changes: 26 additions & 0 deletions packages/react-server/examples/basic/e2e/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1221,3 +1221,29 @@ test("loading @js", async ({ page }) => {
await expect(page.getByTestId("/test/loading")).toBeVisible();
await page.getByText('params {"id":"1"}').click();
});

test("template @js", async ({ page }) => {
await page.goto("/test/template");
await page.getByText("template.tsx [mount: 1]").click();
await waitForHydration(page);

await page
.getByRole("link", { name: "• /test/template/x", exact: true })
.click();
await page.getByText("template.tsx [mount: 2]").click();
await page.getByText("[p1]/template.tsx [mount: 1]").click();

await page.getByRole("link", { name: "• /test/template/x/a" }).click();
await page.getByText("template.tsx [mount: 2]", { exact: true }).click();
await page.getByText("[p1]/template.tsx [mount: 2]").click();

await page.getByRole("link", { name: "• /test/template/x/b" }).click();
await page.getByText("template.tsx [mount: 2]", { exact: true }).click();
await page.getByText("[p1]/template.tsx [mount: 3]").click();

await page
.getByRole("link", { name: "• /test/template/y", exact: true })
.click();
await page.getByText("template.tsx [mount: 3]").click();
await page.getByText("[p1]/template.tsx [mount: 4]").click();
});
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { ClientTime } from "../../_client";
import { MountCount } from "../../_client";

export default function Template(props: React.PropsWithChildren) {
return (
<div className="border p-2">
<div>
[p1]/[p2]/template.tsx <ClientTime />
</div>
<MountCount name="[p1]/[p2]/template.tsx" />
{props.children}
</div>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { ClientTime } from "../_client";
import { MountCount } from "../_client";

export default function Template(props: React.PropsWithChildren) {
return (
<div className="border p-2">
<div>
[p1]/template.tsx <ClientTime />
</div>
<MountCount name="[p1]/template.tsx" />
{props.children}
</div>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
"use client";

import { defaultDict, once, tinyassert } from "@hiogawa/utils";
import React from "react";

export function ClientTime() {
const now = React.useSyncExternalStore(
React.useCallback(() => () => {}, []),
() => Date.now(),
() => null,
const countMap = defaultDict(() => 0);

export function MountCount(props: { name: string }) {
const elRef = React.useRef<HTMLElement>(null);

React.useEffect(
once(() => {
tinyassert(elRef.current);
elRef.current.textContent = String(++countMap[props.name]);
}),
[],
);

return (
<div>
{props.name} [mount: <span ref={elRef} />]
</div>
);
return <>[now: {now}]</>;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { ClientTime } from "./_client";
import { MountCount } from "./_client";

export default function Template(props: React.PropsWithChildren) {
return (
<div className="border p-2">
<div>
template.tsx <ClientTime />
</div>
<MountCount name="template.tsx" />
{props.children}
</div>
);
Expand Down

0 comments on commit d958bfd

Please sign in to comment.