Skip to content

Commit 2dbcd45

Browse files
committed
test(client): add navigating to pages router including hash test
1 parent 6db6e8b commit 2dbcd45

File tree

1 file changed

+162
-2
lines changed

1 file changed

+162
-2
lines changed

packages/next/src/client/components/router-reducer/reducers/navigate-reducer.test.tsx

Lines changed: 162 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react'
2-
import type { fetchServerResponse as fetchServerResponseType } from '../fetch-server-response'
32
import type { FlightData } from '../../../../server/app-render/types'
3+
import type { fetchServerResponse as fetchServerResponseType } from '../fetch-server-response'
44

55
const buildId = 'development'
66

@@ -66,6 +66,10 @@ jest.mock('../fetch-server-response', () => {
6666
return Promise.resolve([flightData, undefined])
6767
}
6868

69+
if (url.pathname === '/pages-router' && url.hash === '#hash') {
70+
return Promise.resolve(['/pages-router', undefined])
71+
}
72+
6973
if (url.pathname === '/parallel-tab-bar/demographics') {
7074
return Promise.resolve([demographicsFlightData, undefined])
7175
}
@@ -82,9 +86,9 @@ import {
8286
} from '../../../../shared/lib/app-router-context.shared-runtime'
8387
import { createInitialRouterState } from '../create-initial-router-state'
8488
import {
85-
NavigateAction,
8689
ACTION_NAVIGATE,
8790
ACTION_PREFETCH,
91+
NavigateAction,
8892
PrefetchAction,
8993
PrefetchKind,
9094
} from '../router-reducer-types'
@@ -1230,6 +1234,162 @@ describe('navigateReducer', () => {
12301234
`)
12311235
})
12321236

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+
12331393
it('should apply navigation with prefetched data', async () => {
12341394
const initialTree = getInitialRouterStateTree()
12351395
const initialCanonicalUrl = '/linking'

0 commit comments

Comments
 (0)