|
| 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 | +} |
0 commit comments