forked from remix-run/remix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransition-test.ts
212 lines (175 loc) · 5.82 KB
/
transition-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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { createAppFixture, createFixture, js } from "./helpers/create-fixture";
import type { Fixture, AppFixture } from "./helpers/create-fixture";
describe("rendering", () => {
let fixture: Fixture;
let app: AppFixture;
const PAGE = "page";
const PAGE_TEXT = "PAGE_TEXT";
const PAGE_INDEX_TEXT = "PAGE_INDEX_TEXT";
const CHILD = "child";
const CHILD_TEXT = "CHILD_TEXT";
const REDIRECT = "redirect";
const REDIRECT_TARGET = "page";
beforeAll(async () => {
fixture = await createFixture({
files: {
"app/root.jsx": js`
import { Outlet, Scripts } from "remix";
export default function Root() {
return (
<html>
<head />
<body>
<main>
<Outlet />
</main>
<Scripts />
</body>
</html>
)
}
`,
"app/routes/index.jsx": js`
import { Link } from "remix";
export default function() {
return (
<div>
<h2>Index</h2>
<Link to="/${PAGE}">${PAGE}</Link>
<Link to="/${REDIRECT}">${REDIRECT}</Link>
</div>
);
}
`,
[`app/routes/${PAGE}.jsx`]: js`
import { Outlet, useLoaderData } from "remix";
export function loader() {
return "${PAGE_TEXT}"
}
export default function() {
let text = useLoaderData();
return (
<>
<h2>{text}</h2>
<Outlet />
</>
);
}
`,
[`app/routes/${PAGE}/index.jsx`]: js`
import { useLoaderData, Link } from "remix";
export function loader() {
return "${PAGE_INDEX_TEXT}"
}
export default function() {
let text = useLoaderData();
return (
<>
<h3>{text}</h3>
<Link to="/${PAGE}/${CHILD}">${CHILD}</Link>
</>
);
}
`,
[`app/routes/${PAGE}/${CHILD}.jsx`]: js`
import { useLoaderData } from "remix";
export function loader() {
return "${CHILD_TEXT}"
}
export default function() {
let text = useLoaderData();
return <h3>{text}</h3>;
}
`,
[`app/routes/${REDIRECT}.jsx`]: js`
import { redirect } from "remix";
export function loader() {
return redirect("/${REDIRECT_TARGET}")
}
export default function() {
return null;
}
`,
"app/routes/gh-1691.jsx": js`
import { Form, redirect, useFetcher, useTransition} from "remix";
export const action = async ({ request }) => {
return redirect("/gh-1691");
};
export const loader = async ({ request }) => {
return {};
};
export default function GitHubIssue1691() {
const fetcher = useFetcher();
return (
<div style={{ fontFamily: "system-ui, sans-serif", lineHeight: "1.4" }}>
<span>{fetcher.state}</span>
<fetcher.Form method="post">
<input type="hidden" name="source" value="fetcher" />
<button type="submit" name="_action" value="add">
Submit
</button>
</fetcher.Form>
</div>
);
}
`,
},
});
app = await createAppFixture(fixture);
});
afterAll(async () => {
await app.close();
});
it("calls all loaders for new routes", async () => {
await app.goto("/");
let responses = app.collectDataResponses();
await app.clickLink(`/${PAGE}`);
expect(
responses.map((res) => new URL(res.url()).searchParams.get("_data"))
).toEqual([`routes/${PAGE}`, `routes/${PAGE}/index`]);
let html = await app.getHtml("main");
expect(html).toMatch(PAGE_TEXT);
expect(html).toMatch(PAGE_INDEX_TEXT);
});
it("calls only loaders for changing routes", async () => {
await app.goto(`/${PAGE}`);
let responses = app.collectDataResponses();
await app.clickLink(`/${PAGE}/${CHILD}`);
expect(
responses.map((res) => new URL(res.url()).searchParams.get("_data"))
).toEqual([`routes/${PAGE}/${CHILD}`]);
let html = await app.getHtml("main");
expect(html).toMatch(PAGE_TEXT);
expect(html).toMatch(CHILD_TEXT);
});
test("loader redirect", async () => {
await app.goto("/");
let responses = app.collectDataResponses();
await app.clickLink(`/${REDIRECT}`);
expect(new URL(app.page.url()).pathname).toBe(`/${REDIRECT_TARGET}`);
expect(
responses.map((res) => new URL(res.url()).searchParams.get("_data"))
).toEqual([`routes/${REDIRECT}`, `routes/${PAGE}`, `routes/${PAGE}/index`]);
let html = await app.getHtml("main");
expect(html).toMatch(PAGE_TEXT);
expect(html).toMatch(PAGE_INDEX_TEXT);
});
it("calls changing routes on POP", async () => {
await app.goto(`/${PAGE}`);
await app.clickLink(`/${PAGE}/${CHILD}`);
let responses = app.collectDataResponses();
await app.goBack();
expect(
responses.map((res) => new URL(res.url()).searchParams.get("_data"))
).toEqual([`routes/${PAGE}/index`]);
let html = await app.getHtml("main");
expect(html).toMatch(PAGE_TEXT);
expect(html).toMatch(PAGE_INDEX_TEXT);
});
it("useFetcher state should return to the idle when redirect from an action", async () => {
await app.goto("/gh-1691");
expect(await app.getHtml("span")).toMatch("idle");
await app.clickSubmitButton("/gh-1691");
expect(await app.getHtml("span")).toMatch("idle");
});
});