88import type { Fixture , AppFixture } from "./helpers/create-fixture.js" ;
99import { PlaywrightFixture , selectHtml } from "./helpers/playwright-fixture.js" ;
1010
11- test . describe . skip ( "actions" , ( ) => {
11+ test . describe ( "actions" , ( ) => {
1212 let fixture : Fixture ;
1313 let appFixture : AppFixture ;
1414
@@ -21,6 +21,7 @@ test.describe.skip("actions", () => {
2121
2222 test . beforeAll ( async ( ) => {
2323 fixture = await createFixture ( {
24+ singleFetch : true ,
2425 files : {
2526 "app/routes/urlencoded.tsx" : js `
2627 import { Form, useActionData } from "react-router-dom";
@@ -199,226 +200,15 @@ test.describe.skip("actions", () => {
199200 } ) => {
200201 let app = new PlaywrightFixture ( appFixture , page ) ;
201202 await app . goto ( `/${ THROWS_REDIRECT } ` ) ;
202- let responses = app . collectDataResponses ( ) ;
203+ let responses = app . collectSingleFetchResponses ( ) ;
203204 await app . clickSubmitButton ( `/${ THROWS_REDIRECT } ` ) ;
204205
205206 await page . waitForSelector ( `#${ REDIRECT_TARGET } ` ) ;
206207
207208 expect ( responses . length ) . toBe ( 1 ) ;
208- expect ( responses [ 0 ] . status ( ) ) . toBe ( 204 ) ;
209+ expect ( responses [ 0 ] . status ( ) ) . toBe ( 200 ) ;
209210
210211 expect ( new URL ( page . url ( ) ) . pathname ) . toBe ( `/${ REDIRECT_TARGET } ` ) ;
211212 expect ( await app . getHtml ( ) ) . toMatch ( PAGE_TEXT ) ;
212213 } ) ;
213214} ) ;
214-
215- // Duplicate suite of the tests above running with single fetch enabled
216- // TODO(v3): remove the above suite of tests and just keep these
217- test . describe ( "single fetch" , ( ) => {
218- test . describe ( "actions" , ( ) => {
219- let fixture : Fixture ;
220- let appFixture : AppFixture ;
221-
222- let FIELD_NAME = "message" ;
223- let WAITING_VALUE = "Waiting..." ;
224- let SUBMITTED_VALUE = "Submission" ;
225- let THROWS_REDIRECT = "redirect-throw" ;
226- let REDIRECT_TARGET = "page" ;
227- let PAGE_TEXT = "PAGE_TEXT" ;
228-
229- test . beforeAll ( async ( ) => {
230- fixture = await createFixture ( {
231- singleFetch : true ,
232- files : {
233- "app/routes/urlencoded.tsx" : js `
234- import { Form, useActionData } from "react-router-dom";
235-
236- export let action = async ({ request }) => {
237- let formData = await request.formData();
238- return formData.get("${ FIELD_NAME } ");
239- };
240-
241- export default function Actions() {
242- let data = useActionData()
243-
244- return (
245- <Form method="post" id="form">
246- <p id="text">
247- {data ? <span id="action-text">{data}</span> : "${ WAITING_VALUE } "}
248- </p>
249- <p>
250- <input type="text" defaultValue="${ SUBMITTED_VALUE } " name="${ FIELD_NAME } " />
251- <button type="submit" id="submit">Go</button>
252- </p>
253- </Form>
254- );
255- }
256- ` ,
257-
258- "app/routes/request-text.tsx" : js `
259- import { Form, useActionData } from "react-router-dom";
260-
261- export let action = async ({ request }) => {
262- let text = await request.text();
263- return text;
264- };
265-
266- export default function Actions() {
267- let data = useActionData()
268-
269- return (
270- <Form method="post" id="form">
271- <p id="text">
272- {data ? <span id="action-text">{data}</span> : "${ WAITING_VALUE } "}
273- </p>
274- <p>
275- <input name="a" defaultValue="1" />
276- <input name="b" defaultValue="2" />
277- <button type="submit" id="submit">Go</button>
278- </p>
279- </Form>
280- );
281- }
282- ` ,
283-
284- [ `app/routes/${ THROWS_REDIRECT } .jsx` ] : js `
285- import { redirect } from "@react-router/node";
286- import { Form } from "react-router-dom";
287-
288- export function action() {
289- throw redirect("/${ REDIRECT_TARGET } ")
290- }
291-
292- export default function () {
293- return (
294- <Form method="post">
295- <button type="submit">Go</button>
296- </Form>
297- )
298- }
299- ` ,
300-
301- [ `app/routes/${ REDIRECT_TARGET } .jsx` ] : js `
302- export default function () {
303- return <div id="${ REDIRECT_TARGET } ">${ PAGE_TEXT } </div>
304- }
305- ` ,
306-
307- "app/routes/no-action.tsx" : js `
308- import { Form } from "react-router-dom";
309-
310- export default function Component() {
311- return (
312- <Form method="post">
313- <button type="submit">Submit without action</button>
314- </Form>
315- );
316- }
317- ` ,
318- } ,
319- } ) ;
320-
321- appFixture = await createAppFixture ( fixture ) ;
322- } ) ;
323-
324- test . afterAll ( ( ) => {
325- appFixture . close ( ) ;
326- } ) ;
327-
328- let logs : string [ ] = [ ] ;
329-
330- test . beforeEach ( ( { page } ) => {
331- page . on ( "console" , ( msg ) => {
332- logs . push ( msg . text ( ) ) ;
333- } ) ;
334- } ) ;
335-
336- test . afterEach ( ( ) => {
337- expect ( logs ) . toHaveLength ( 0 ) ;
338- } ) ;
339-
340- test ( "is not called on document GET requests" , async ( ) => {
341- let res = await fixture . requestDocument ( "/urlencoded" ) ;
342- let html = selectHtml ( await res . text ( ) , "#text" ) ;
343- expect ( html ) . toMatch ( WAITING_VALUE ) ;
344- } ) ;
345-
346- test ( "is called on document POST requests" , async ( ) => {
347- let FIELD_VALUE = "cheeseburger" ;
348-
349- let params = new URLSearchParams ( ) ;
350- params . append ( FIELD_NAME , FIELD_VALUE ) ;
351-
352- let res = await fixture . postDocument ( "/urlencoded" , params ) ;
353-
354- let html = selectHtml ( await res . text ( ) , "#text" ) ;
355- expect ( html ) . toMatch ( FIELD_VALUE ) ;
356- } ) ;
357-
358- test ( "is called on script transition POST requests" , async ( { page } ) => {
359- let app = new PlaywrightFixture ( appFixture , page ) ;
360- await app . goto ( `/urlencoded` ) ;
361- await page . waitForSelector ( `#text:has-text("${ WAITING_VALUE } ")` ) ;
362-
363- await page . click ( "button[type=submit]" ) ;
364- await page . waitForSelector ( "#action-text" ) ;
365- await page . waitForSelector ( `#text:has-text("${ SUBMITTED_VALUE } ")` ) ;
366- } ) ;
367-
368- test ( "throws a 405 when no action exists" , async ( { page } ) => {
369- let app = new PlaywrightFixture ( appFixture , page ) ;
370- await app . goto ( `/no-action` ) ;
371- await page . click ( "button[type=submit]" ) ;
372- await page . waitForSelector ( `h1:has-text("405 Method Not Allowed")` ) ;
373- expect ( logs . length ) . toBe ( 2 ) ;
374- expect ( logs [ 0 ] ) . toMatch (
375- 'Route "routes/no-action" does not have an action'
376- ) ;
377- // logs[1] is the raw ErrorResponse instance from the boundary but playwright
378- // seems to just log the name of the constructor, which in the minified code
379- // is meaningless so we don't bother asserting
380-
381- // The rest of the tests in this suite assert no logs, so clear this out to
382- // avoid failures in afterEach
383- logs = [ ] ;
384- } ) ;
385-
386- test ( "properly encodes form data for request.text() usage" , async ( {
387- page,
388- } ) => {
389- let app = new PlaywrightFixture ( appFixture , page ) ;
390- await app . goto ( `/request-text` ) ;
391- await page . waitForSelector ( `#text:has-text("${ WAITING_VALUE } ")` ) ;
392-
393- await page . click ( "button[type=submit]" ) ;
394- await page . waitForSelector ( "#action-text" ) ;
395- expect ( await app . getHtml ( "#action-text" ) ) . toBe (
396- '<span id="action-text">a=1&b=2</span>'
397- ) ;
398- } ) ;
399-
400- test ( "redirects a thrown response on document requests" , async ( ) => {
401- let params = new URLSearchParams ( ) ;
402- let res = await fixture . postDocument ( `/${ THROWS_REDIRECT } ` , params ) ;
403- expect ( res . status ) . toBe ( 302 ) ;
404- expect ( res . headers . get ( "Location" ) ) . toBe ( `/${ REDIRECT_TARGET } ` ) ;
405- } ) ;
406-
407- test ( "redirects a thrown response on script transitions" , async ( {
408- page,
409- } ) => {
410- let app = new PlaywrightFixture ( appFixture , page ) ;
411- await app . goto ( `/${ THROWS_REDIRECT } ` ) ;
412- let responses = app . collectSingleFetchResponses ( ) ;
413- await app . clickSubmitButton ( `/${ THROWS_REDIRECT } ` ) ;
414-
415- await page . waitForSelector ( `#${ REDIRECT_TARGET } ` ) ;
416-
417- expect ( responses . length ) . toBe ( 1 ) ;
418- expect ( responses [ 0 ] . status ( ) ) . toBe ( 200 ) ;
419-
420- expect ( new URL ( page . url ( ) ) . pathname ) . toBe ( `/${ REDIRECT_TARGET } ` ) ;
421- expect ( await app . getHtml ( ) ) . toMatch ( PAGE_TEXT ) ;
422- } ) ;
423- } ) ;
424- } ) ;
0 commit comments