Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for router.refresh preserving unaffected segments #46687

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -375,4 +375,208 @@ describe('refreshReducer', () => {

expect(newState).toMatchObject(expectedState)
})

it('should preserve existing segments (concurrent)', async () => {
const initialTree = getInitialRouterStateTree()
const initialCanonicalUrl = '/linking'
const children = (
<html>
<head></head>
<body>Root layout</body>
</html>
)
const initialParallelRoutes: CacheNode['parallelRoutes'] = new Map([
[
'children',
new Map([
[
'linking',
{
status: CacheStates.READY,
parallelRoutes: new Map([
[
'children',
new Map([
[
'',
{
status: CacheStates.READY,
data: null,
subTreeData: <>Linking page</>,
parallelRoutes: new Map(),
},
],
]),
],
]),
data: null,
subTreeData: <>Linking layout level</>,
},
],
[
'about',
{
status: CacheStates.READY,
parallelRoutes: new Map([
[
'children',
new Map([
[
'',
{
status: CacheStates.READY,
data: null,
subTreeData: <>About page</>,
parallelRoutes: new Map(),
},
],
]),
],
]),
data: null,
subTreeData: <>About layout level</>,
},
],
]),
],
])

const state = createInitialRouterState({
initialTree,
initialHead: null,
initialCanonicalUrl,
children,
initialParallelRoutes,
isServer: false,
location: new URL('/linking', 'https://localhost') as any,
})

const state2 = createInitialRouterState({
initialTree,
initialHead: null,
initialCanonicalUrl,
children,
initialParallelRoutes,
isServer: false,
location: new URL('/linking', 'https://localhost') as any,
})

const action: RefreshAction = {
type: ACTION_REFRESH,
cache: {
status: CacheStates.LAZY_INITIALIZED,
data: null,
subTreeData: null,
parallelRoutes: new Map(),
},
mutable: {},
origin: new URL('/linking', 'https://localhost').origin,
}

await runPromiseThrowChain(() => refreshReducer(state, action))

const newState = await runPromiseThrowChain(() =>
refreshReducer(state2, action)
)

const expectedState: ReturnType<typeof refreshReducer> = {
prefetchCache: new Map(),
pushRef: {
mpaNavigation: false,
pendingPush: false,
},
focusAndScrollRef: {
apply: false,
},
canonicalUrl: '/linking',
cache: {
status: CacheStates.READY,
data: null,
subTreeData: (
<html>
<head></head>
<body>
<h1>Linking Page!</h1>
</body>
</html>
),
parallelRoutes: new Map([
[
'children',
new Map([
[
'linking',
{
status: CacheStates.LAZY_INITIALIZED,
parallelRoutes: new Map([
[
'children',
new Map([
[
'',
{
status: CacheStates.LAZY_INITIALIZED,
data: null,
subTreeData: null,
parallelRoutes: new Map(),
head: (
<>
<title>Linking page!</title>
</>
),
},
],
]),
],
]),
data: null,
subTreeData: null,
},
],
[
'about',
{
status: CacheStates.READY,
parallelRoutes: new Map([
[
'children',
new Map([
[
'',
{
status: CacheStates.READY,
data: null,
subTreeData: <>About page</>,
parallelRoutes: new Map(),
},
],
]),
],
]),
data: null,
subTreeData: <>About layout level</>,
},
],
]),
],
]),
},
tree: [
'',
{
children: [
'linking',
{
children: ['', {}],
},
],
},
undefined,
undefined,
true,
],
}

expect(newState).toMatchObject(expectedState)
})
})