Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(react-server): test component hmr #55

Merged
merged 4 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions examples/react-server/e2e/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,57 @@ test("client-component", async ({ page }) => {
await page.getByTestId("client-component").getByText("Count: 1").click();
});

test("client hmr @dev", async ({ page }) => {
usePageErrorChecker(page);
await page.goto("/");
await waitForHydration(page);

await using editor = await createEditor("src/routes/_client.tsx");
await using _ = await createReloadChecker(page);

await page.getByRole("heading", { name: "Hello Client Component" }).click();
await editor.edit((s) =>
s.replace("Hello Client Component", "Hello [EDIT] Client Component"),
);
await page
.getByRole("heading", { name: "Hello [EDIT] Client Component" })
.click();
});

test("server hmr @dev", async ({ page }) => {
usePageErrorChecker(page);
await page.goto("/");
await waitForHydration(page);

await using editor = await createEditor("src/routes/page.tsx");
await using _ = await createReloadChecker(page);

await page.getByRole("heading", { name: "Hello Server Component" }).click();
await editor.edit((s) =>
s.replace("Hello Server Component", "Hello [EDIT] Server Component"),
);
await page
.getByRole("heading", { name: "Hello [EDIT] Server Component" })
.click();
});

test("shared hmr @dev", async ({ page }) => {
usePageErrorChecker(page);
await page.goto("/");
await waitForHydration(page);

await using editor = await createEditor("src/routes/_shared.tsx");
await using _ = await createReloadChecker(page);

await page.getByText("Shared Component (server)").click();
await page.getByText("Shared Component (client)").click();
await editor.edit((s) =>
s.replace("Shared Component", "Shared [EDIT] Component"),
);
await page.getByText("Shared [EDIT] Component (server)").click();
await page.getByText("Shared [EDIT] Component (client)").click();
});

test("server-action @js", async ({ page }) => {
usePageErrorChecker(page);
await page.goto("/");
Expand Down
2 changes: 2 additions & 0 deletions examples/react-server/e2e/helper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from "fs";
import { sleep } from "@hiogawa/utils";
import { type Page, expect, test } from "@playwright/test";

export const testNoJs = test.extend({
Expand Down Expand Up @@ -44,6 +45,7 @@ export async function createReloadChecker(page: Page) {
}

async function check() {
await sleep(300);
await expect(page.locator(`meta[name="x-reload-check"]`)).toBeAttached({
timeout: 1,
});
Expand Down
10 changes: 10 additions & 0 deletions examples/react-server/src/__snapshots__/basic.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ exports[`basic 1`] = `
<h4>
Hello Server Component
</h4>
<div>
Shared Component (
server
)
</div>
<div
data-testid="server-action"
>
Expand Down Expand Up @@ -77,6 +82,11 @@ exports[`basic 1`] = `
<h4>
Hello Client Component
</h4>
<div>
Shared Component (
client
)
</div>
<div
data-hydrated="true"
>
Expand Down
1 change: 1 addition & 0 deletions examples/react-server/src/routes/_client.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

.client-btn {
background-color: rgb(255, 220, 220);
cursor: pointer;
font-size: 1rem;
font-family: inherit;
border: none;
Expand Down
2 changes: 2 additions & 0 deletions examples/react-server/src/routes/_client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import "./_client.css";
import React from "react";
import { checkAnswer } from "./_action";
import { SharedComponent } from "./_shared";

export function ClientComponent() {
const [count, setCount] = React.useState(0);
Expand All @@ -15,6 +16,7 @@ export function ClientComponent() {
return (
<div data-testid="client-component">
<h4>Hello Client Component</h4>
<SharedComponent message="client" />
<div data-hydrated={hydrated}>hydrated: {String(hydrated)}</div>
<div>Count: {count}</div>
<button className="client-btn" onClick={() => setCount((v) => v - 1)}>
Expand Down
1 change: 1 addition & 0 deletions examples/react-server/src/routes/_server.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

.server-btn {
background-color: rgb(220, 220, 255);
cursor: pointer;
font-size: 1rem;
font-family: inherit;
border: none;
Expand Down
3 changes: 3 additions & 0 deletions examples/react-server/src/routes/_shared.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function SharedComponent(props: { message: string }) {
return <div>Shared Component ({props.message})</div>;
}
2 changes: 2 additions & 0 deletions examples/react-server/src/routes/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "./_server.css";
import { changeCounter, getCounter } from "./_action";
import { ClientComponent, UseActionStateDemo } from "./_client";
import { SharedComponent } from "./_shared";

export default async function Layout() {
return (
Expand All @@ -24,6 +25,7 @@ async function Page() {
return (
<div>
<h4>Hello Server Component</h4>
<SharedComponent message="server" />
<ServerActionDemo />
<ClientComponent />
</div>
Expand Down