forked from remix-run/remix
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaction-test.ts
186 lines (152 loc) · 5.54 KB
/
action-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
import { test, expect } from "@playwright/test";
import { createFixture, createAppFixture, js } from "./helpers/create-fixture";
import type { Fixture, AppFixture } from "./helpers/create-fixture";
import { PlaywrightFixture, selectHtml } from "./helpers/playwright-fixture";
test.describe("actions", () => {
let fixture: Fixture;
let appFixture: AppFixture;
let FIELD_NAME = "message";
let WAITING_VALUE = "Waiting...";
let SUBMITTED_VALUE = "Submission";
let THROWS_REDIRECT = "redirect-throw";
let REDIRECT_TARGET = "page";
let PAGE_TEXT = "PAGE_TEXT";
test.beforeAll(async () => {
fixture = await createFixture({
config: {
future: {
v2_routeConvention: true,
},
},
files: {
"app/routes/urlencoded.jsx": js`
import { Form, useActionData } from "@remix-run/react";
export let action = async ({ request }) => {
let formData = await request.formData();
return formData.get("${FIELD_NAME}");
};
export default function Actions() {
let data = useActionData()
return (
<Form method="post" id="form">
<p id="text">
{data ? <span id="action-text">{data}</span> : "${WAITING_VALUE}"}
</p>
<p>
<input type="text" defaultValue="${SUBMITTED_VALUE}" name="${FIELD_NAME}" />
<button type="submit" id="submit">Go</button>
</p>
</Form>
);
}
`,
"app/routes/request-text.jsx": js`
import { Form, useActionData } from "@remix-run/react";
export let action = async ({ request }) => {
let text = await request.text();
return text;
};
export default function Actions() {
let data = useActionData()
return (
<Form method="post" id="form">
<p id="text">
{data ? <span id="action-text">{data}</span> : "${WAITING_VALUE}"}
</p>
<p>
<input name="a" defaultValue="1" />
<input name="b" defaultValue="2" />
<button type="submit" id="submit">Go</button>
</p>
</Form>
);
}
`,
[`app/routes/${THROWS_REDIRECT}.jsx`]: js`
import { redirect } from "@remix-run/node";
import { Form } from "@remix-run/react";
export function action() {
throw redirect("/${REDIRECT_TARGET}")
}
export default function () {
return (
<Form method="post">
<button type="submit">Go</button>
</Form>
)
}
`,
[`app/routes/${REDIRECT_TARGET}.jsx`]: js`
export default function () {
return <div id="${REDIRECT_TARGET}">${PAGE_TEXT}</div>
}
`,
},
});
appFixture = await createAppFixture(fixture);
});
test.afterAll(() => {
appFixture.close();
});
let logs: string[] = [];
test.beforeEach(({ page }) => {
page.on("console", (msg) => {
logs.push(msg.text());
});
});
test.afterEach(() => {
expect(logs).toHaveLength(0);
});
test("is not called on document GET requests", async () => {
let res = await fixture.requestDocument("/urlencoded");
let html = selectHtml(await res.text(), "#text");
expect(html).toMatch(WAITING_VALUE);
});
test("is called on document POST requests", async () => {
let FIELD_VALUE = "cheeseburger";
let params = new URLSearchParams();
params.append(FIELD_NAME, FIELD_VALUE);
let res = await fixture.postDocument("/urlencoded", params);
let html = selectHtml(await res.text(), "#text");
expect(html).toMatch(FIELD_VALUE);
});
test("is called on script transition POST requests", async ({ page }) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto(`/urlencoded`);
await page.waitForSelector(`#text:has-text("${WAITING_VALUE}")`);
await page.click("button[type=submit]");
await page.waitForSelector("#action-text");
await page.waitForSelector(`#text:has-text("${SUBMITTED_VALUE}")`);
});
test("properly encodes form data for request.text() usage", async ({
page,
}) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto(`/request-text`);
await page.waitForSelector(`#text:has-text("${WAITING_VALUE}")`);
await page.click("button[type=submit]");
await page.waitForSelector("#action-text");
expect(await app.getHtml("#action-text")).toBe(
'<span id="action-text">a=1&b=2</span>'
);
});
test("redirects a thrown response on document requests", async () => {
let params = new URLSearchParams();
let res = await fixture.postDocument(`/${THROWS_REDIRECT}`, params);
expect(res.status).toBe(302);
expect(res.headers.get("Location")).toBe(`/${REDIRECT_TARGET}`);
});
test("redirects a thrown response on script transitions", async ({
page,
}) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto(`/${THROWS_REDIRECT}`);
let responses = app.collectDataResponses();
await app.clickSubmitButton(`/${THROWS_REDIRECT}`);
await page.waitForSelector(`#${REDIRECT_TARGET}`);
expect(responses.length).toBe(1);
expect(responses[0].status()).toBe(204);
expect(new URL(page.url()).pathname).toBe(`/${REDIRECT_TARGET}`);
expect(await app.getHtml()).toMatch(PAGE_TEXT);
});
});