Skip to content

Commit

Permalink
Add failing test for syncronous blocker.proceed calls (#9914)
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 authored Aug 27, 2024
1 parent 91184a7 commit b62993a
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 35 deletions.
120 changes: 120 additions & 0 deletions integration/blocking-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import { test, expect } from "@playwright/test";

import type { AppFixture, Fixture } from "./helpers/create-fixture.js";
import {
createFixture,
js,
createAppFixture,
} from "./helpers/create-fixture.js";
import { PlaywrightFixture } from "./helpers/playwright-fixture.js";

let fixture: Fixture;
let appFixture: AppFixture;

test.afterAll(() => appFixture.close());

test("handles synchronous proceeding correctly", async ({ page }) => {
fixture = await createFixture({
files: {
"app/routes/_index.tsx": js`
import { Link } from "@remix-run/react";
export default function Component() {
return (
<div>
<h1 id="index">Index</h1>
<Link to="/a">/a</Link>
</div>
)
}
`,
"app/routes/a.tsx": js`
import { Link } from "@remix-run/react";
export default function Component() {
return (
<div>
<h1 id="a">A</h1>
<Link to="/b">/b</Link>
</div>
)
}
`,
"app/routes/b.tsx": js`
import * as React from "react";
import { Form, useAction, useBlocker } from "@remix-run/react";
export default function Component() {
return (
<div>
<h1 id="b">B</h1>
<ImportantForm />
</div>
)
}
function ImportantForm() {
let [value, setValue] = React.useState("");
let shouldBlock = React.useCallback<BlockerFunction>(
({ currentLocation, nextLocation }) =>
value !== "" && currentLocation.pathname !== nextLocation.pathname,
[value]
);
let blocker = useBlocker(shouldBlock);
// Reset the blocker if the user cleans the form
React.useEffect(() => {
if (blocker.state === "blocked") {
blocker.proceed();
}
}, [blocker]);
return (
<>
<p>
Is the form dirty?{" "}
{value !== "" ? (
<span style={{ color: "red" }}>Yes</span>
) : (
<span style={{ color: "green" }}>No</span>
)}
</p>
<Form method="post">
<label>
Enter some important data:
<input
name="data"
value={value}
onChange={(e) => setValue(e.target.value)}
/>
</label>
<button type="submit">Save</button>
</Form>
</>
);
}
`,
},
});

// This creates an interactive app using puppeteer.
appFixture = await createAppFixture(fixture);

let app = new PlaywrightFixture(appFixture, page);

await app.goto("/");
await app.clickLink("/a");
await page.waitForSelector("#a");
await app.clickLink("/b");
await page.waitForSelector("#b");
await page.getByLabel("Enter some important data:").fill("Hello Remix!");

// Going back should:
// - block
// - immediately call blocker.proceed() once we enter the blocked state
// - and land back one history entry (/a)
await page.goBack();
await page.waitForSelector("#a");
expect(await app.getHtml()).toContain("A");
});
2 changes: 1 addition & 1 deletion integration/helpers/create-fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ export async function createFixtureProject(
`);
}
contents = contents.replace(
"global.INJECTED_FIXTURE_REMIX_CONFIG",
/(global|globalThis)\.INJECTED_FIXTURE_REMIX_CONFIG/,
`${serializeJavaScript(init.config ?? {})}`
);
fse.writeFileSync(path.join(projectDir, "remix.config.js"), contents);
Expand Down
2 changes: 1 addition & 1 deletion integration/helpers/deno-template/remix.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ module.exports = {

// !!! Don't adjust this without changing the code that overwrites this
// in createFixtureProject()
...global.INJECTED_FIXTURE_REMIX_CONFIG,
...globalThis.INJECTED_FIXTURE_REMIX_CONFIG,
};
2 changes: 1 addition & 1 deletion integration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@remix-run/dev": "workspace:*",
"@remix-run/express": "workspace:*",
"@remix-run/node": "workspace:*",
"@remix-run/router": "1.19.1",
"@remix-run/router": "0.0.0-experimental-7d87ffb8c",
"@remix-run/server-runtime": "workspace:*",
"@types/express": "^4.17.9",
"@vanilla-extract/css": "^1.10.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/remix-dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@mdx-js/mdx": "^2.3.0",
"@npmcli/package-json": "^4.0.1",
"@remix-run/node": "workspace:*",
"@remix-run/router": "1.19.1",
"@remix-run/router": "0.0.0-experimental-7d87ffb8c",
"@remix-run/server-runtime": "workspace:*",
"@types/mdx": "^2.0.5",
"@vanilla-extract/integration": "^6.2.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/remix-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
"tsc": "tsc"
},
"dependencies": {
"@remix-run/router": "1.19.1",
"@remix-run/router": "0.0.0-experimental-7d87ffb8c",
"@remix-run/server-runtime": "workspace:*",
"react-router": "6.26.1",
"react-router-dom": "6.26.1",
"react-router": "0.0.0-experimental-7d87ffb8c",
"react-router-dom": "0.0.0-experimental-7d87ffb8c",
"turbo-stream": "2.3.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/remix-server-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"tsc": "tsc"
},
"dependencies": {
"@remix-run/router": "1.19.1",
"@remix-run/router": "0.0.0-experimental-7d87ffb8c",
"@types/cookie": "^0.6.0",
"@web3-storage/multipart-parser": "^1.0.0",
"cookie": "^0.6.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/remix-testing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
"dependencies": {
"@remix-run/node": "workspace:*",
"@remix-run/react": "workspace:*",
"@remix-run/router": "1.19.1",
"react-router-dom": "6.26.1"
"@remix-run/router": "0.0.0-experimental-7d87ffb8c",
"react-router-dom": "0.0.0-experimental-7d87ffb8c"
},
"devDependencies": {
"@remix-run/server-runtime": "workspace:*",
Expand Down
50 changes: 25 additions & 25 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b62993a

Please sign in to comment.