-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Expand file tree
/
Copy pathhook-useSubmit-test.ts
More file actions
135 lines (120 loc) · 4.42 KB
/
Copy pathhook-useSubmit-test.ts
File metadata and controls
135 lines (120 loc) · 4.42 KB
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
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";
test.describe("`useSubmit()` returned function", () => {
let fixture: Fixture;
let appFixture: AppFixture;
test.beforeAll(async () => {
fixture = await createFixture({
files: {
"app/routes/_index.tsx": js`
import { useLoaderData, useSubmit } from "react-router";
export function loader({ request }) {
let url = new URL(request.url);
return url.searchParams.toString()
}
export default function Index() {
let submit = useSubmit();
let handleClick = event => {
event.preventDefault()
submit(event.nativeEvent.submitter || event.currentTarget)
}
let data = useLoaderData();
return (
<form>
<input type="text" name="tasks" defaultValue="first" />
<input type="text" name="tasks" defaultValue="second" />
<button onClick={handleClick} name="tasks" value="third">
Prepare Third Task
</button>
<pre>{data}</pre>
</form>
)
}
`,
"app/routes/action.tsx": js`
import { useActionData, useSubmit } from "react-router";
export async function action({ request }) {
let contentType = request.headers.get('Content-Type');
if (contentType.includes('application/json')) {
return { value: await request.json() };
}
if (contentType.includes('text/plain')) {
return { value: await request.text() };
}
let fd = await request.formData();
return { value: new URLSearchParams(fd.entries()).toString() }
}
export default function Component() {
let submit = useSubmit();
let data = useActionData();
return (
<>
<button id="submit-json" onClick={() => submit(
{ key: 'value' },
{ method: 'post', encType: 'application/json' },
)}>
Submit JSON
</button>
<button id="submit-text" onClick={() => submit(
"raw text",
{ method: 'post', encType: 'text/plain' },
)}>
Submit Text
</button>
<button id="submit-formData" onClick={() => submit(
{ key: 'value' },
{ method: 'post' },
)}>
Submit FrmData
</button>
{data ? <p id="action-data">data: {JSON.stringify(data)}</p> : null}
</>
);
}
`,
},
});
appFixture = await createAppFixture(fixture);
});
test.afterAll(() => {
appFixture.close();
});
test("submits the submitter's value appended to the form data", async ({
page,
}) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/");
await app.clickElement("text=Prepare Third Task");
await page.waitForLoadState("load");
expect(await app.getHtml("pre")).toBe(
`<pre>tasks=first&tasks=second&tasks=third</pre>`,
);
});
test("submits json data", async ({ page }) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/action", true);
await app.clickElement("#submit-json");
await page.waitForSelector("#action-data");
expect(await app.getHtml()).toMatch('data: {"value":{"key":"value"}}');
});
test("submits text data", async ({ page }) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/action", true);
await app.clickElement("#submit-text");
await page.waitForSelector("#action-data");
expect(await app.getHtml()).toMatch('data: {"value":"raw text"}');
});
test("submits form data", async ({ page }) => {
let app = new PlaywrightFixture(appFixture, page);
await app.goto("/action", true);
await app.clickElement("#submit-formData");
await page.waitForSelector("#action-data");
expect(await app.getHtml()).toMatch('data: {"value":"key=value"}');
});
});