-
-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathdefer-loader-test.ts
110 lines (100 loc) · 3.21 KB
/
defer-loader-test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { test, expect } from "@playwright/test";
import {
createAppFixture,
createFixture,
js,
} from "./helpers/create-fixture.js";
import type { Fixture, AppFixture } from "./helpers/create-fixture.js";
import { PlaywrightFixture } from "./helpers/playwright-fixture.js";
let fixture: Fixture;
let appFixture: AppFixture;
test.describe("deferred loaders", () => {
test.beforeAll(async () => {
fixture = await createFixture({
files: {
"app/routes/_index.tsx": js`
import { useLoaderData, Link } from "react-router";
export default function Index() {
return (
<div>
<Link to="/redirect">Redirect</Link>
<Link to="/direct-promise-access">Direct Promise Access</Link>
</div>
)
}
`,
"app/routes/redirect.tsx": js`
import { data } from 'react-router';
export function loader() {
return data(
{ food: "pizza" },
{
status: 301,
headers: {
Location: "/?redirected"
}
}
);
}
export default function Redirect() {
return null;
}
`,
"app/routes/direct-promise-access.tsx": js`
import * as React from "react";
import { useLoaderData, Link, Await } from "react-router";
export function loader() {
return {
bar: new Promise(async (resolve, reject) => {
resolve("hamburger");
}),
};
}
let count = 0;
export default function Index() {
let {bar} = useLoaderData();
React.useEffect(() => {
let aborted = false;
bar.then((data) => {
if (aborted) return;
document.getElementById("content").innerHTML = data + " " + (++count);
document.getElementById("content").setAttribute("data-done", "");
});
return () => {
aborted = true;
};
}, [bar]);
return (
<div id="content">
Waiting for client hydration....
</div>
)
}
`,
},
});
appFixture = await createAppFixture(fixture);
});
test.afterAll(async () => appFixture.close());
test("deferred response can redirect on document request", async ({
page,
}) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/redirect");
await page.waitForURL(/\?redirected/);
});
test("deferred response can redirect on transition", async ({ page }) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/");
await app.clickLink("/redirect");
await page.waitForURL(/\?redirected/);
});
test("can directly access result from deferred promise on document request", async ({
page,
}) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/direct-promise-access");
let element = await page.waitForSelector("[data-done]");
expect(await element.innerText()).toMatch("hamburger 1");
});
});