1
1
import React from 'react'
2
- import type { fetchServerResponse as fetchServerResponseType } from '../fetch-server-response'
3
2
import type { FlightData } from '../../../../server/app-render/types'
3
+ import type { fetchServerResponse as fetchServerResponseType } from '../fetch-server-response'
4
4
5
5
const buildId = 'development'
6
6
@@ -66,6 +66,10 @@ jest.mock('../fetch-server-response', () => {
66
66
return Promise . resolve ( [ flightData , undefined ] )
67
67
}
68
68
69
+ if ( url . pathname === '/pages-router' && url . hash === '#hash' ) {
70
+ return Promise . resolve ( [ '/pages-router' , undefined ] )
71
+ }
72
+
69
73
if ( url . pathname === '/parallel-tab-bar/demographics' ) {
70
74
return Promise . resolve ( [ demographicsFlightData , undefined ] )
71
75
}
@@ -82,9 +86,9 @@ import {
82
86
} from '../../../../shared/lib/app-router-context.shared-runtime'
83
87
import { createInitialRouterState } from '../create-initial-router-state'
84
88
import {
85
- NavigateAction ,
86
89
ACTION_NAVIGATE ,
87
90
ACTION_PREFETCH ,
91
+ NavigateAction ,
88
92
PrefetchAction ,
89
93
PrefetchKind ,
90
94
} from '../router-reducer-types'
@@ -1230,6 +1234,162 @@ describe('navigateReducer', () => {
1230
1234
` )
1231
1235
} )
1232
1236
1237
+ it ( 'should apply navigation to pages router (including hash)' , async ( ) => {
1238
+ const initialTree = getInitialRouterStateTree ( )
1239
+ const initialCanonicalUrl = '/linking'
1240
+ const children = (
1241
+ < html >
1242
+ < head > </ head >
1243
+ < body > Root layout</ body >
1244
+ </ html >
1245
+ )
1246
+ const initialParallelRoutes : CacheNode [ 'parallelRoutes' ] = new Map ( [
1247
+ [
1248
+ 'children' ,
1249
+ new Map ( [
1250
+ [
1251
+ 'linking' ,
1252
+ {
1253
+ status : CacheStates . READY ,
1254
+ parallelRoutes : new Map ( [
1255
+ [
1256
+ 'children' ,
1257
+ new Map ( [
1258
+ [
1259
+ '__PAGE__' ,
1260
+ {
1261
+ status : CacheStates . READY ,
1262
+ data : null ,
1263
+ subTreeData : < > Linking page</ > ,
1264
+ parallelRoutes : new Map ( ) ,
1265
+ } ,
1266
+ ] ,
1267
+ ] ) ,
1268
+ ] ,
1269
+ ] ) ,
1270
+ data : null ,
1271
+ subTreeData : < > Linking layout level</ > ,
1272
+ } ,
1273
+ ] ,
1274
+ ] ) ,
1275
+ ] ,
1276
+ ] )
1277
+
1278
+ const state = createInitialRouterState ( {
1279
+ buildId,
1280
+ initialTree,
1281
+ initialHead : null ,
1282
+ initialCanonicalUrl,
1283
+ children,
1284
+ initialParallelRoutes,
1285
+ isServer : false ,
1286
+ location : new URL ( '/linking' , 'https://localhost' ) as any ,
1287
+ } )
1288
+
1289
+ const state2 = createInitialRouterState ( {
1290
+ buildId,
1291
+ initialTree,
1292
+ initialHead : null ,
1293
+ initialCanonicalUrl,
1294
+ children,
1295
+ initialParallelRoutes,
1296
+ isServer : false ,
1297
+ location : new URL ( '/linking#hash' , 'https://localhost' ) as any ,
1298
+ } )
1299
+
1300
+ const action : NavigateAction = {
1301
+ type : ACTION_NAVIGATE ,
1302
+ url : new URL ( '/pages-router#hash' , 'https://localhost' ) ,
1303
+ isExternalUrl : false ,
1304
+ locationSearch : '' ,
1305
+ navigateType : 'push' ,
1306
+ shouldScroll : false , // should not scroll
1307
+ forceOptimisticNavigation : false ,
1308
+ cache : {
1309
+ status : CacheStates . LAZY_INITIALIZED ,
1310
+ data : null ,
1311
+ subTreeData : null ,
1312
+ parallelRoutes : new Map ( ) ,
1313
+ } ,
1314
+ mutable : { globalMutable } ,
1315
+ }
1316
+
1317
+ await runPromiseThrowChain ( ( ) => navigateReducer ( state , action ) )
1318
+
1319
+ const newState = await runPromiseThrowChain ( ( ) =>
1320
+ navigateReducer ( state2 , action )
1321
+ )
1322
+
1323
+ expect ( newState ) . toMatchInlineSnapshot ( `
1324
+ Object {
1325
+ "buildId": "development",
1326
+ "cache": Object {
1327
+ "data": null,
1328
+ "parallelRoutes": Map {
1329
+ "children" => Map {
1330
+ "linking" => Object {
1331
+ "data": null,
1332
+ "parallelRoutes": Map {
1333
+ "children" => Map {
1334
+ "__PAGE__" => Object {
1335
+ "data": null,
1336
+ "parallelRoutes": Map {},
1337
+ "status": "READY",
1338
+ "subTreeData": <React.Fragment>
1339
+ Linking page
1340
+ </React.Fragment>,
1341
+ },
1342
+ },
1343
+ },
1344
+ "status": "READY",
1345
+ "subTreeData": <React.Fragment>
1346
+ Linking layout level
1347
+ </React.Fragment>,
1348
+ },
1349
+ },
1350
+ },
1351
+ "status": "READY",
1352
+ "subTreeData": <html>
1353
+ <head />
1354
+ <body>
1355
+ Root layout
1356
+ </body>
1357
+ </html>,
1358
+ },
1359
+ "canonicalUrl": "/pages-router#hash",
1360
+ "focusAndScrollRef": Object {
1361
+ "apply": false,
1362
+ "hashFragment": null,
1363
+ "onlyHashChange": false,
1364
+ "segmentPaths": Array [],
1365
+ },
1366
+ "nextUrl": "/linking",
1367
+ "prefetchCache": Map {},
1368
+ "pushRef": Object {
1369
+ "mpaNavigation": true,
1370
+ "pendingPush": true,
1371
+ },
1372
+ "tree": Array [
1373
+ "",
1374
+ Object {
1375
+ "children": Array [
1376
+ "linking",
1377
+ Object {
1378
+ "children": Array [
1379
+ "__PAGE__",
1380
+ Object {},
1381
+ ],
1382
+ },
1383
+ ],
1384
+ },
1385
+ undefined,
1386
+ undefined,
1387
+ true,
1388
+ ],
1389
+ }
1390
+ ` )
1391
+ } )
1392
+
1233
1393
it ( 'should apply navigation with prefetched data' , async ( ) => {
1234
1394
const initialTree = getInitialRouterStateTree ( )
1235
1395
const initialCanonicalUrl = '/linking'
0 commit comments