@@ -13,23 +13,27 @@ test.beforeAll(async () => {
13
13
"app/routes/index.jsx" : js `
14
14
import { json } from "@remix-run/node";
15
15
import { Form, useLoaderData, useActionData } from "@remix-run/react";
16
- export async function loader({ request }) {
17
- let text = (await request.text());
16
+
17
+ async function requestToJson(request) {
18
+ let body = null;
19
+
20
+ if (request.body) {
21
+ let fd = await request.formData();
22
+ body = Object.fromEntries(fd.entries());
23
+ }
24
+
18
25
return json({
19
26
method: request.method,
20
27
url: request.url,
21
28
headers: Object.fromEntries(request.headers.entries()),
22
- text ,
29
+ body ,
23
30
});
24
31
}
25
- export async function action({ request }) {
26
- let text = (await request.text());
27
- return json({
28
- method: request.method,
29
- url: request.url,
30
- headers: Object.fromEntries(request.headers.entries()),
31
- text,
32
- });
32
+ export async function loader({ request }) {
33
+ return requestToJson(request);
34
+ }
35
+ export function action({ request }) {
36
+ return requestToJson(request);
33
37
}
34
38
export default function Index() {
35
39
let loaderData = useLoaderData();
@@ -42,16 +46,24 @@ test.beforeAll(async () => {
42
46
Set Cookie
43
47
</button>
44
48
<Form method="get" reloadDocument>
45
- <button type="submit" id="submit-get-ssr" name="type" value="ssr" />
49
+ <button type="submit" id="submit-get-ssr" name="type" value="ssr">
50
+ SSR GET
51
+ </button>
46
52
</Form>
47
53
<Form method="get">
48
- <button type="submit" id="submit-get-csr" name="type" value="csr" />
54
+ <button type="submit" id="submit-get-csr" name="type" value="csr">
55
+ CSR GET
56
+ </button>
49
57
</Form>
50
58
<Form method="post" reloadDocument>
51
- <button type="submit" id="submit-post-ssr" name="type" value="ssr" />
59
+ <button type="submit" id="submit-post-ssr" name="type" value="ssr">
60
+ SSR POST
61
+ </button>
52
62
</Form>
53
63
<Form method="post">
54
- <button type="submit" id="submit-post-csr" name="type" value="csr" />
64
+ <button type="submit" id="submit-post-csr" name="type" value="csr">
65
+ CSR POST
66
+ </button>
55
67
</Form>
56
68
<pre id="loader-data">{JSON.stringify(loaderData)}</pre>
57
69
{actionData ?
@@ -78,15 +90,15 @@ test("loader request on SSR GET requests", async ({ page }) => {
78
90
expect ( loaderData . method ) . toEqual ( "GET" ) ;
79
91
expect ( loaderData . url ) . toMatch ( / ^ h t t p : \/ \/ l o c a l h o s t : \d + \/ $ / ) ;
80
92
expect ( loaderData . headers . cookie ) . toEqual ( undefined ) ;
81
- expect ( loaderData . text ) . toEqual ( "" ) ;
93
+ expect ( loaderData . body ) . toEqual ( null ) ;
82
94
83
95
await app . clickElement ( "#submit-get-ssr" ) ;
84
96
85
97
loaderData = JSON . parse ( await page . locator ( "#loader-data" ) . innerHTML ( ) ) ;
86
98
expect ( loaderData . method ) . toEqual ( "GET" ) ;
87
99
expect ( loaderData . url ) . toMatch ( / ^ h t t p : \/ \/ l o c a l h o s t : \d + \/ \? t y p e = s s r $ / ) ;
88
100
expect ( loaderData . headers . cookie ) . toEqual ( "cookie=nomnom" ) ;
89
- expect ( loaderData . text ) . toEqual ( "" ) ;
101
+ expect ( loaderData . body ) . toEqual ( null ) ;
90
102
} ) ;
91
103
92
104
test ( "loader request on CSR GET requests" , async ( { page } ) => {
@@ -98,15 +110,15 @@ test("loader request on CSR GET requests", async ({ page }) => {
98
110
expect ( loaderData . method ) . toEqual ( "GET" ) ;
99
111
expect ( loaderData . url ) . toMatch ( / ^ h t t p : \/ \/ l o c a l h o s t : \d + \/ $ / ) ;
100
112
expect ( loaderData . headers . cookie ) . toEqual ( undefined ) ;
101
- expect ( loaderData . text ) . toEqual ( "" ) ;
113
+ expect ( loaderData . body ) . toEqual ( null ) ;
102
114
103
115
await app . clickElement ( "#submit-get-csr" ) ;
104
116
105
117
loaderData = JSON . parse ( await page . locator ( "#loader-data" ) . innerHTML ( ) ) ;
106
118
expect ( loaderData . method ) . toEqual ( "GET" ) ;
107
119
expect ( loaderData . url ) . toMatch ( / ^ h t t p : \/ \/ l o c a l h o s t : \d + \/ \? t y p e = c s r $ / ) ;
108
120
expect ( loaderData . headers . cookie ) . toEqual ( "cookie=nomnom" ) ;
109
- expect ( loaderData . text ) . toEqual ( "" ) ;
121
+ expect ( loaderData . body ) . toEqual ( null ) ;
110
122
} ) ;
111
123
112
124
test ( "action + loader requests SSR POST requests" , async ( { page } ) => {
@@ -118,21 +130,21 @@ test("action + loader requests SSR POST requests", async ({ page }) => {
118
130
expect ( loaderData . method ) . toEqual ( "GET" ) ;
119
131
expect ( loaderData . url ) . toMatch ( / ^ h t t p : \/ \/ l o c a l h o s t : \d + \/ $ / ) ;
120
132
expect ( loaderData . headers . cookie ) . toEqual ( undefined ) ;
121
- expect ( loaderData . text ) . toEqual ( "" ) ;
133
+ expect ( loaderData . body ) . toEqual ( null ) ;
122
134
123
135
await app . clickElement ( "#submit-post-ssr" ) ;
124
136
125
137
let actionData = JSON . parse ( await page . locator ( "#action-data" ) . innerHTML ( ) ) ;
126
138
expect ( actionData . method ) . toEqual ( "POST" ) ;
127
139
expect ( actionData . url ) . toMatch ( / ^ h t t p : \/ \/ l o c a l h o s t : \d + \/ $ / ) ;
128
140
expect ( actionData . headers . cookie ) . toEqual ( "cookie=nomnom" ) ;
129
- expect ( actionData . text ) . toEqual ( " type= ssr") ;
141
+ expect ( actionData . body ) . toEqual ( { type : " ssr" } ) ;
130
142
131
143
loaderData = JSON . parse ( await page . locator ( "#loader-data" ) . innerHTML ( ) ) ;
132
144
expect ( loaderData . method ) . toEqual ( "GET" ) ;
133
145
expect ( loaderData . url ) . toMatch ( / ^ h t t p : \/ \/ l o c a l h o s t : \d + \/ $ / ) ;
134
146
expect ( loaderData . headers . cookie ) . toEqual ( "cookie=nomnom" ) ;
135
- expect ( loaderData . text ) . toEqual ( "" ) ;
147
+ expect ( loaderData . body ) . toEqual ( null ) ;
136
148
} ) ;
137
149
138
150
test ( "action + loader requests on CSR POST requests" , async ( { page } ) => {
@@ -144,19 +156,19 @@ test("action + loader requests on CSR POST requests", async ({ page }) => {
144
156
expect ( loaderData . method ) . toEqual ( "GET" ) ;
145
157
expect ( loaderData . url ) . toMatch ( / ^ h t t p : \/ \/ l o c a l h o s t : \d + \/ $ / ) ;
146
158
expect ( loaderData . headers . cookie ) . toEqual ( undefined ) ;
147
- expect ( loaderData . text ) . toEqual ( "" ) ;
159
+ expect ( loaderData . body ) . toEqual ( null ) ;
148
160
149
161
await app . clickElement ( "#submit-post-csr" ) ;
150
162
151
163
let actionData = JSON . parse ( await page . locator ( "#action-data" ) . innerHTML ( ) ) ;
152
164
expect ( actionData . method ) . toEqual ( "POST" ) ;
153
165
expect ( actionData . url ) . toMatch ( / ^ h t t p : \/ \/ l o c a l h o s t : \d + \/ $ / ) ;
154
166
expect ( actionData . headers . cookie ) . toEqual ( "cookie=nomnom" ) ;
155
- expect ( actionData . text ) . toEqual ( " type= csr") ;
167
+ expect ( actionData . body ) . toEqual ( { type : " csr" } ) ;
156
168
157
169
loaderData = JSON . parse ( await page . locator ( "#loader-data" ) . innerHTML ( ) ) ;
158
170
expect ( loaderData . method ) . toEqual ( "GET" ) ;
159
171
expect ( loaderData . url ) . toMatch ( / ^ h t t p : \/ \/ l o c a l h o s t : \d + \/ $ / ) ;
160
172
expect ( loaderData . headers . cookie ) . toEqual ( "cookie=nomnom" ) ;
161
- expect ( loaderData . text ) . toEqual ( "" ) ;
173
+ expect ( loaderData . body ) . toEqual ( null ) ;
162
174
} ) ;
0 commit comments