Skip to content

Commit 97d2947

Browse files
authored
test(fullstack): test react-router (#1188)
1 parent 7f4a4cb commit 97d2947

File tree

3 files changed

+123
-2
lines changed

3 files changed

+123
-2
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import { type Page, expect, test } from "@playwright/test";
2+
import { type Fixture, useFixture } from "./fixture";
3+
import { expectNoReload, waitForHydration } from "./helper";
4+
5+
test.describe("dev", () => {
6+
const f = useFixture({ root: "examples/react-router", mode: "dev" });
7+
defineTest(f);
8+
});
9+
10+
test.describe("build", () => {
11+
const f = useFixture({ root: "examples/react-router", mode: "build" });
12+
defineTest(f);
13+
});
14+
15+
function defineTest(f: Fixture) {
16+
test("basic", async ({ page }) => {
17+
await page.goto(f.url());
18+
await using _ = await expectNoReload(page);
19+
20+
const errors: Error[] = [];
21+
page.on("pageerror", (error) => {
22+
errors.push(error);
23+
});
24+
25+
// hydration
26+
await waitForHydration(page);
27+
expect(errors).toEqual([]); // no hydration mismatch
28+
29+
await testClient(page);
30+
await testCss(page);
31+
await testNavigation(page);
32+
33+
expect(errors).toEqual([]);
34+
});
35+
36+
test.describe(() => {
37+
test.use({ javaScriptEnabled: false });
38+
39+
test("ssr", async ({ page }) => {
40+
await page.goto(f.url());
41+
await expect(page.locator(".counter-card")).toContainText("Count: 0");
42+
await testCss(page);
43+
if (f.mode === "build") {
44+
await expect(
45+
page.locator("link[rel='modulepreload']").first(),
46+
).toBeAttached();
47+
}
48+
});
49+
});
50+
51+
if (f.mode === "dev") {
52+
test("react hmr", async ({ page }) => {
53+
await page.goto(f.url());
54+
await waitForHydration(page);
55+
await using _ = await expectNoReload(page);
56+
57+
await testClient(page);
58+
59+
const jsFile = f.createEditor("src/routes/index.tsx");
60+
jsFile.edit((s) => s.replace("Count:", "Count-edit:"));
61+
62+
await expect(page.locator(".counter-card")).toContainText(
63+
"Count-edit: 1",
64+
);
65+
66+
jsFile.reset();
67+
await expect(page.locator(".counter-card")).toContainText("Count: 1");
68+
69+
await testCss(page);
70+
});
71+
72+
test("hmr css", async ({ page }) => {
73+
await page.goto(f.url());
74+
await waitForHydration(page);
75+
await using _ = await expectNoReload(page);
76+
77+
await testClient(page);
78+
79+
// scoped css
80+
const cssFile = f.createEditor("src/routes/index.css");
81+
cssFile.edit((s) =>
82+
s.replace("color: rgb(100, 108, 255);", "color: rgb(0, 0, 255);"),
83+
);
84+
await expect(
85+
page.getByRole("heading", { name: "React Router Custom Framework" }),
86+
).toHaveCSS("color", "rgb(0, 0, 255)");
87+
cssFile.reset();
88+
89+
// css is restored
90+
await testCss(page);
91+
});
92+
}
93+
}
94+
95+
async function testClient(page: Page) {
96+
await expect(page.locator(".counter-card")).toContainText("Count: 0");
97+
await page.getByRole("button", { name: "Increment" }).click();
98+
await expect(page.locator(".counter-card")).toContainText("Count: 1");
99+
}
100+
101+
async function testCss(page: Page) {
102+
// styles.css
103+
await expect(page.getByRole("button", { name: "Increment" })).toHaveCSS(
104+
"background-color",
105+
"rgb(83, 91, 242)",
106+
);
107+
// index.css
108+
await expect(
109+
page.getByRole("heading", { name: "React Router Custom Framework" }),
110+
).toHaveCSS("color", "rgb(100, 108, 255)");
111+
}
112+
113+
async function testNavigation(page: Page) {
114+
await page.getByRole("link", { name: "About" }).click();
115+
await page.waitForURL("**/about");
116+
await expect(page.getByRole("heading", { name: "About" })).toBeVisible();
117+
}

packages/fullstack/examples/react-router/src/routes/index.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@
1717
text-align: center;
1818
margin-bottom: 2rem;
1919
}
20+
21+
.hero h1 {
22+
color: rgb(100, 108, 255);
23+
}

packages/fullstack/examples/react-router/src/styles.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ h1 {
5959
}
6060

6161
button {
62-
background: #646cff;
62+
background: rgb(83, 91, 242);
6363
color: white;
6464
border: none;
6565
padding: 0.5rem 1rem;
@@ -76,4 +76,4 @@ button:hover {
7676
color: #666;
7777
font-size: 1.1rem;
7878
margin-bottom: 2rem;
79-
}
79+
}

0 commit comments

Comments
 (0)