3
3
*/
4
4
5
5
import type { StaticHandler , StaticHandlerContext } from "../router" ;
6
- import { UNSAFE_DEFERRED_SYMBOL , createStaticHandler } from "../router" ;
6
+ import {
7
+ UNSAFE_DEFERRED_SYMBOL ,
8
+ createStaticHandler ,
9
+ getStaticContextFromError ,
10
+ } from "../router" ;
7
11
import {
8
12
ErrorResponseImpl ,
9
13
defer ,
@@ -13,7 +17,7 @@ import {
13
17
} from "../utils" ;
14
18
import { deferredData , trackedPromise } from "./utils/custom-matchers" ;
15
19
import { createDeferred } from "./utils/data-router-setup" ;
16
- import { createRequest , createSubmitRequest } from "./utils/utils" ;
20
+ import { createRequest , createSubmitRequest , invariant } from "./utils/utils" ;
17
21
18
22
interface CustomMatchers < R = jest . Expect > {
19
23
trackedPromise ( data ?: any , error ?: any , aborted ?: boolean ) : R ;
@@ -1385,6 +1389,58 @@ describe("ssr", () => {
1385
1389
] ) ;
1386
1390
} ) ;
1387
1391
} ) ;
1392
+
1393
+ describe ( "getStaticContextFromError" , ( ) => {
1394
+ it ( "should provide a context for a second-pass render for a thrown error" , async ( ) => {
1395
+ let { query } = createStaticHandler ( SSR_ROUTES ) ;
1396
+ let context = await query ( createRequest ( "/" ) ) ;
1397
+ expect ( context ) . toMatchObject ( {
1398
+ errors : null ,
1399
+ loaderData : {
1400
+ index : "INDEX LOADER" ,
1401
+ } ,
1402
+ statusCode : 200 ,
1403
+ } ) ;
1404
+
1405
+ let error = new Error ( "💥" ) ;
1406
+ invariant ( ! ( context instanceof Response ) , "Uh oh" ) ;
1407
+ context = getStaticContextFromError ( SSR_ROUTES , context , error ) ;
1408
+ expect ( context ) . toMatchObject ( {
1409
+ errors : {
1410
+ index : error ,
1411
+ } ,
1412
+ loaderData : {
1413
+ index : "INDEX LOADER" ,
1414
+ } ,
1415
+ statusCode : 500 ,
1416
+ } ) ;
1417
+ } ) ;
1418
+
1419
+ it ( "should accept a thrown response from entry.server" , async ( ) => {
1420
+ let { query } = createStaticHandler ( SSR_ROUTES ) ;
1421
+ let context = await query ( createRequest ( "/" ) ) ;
1422
+ expect ( context ) . toMatchObject ( {
1423
+ errors : null ,
1424
+ loaderData : {
1425
+ index : "INDEX LOADER" ,
1426
+ } ,
1427
+ statusCode : 200 ,
1428
+ } ) ;
1429
+
1430
+ let errorResponse = new ErrorResponseImpl ( 400 , "Bad Request" , "Oops!" ) ;
1431
+ invariant ( ! ( context instanceof Response ) , "Uh oh" ) ;
1432
+ context = getStaticContextFromError ( SSR_ROUTES , context , errorResponse ) ;
1433
+ expect ( context ) . toMatchObject ( {
1434
+ errors : {
1435
+ index : errorResponse ,
1436
+ } ,
1437
+ loaderData : {
1438
+ index : "INDEX LOADER" ,
1439
+ } ,
1440
+ statusCode : 400 ,
1441
+ } ) ;
1442
+ } ) ;
1443
+ } ) ;
1388
1444
} ) ;
1389
1445
1390
1446
describe ( "singular route requests" , ( ) => {
0 commit comments