-
Notifications
You must be signed in to change notification settings - Fork 26.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for fillCacheWithNewSubTreeData (#45298)
<!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change that you're making: --> Follow up to moving router-reducer. This adds a unit test for injecting the subTreeData. ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md) ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] [e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md) ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm build && pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
- Loading branch information
1 parent
a94b9db
commit 0d635df
Showing
2 changed files
with
152 additions
and
2 deletions.
There are no files selected for viewing
152 changes: 152 additions & 0 deletions
152
packages/next/src/client/components/router-reducer/fill-cache-with-new-subtree-data.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
import React from 'react' | ||
import { fillCacheWithNewSubTreeData } from './fill-cache-with-new-subtree-data' | ||
import { CacheStates, CacheNode } from '../../../shared/lib/app-router-context' | ||
import { FlightData } from '../../../server/app-render' | ||
|
||
const getFlightData = (): FlightData => { | ||
return [ | ||
[ | ||
'children', | ||
'linking', | ||
'children', | ||
'about', | ||
[ | ||
'about', | ||
{ | ||
children: ['', {}], | ||
}, | ||
], | ||
<h1>SubTreeData Injected!</h1>, | ||
<> | ||
<title>Head Injected!</title> | ||
</>, | ||
], | ||
] | ||
} | ||
|
||
describe('fillCacheWithNewSubtreeData', () => { | ||
it('should apply subTreeData and head property', () => { | ||
const cache: CacheNode = { | ||
status: CacheStates.LAZY_INITIALIZED, | ||
data: null, | ||
subTreeData: null, | ||
parallelRoutes: new Map(), | ||
} | ||
const existingCache: CacheNode = { | ||
data: null, | ||
status: CacheStates.READY, | ||
subTreeData: <>Root layout</>, | ||
parallelRoutes: new Map([ | ||
[ | ||
'children', | ||
new Map([ | ||
[ | ||
'linking', | ||
{ | ||
data: null, | ||
status: CacheStates.READY, | ||
subTreeData: <>Linking</>, | ||
parallelRoutes: new Map([ | ||
[ | ||
'children', | ||
new Map([ | ||
[ | ||
'', | ||
{ | ||
data: null, | ||
status: CacheStates.READY, | ||
subTreeData: <>Page</>, | ||
parallelRoutes: new Map(), | ||
}, | ||
], | ||
]), | ||
], | ||
]), | ||
}, | ||
], | ||
]), | ||
], | ||
]), | ||
} | ||
|
||
const flightData = getFlightData() | ||
|
||
if (typeof flightData === 'string') { | ||
throw new Error('invalid flight data') | ||
} | ||
|
||
// Mirrors the way router-reducer values are passed in. | ||
const flightDataPath = flightData[0] | ||
|
||
fillCacheWithNewSubTreeData(cache, existingCache, flightDataPath) | ||
|
||
const expectedCache: CacheNode = { | ||
data: null, | ||
status: CacheStates.LAZY_INITIALIZED, | ||
subTreeData: null, | ||
parallelRoutes: new Map([ | ||
[ | ||
'children', | ||
new Map([ | ||
[ | ||
'linking', | ||
{ | ||
data: null, | ||
status: CacheStates.READY, | ||
subTreeData: <>Linking</>, | ||
parallelRoutes: new Map([ | ||
[ | ||
'children', | ||
new Map([ | ||
// TODO-APP: this segment should be preserved when creating the new cache | ||
[ | ||
'', | ||
{ | ||
data: null, | ||
status: CacheStates.READY, | ||
subTreeData: <>Page</>, | ||
parallelRoutes: new Map(), | ||
}, | ||
], | ||
[ | ||
'about', | ||
{ | ||
data: null, | ||
parallelRoutes: new Map([ | ||
[ | ||
'children', | ||
new Map([ | ||
[ | ||
'', | ||
{ | ||
data: null, | ||
status: CacheStates.LAZY_INITIALIZED, | ||
subTreeData: null, | ||
parallelRoutes: new Map(), | ||
head: ( | ||
<> | ||
<title>Head Injected!</title> | ||
</> | ||
), | ||
}, | ||
], | ||
]), | ||
], | ||
]), | ||
subTreeData: <h1>SubTreeData Injected!</h1>, | ||
status: CacheStates.READY, | ||
}, | ||
], | ||
]), | ||
], | ||
]), | ||
}, | ||
], | ||
]), | ||
], | ||
]), | ||
} | ||
|
||
expect(cache).toMatchObject(expectedCache) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters