File size: 4,053 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
import React from 'react'
import type { FlightRouterState } from '../../../server/app-render/types'
import type { CacheNode } from '../../../shared/lib/app-router-context.shared-runtime'
import { createInitialRouterState } from './create-initial-router-state'
import { PrefetchCacheEntryStatus, PrefetchKind } from './router-reducer-types'
const getInitialRouterStateTree = (): FlightRouterState => [
'',
{
children: [
'linking',
{
children: ['', {}],
},
],
},
undefined,
undefined,
true,
]
const navigatedAt = Date.now()
describe('createInitialRouterState', () => {
it('should return the correct initial router state', () => {
const initialTree = getInitialRouterStateTree()
const initialCanonicalUrl = '/linking'
const children = (
<html>
<head></head>
<body>Root layout</body>
</html>
)
const initialParallelRoutes: CacheNode['parallelRoutes'] = new Map()
const state = createInitialRouterState({
navigatedAt,
initialFlightData: [[initialTree, ['', children, {}, null]]],
initialCanonicalUrlParts: initialCanonicalUrl.split('/'),
initialParallelRoutes,
location: new URL('/linking', 'https://localhost') as any,
couldBeIntercepted: false,
postponed: false,
prerendered: false,
})
const state2 = createInitialRouterState({
navigatedAt,
initialFlightData: [[initialTree, ['', children, {}, null]]],
initialCanonicalUrlParts: initialCanonicalUrl.split('/'),
initialParallelRoutes,
location: new URL('/linking', 'https://localhost') as any,
couldBeIntercepted: false,
postponed: false,
prerendered: false,
})
const expectedCache: CacheNode = {
navigatedAt,
lazyData: null,
rsc: children,
prefetchRsc: null,
head: null,
prefetchHead: null,
loading: null,
parallelRoutes: new Map([
[
'children',
new Map([
[
'linking',
{
navigatedAt,
parallelRoutes: new Map([
[
'children',
new Map([
[
'',
{
navigatedAt,
lazyData: null,
rsc: null,
prefetchRsc: null,
parallelRoutes: new Map(),
loading: null,
head: null,
prefetchHead: null,
},
],
]),
],
]),
lazyData: null,
rsc: null,
prefetchRsc: null,
head: null,
prefetchHead: null,
loading: null,
},
],
]),
],
]),
}
const expected: ReturnType<typeof createInitialRouterState> = {
tree: initialTree,
canonicalUrl: initialCanonicalUrl,
prefetchCache: new Map([
[
'/linking',
{
key: '/linking',
data: expect.any(Promise),
prefetchTime: expect.any(Number),
kind: PrefetchKind.AUTO,
lastUsedTime: expect.any(Number),
treeAtTimeOfPrefetch: initialTree,
status: PrefetchCacheEntryStatus.fresh,
url: new URL('/linking', 'https://localhost'),
staleTime: -1,
},
],
]),
pushRef: {
pendingPush: false,
mpaNavigation: false,
preserveCustomHistoryState: true,
},
focusAndScrollRef: {
apply: false,
onlyHashChange: false,
hashFragment: null,
segmentPaths: [],
},
cache: expectedCache,
nextUrl: '/linking',
}
expect(state).toMatchObject(expected)
expect(state2).toMatchObject(expected)
})
})
|