Skip to content

Migrate Fog of War from Remix 2.10.0 #11735

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

Merged
merged 8 commits into from
Jun 27, 2024
761 changes: 761 additions & 0 deletions integration/fog-of-war-test.ts

Large diffs are not rendered by default.

8 changes: 6 additions & 2 deletions integration/link-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,10 @@ test.describe("route module link export", () => {
});

test.describe("script imports", () => {
// Disable JS for this test since we don't want it to hydrate and remove
// the initial <script> tags
test.use({ javaScriptEnabled: false });

test("are added to the document", async ({ page }) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/");
Expand All @@ -605,8 +609,8 @@ test.describe("route module link export", () => {
let moduleScriptText = await moduleScript.innerText();
expect(
Array.from(moduleScriptText.matchAll(/import "\/assets\/manifest-/g)),
"invalid build manifest"
).toHaveLength(1);
"did not expect a manifest due to fog of war"
).toHaveLength(0);
expect(
Array.from(moduleScriptText.matchAll(/import \* as route0 from "/g)),
"invalid route0"
Expand Down
93 changes: 93 additions & 0 deletions integration/prefetch-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,99 @@ test.describe.skip("multi fetch", () => {
});
});

test.describe("prefetch=render (fog of war)", () => {
let appFixture: AppFixture;

test.afterAll(() => {
appFixture?.close();
});

test("adds prefetch tags after discovery", async ({ page }) => {
let fixture = await createFixture({
files: {
"app/root.tsx": js`
import * as React from "react";
import {
Link,
Links,
Meta,
Outlet,
Scripts,
useLoaderData,
} from "react-router";
export default function Root() {
let [discover, setDiscover] = React.useState(false);
return (
<html lang="en">
<head>
<Meta />
<Links />
</head>
<body>
<nav id="nav">
<Link
to="/with-loader"
discover={discover ? "render" : "none"}
prefetch="render">
Loader Page
</Link>
<br/>
<button onClick={() => setDiscover(true)}>
Discover Link
</button>
</nav>
<Outlet />
<Scripts />
</body>
</html>
);
}
`,

"app/routes/_index.tsx": js`
export default function() {
return <h2 className="index">Index</h2>;
}
`,

"app/routes/with-loader.tsx": js`
export function loader() {
return { message: 'data from the loader' };
}
export default function() {
return <h2 className="with-loader">With Loader</h2>;
}
`,
},
});
appFixture = await createAppFixture(fixture);
let app = new PlaywrightFixture(appFixture, page);

let consoleLogs: string[] = [];
page.on("console", (msg) => {
consoleLogs.push(msg.text());
});

let selectors = {
data: "#nav link[href='/with-loader.data']",
route: "#nav link[href^='/build/routes/with-loader-']",
};
await app.goto("/", true);
expect(await app.page.$(selectors.data)).toBeNull();
expect(await app.page.$(selectors.route)).toBeNull();
expect(consoleLogs).toEqual([
"Tried to prefetch /with-loader but no routes matched.",
]);

await app.clickElement("button");
await page.waitForSelector(selectors.data, { state: "attached" });
await page.waitForSelector(selectors.route, { state: "attached" });

// Ensure no other links in the #nav element
expect(await page.locator("#nav link").count()).toBe(2);
});
});

test.describe("prefetch=intent (hover)", () => {
let fixture: Fixture;
let appFixture: AppFixture;
Expand Down
40 changes: 24 additions & 16 deletions packages/react-router/__tests__/dom/data-browser-router-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2152,6 +2152,7 @@ function testDomRouter(
"<div>
<form
action="/base/path"
data-discover="true"
method="get"
>
<p>
Expand All @@ -2178,6 +2179,7 @@ function testDomRouter(
"<div>
<form
action="/base/path?a=1&b=2"
data-discover="true"
method="get"
>
<p>
Expand Down Expand Up @@ -2233,6 +2235,7 @@ function testDomRouter(
"<div>
<form
action="/base/path"
data-discover="true"
method="post"
>
<p>
Expand Down Expand Up @@ -2260,6 +2263,7 @@ function testDomRouter(
"<div>
<form
action="/base/path"
data-discover="true"
method="post"
>
<p>
Expand Down Expand Up @@ -2417,23 +2421,24 @@ function testDomRouter(
fireEvent.click(screen.getByText("Submit"));
await waitFor(() => screen.getByText("Yes"));
expect(getHtml(container)).toMatchInlineSnapshot(`
"<div>
<form
action="/foo"
method="post"
"<div>
<form
action="/foo"
data-discover="true"
method="post"
>
<p>
Yes
</p>
<button
formaction="/foo/bar"
type="submit"
>
<p>
Yes
</p>
<button
formaction="/foo/bar"
type="submit"
>
Submit
</button>
</form>
</div>"
`);
Submit
</button>
</form>
</div>"
`);
});

it("supports uppercase form method attributes", async () => {
Expand Down Expand Up @@ -5073,6 +5078,7 @@ function testDomRouter(
"<div>
<form
action="/fetch"
data-discover="true"
method="post"
>
<button
Expand Down Expand Up @@ -6726,6 +6732,7 @@ function testDomRouter(
</p>
<form
action="/base/fetch"
data-discover="true"
method="post"
>
<button
Expand All @@ -6746,6 +6753,7 @@ function testDomRouter(
</p>
<form
action="/base/fetch"
data-discover="true"
method="post"
>
<button
Expand Down
24 changes: 15 additions & 9 deletions packages/react-router/__tests__/dom/data-static-router-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe("A <StaticRouterProvider>", () => {
</React.StrictMode>
);
expect(html).toMatch("<h1>👋</h1>");
expect(html).toMatch('<a href="/the/other/path">');
expect(html).toMatch('<a href="/the/other/path" data-discover="true">');

// @ts-expect-error
expect(hooksData1.location).toEqual({
Expand Down Expand Up @@ -243,7 +243,7 @@ describe("A <StaticRouterProvider>", () => {
</React.StrictMode>
);
expect(html).toMatch("<h1>👋</h1>");
expect(html).toMatch('<a href="/the/other/path">');
expect(html).toMatch('<a href="/the/other/path" data-discover="true">');

// @ts-expect-error
expect(hooksData1.location).toEqual({
Expand Down Expand Up @@ -355,7 +355,9 @@ describe("A <StaticRouterProvider>", () => {
</React.StrictMode>
);
expect(html).toMatch("<h1>👋</h1>");
expect(html).toMatch('<a href="/base/the/other/path">');
expect(html).toMatch(
'<a href="/base/the/other/path" data-discover="true">'
);

// @ts-expect-error
expect(location).toEqual({
Expand Down Expand Up @@ -528,7 +530,9 @@ describe("A <StaticRouterProvider>", () => {
/>
</React.StrictMode>
);
expect(html).toContain('<a href="/path/with%20space">👋</a>');
expect(html).toContain(
'<a href="/path/with%20space" data-discover="true">👋</a>'
);
});

it("does not encode user-specified <a href> values", async () => {
Expand All @@ -551,7 +555,9 @@ describe("A <StaticRouterProvider>", () => {
/>
</React.StrictMode>
);
expect(html).toContain('<a href="/path/with space">👋</a>');
expect(html).toContain(
'<a href="/path/with space" data-discover="true">👋</a>'
);
});

it("encodes auto-generated <form action> values to avoid hydration errors (action=undefined)", async () => {
Expand All @@ -573,7 +579,7 @@ describe("A <StaticRouterProvider>", () => {
</React.StrictMode>
);
expect(html).toContain(
'<form method="get" action="/path/with%20space">👋</form>'
'<form method="get" action="/path/with%20space" data-discover="true">👋</form>'
);
});

Expand All @@ -598,7 +604,7 @@ describe("A <StaticRouterProvider>", () => {
</React.StrictMode>
);
expect(html).toContain(
'<form method="get" action="/path/with%20space">👋</form>'
'<form method="get" action="/path/with%20space" data-discover="true">👋</form>'
);
});

Expand All @@ -623,7 +629,7 @@ describe("A <StaticRouterProvider>", () => {
</React.StrictMode>
);
expect(html).toContain(
'<form method="get" action="/path/with space">👋</form>'
'<form method="get" action="/path/with space" data-discover="true">👋</form>'
);
});

Expand Down Expand Up @@ -1259,7 +1265,7 @@ describe("A <StaticRouterProvider>", () => {
</React.StrictMode>
);
expect(html).toMatch(
'<a href="/the/path">relative path</a>' +
'<a href="/the/path" data-discover="true">relative path</a>' +
'<a href="http://localhost/the/path">absolute same-origin url</a>' +
'<a href="https://remix.run">absolute different-origin url</a>' +
'<a href="mailto:[email protected]">absolute mailto: url</a>'
Expand Down
4 changes: 4 additions & 0 deletions packages/react-router/__tests__/dom/link-push-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ describe("Link push and replace", () => {
Home
</h1>
<a
data-discover="true"
href="/home?name=michael"
onClick={[Function]}
>
Expand Down Expand Up @@ -151,6 +152,7 @@ describe("Link push and replace", () => {
Home
</h1>
<a
data-discover="true"
href="/home#bio"
onClick={[Function]}
>
Expand Down Expand Up @@ -210,6 +212,7 @@ describe("Link push and replace", () => {
Home
</h1>
<a
data-discover="true"
href="/home"
onClick={[Function]}
>
Expand Down Expand Up @@ -271,6 +274,7 @@ describe("Link push and replace", () => {
Home
</h1>
<a
data-discover="true"
href="/home"
onClick={[Function]}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@ describe("special character tests", () => {
expect(getHtml(ctx.container)).toMatchInlineSnapshot(`
"<div>
<a
data-discover="true"
href="/parent/child/%20%20param%20%20/grandchild"
>
Link to grandchild
Expand All @@ -551,6 +552,7 @@ describe("special character tests", () => {
expect(getHtml(ctx.container)).toMatchInlineSnapshot(`
"<div>
<a
data-discover="true"
href="/parent/child/%20%20param%20%20/grandchild"
>
Link to grandchild
Expand Down
Loading