forked from remix-run/remix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetcher-test.ts
145 lines (125 loc) · 4.35 KB
/
fetcher-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
import { test, expect } from "@playwright/test";
import { createAppFixture, createFixture, js } from "./helpers/create-fixture";
import type { Fixture, AppFixture } from "./helpers/create-fixture";
import { PlaywrightFixture } from "./helpers/playwright-fixture";
test.describe("useFetcher", () => {
let fixture: Fixture;
let appFixture: AppFixture;
let CHEESESTEAK = "CHEESESTEAK";
let LUNCH = "LUNCH";
test.beforeAll(async () => {
fixture = await createFixture({
files: {
"app/routes/resource-route-action-only.ts": js`
import { json } from "@remix-run/node";
export function action() {
return json("${CHEESESTEAK}");
}
`,
"app/routes/fetcher-action-only-call.tsx": js`
import { useFetcher } from "@remix-run/react";
export default function FetcherActionOnlyCall() {
let fetcher = useFetcher();
let executeFetcher = () => {
fetcher.submit(new URLSearchParams(), {
method: 'post',
action: '/resource-route-action-only',
});
};
return (
<>
<button id="fetcher-submit" onClick={executeFetcher}>Click Me</button>
{fetcher.data && <pre>{fetcher.data}</pre>}
</>
);
}
`,
"app/routes/resource-route.jsx": js`
export function loader() {
return "${LUNCH}";
}
export function action() {
return "${CHEESESTEAK}";
}
`,
"app/routes/index.jsx": js`
import { useFetcher } from "@remix-run/react";
export default function Index() {
let fetcher = useFetcher();
return (
<>
<fetcher.Form action="/resource-route">
<button type="submit" formMethod="get">get</button>
<button type="submit" formMethod="post">post</button>
</fetcher.Form>
<button id="fetcher-load" type="button" onClick={() => {
fetcher.load('/resource-route');
}}>
load
</button>
<button id="fetcher-submit" type="button" onClick={() => {
fetcher.submit(new URLSearchParams(), {
method: 'post',
action: '/resource-route'
});
}}>
submit
</button>
<pre>{fetcher.data}</pre>
</>
);
}
`,
},
});
appFixture = await createAppFixture(fixture);
});
test.afterAll(async () => {
await appFixture.close();
});
test.describe("No JavaScript", () => {
test.use({ javaScriptEnabled: false });
test("Form can hit a loader", async ({ page }) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/");
await Promise.all([
page.waitForNavigation(),
app.clickSubmitButton("/resource-route", {
wait: false,
method: "get",
}),
]);
expect(await app.getHtml("pre")).toMatch(LUNCH);
});
test("Form can hit an action", async ({ page }) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/");
await Promise.all([
page.waitForNavigation({ waitUntil: "load" }),
app.clickSubmitButton("/resource-route", {
wait: false,
method: "post",
}),
]);
expect(await app.getHtml("pre")).toMatch(CHEESESTEAK);
});
});
test("load can hit a loader", async ({ page }) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/");
await app.clickElement("#fetcher-load");
expect(await app.getHtml("pre")).toMatch(LUNCH);
});
test("submit can hit an action", async ({ page }) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/");
await app.clickElement("#fetcher-submit");
expect(await app.getHtml("pre")).toMatch(CHEESESTEAK);
});
test("submit can hit an action only route", async ({ page }) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/fetcher-action-only-call");
await app.clickElement("#fetcher-submit");
expect(await app.getHtml("pre")).toMatch(CHEESESTEAK);
});
});