Unverified Commit 0d635dff authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

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)
parent a94b9db8
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)
})
})
...@@ -88,8 +88,6 @@ describe('fillLazyItemsTillLeafWithHead', () => { ...@@ -88,8 +88,6 @@ describe('fillLazyItemsTillLeafWithHead', () => {
const flightDataPath = flightData[0] const flightDataPath = flightData[0]
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
const [treePatch, _subTreeData, head] = flightDataPath.slice(-3) const [treePatch, _subTreeData, head] = flightDataPath.slice(-3)
// const flightSegmentPath = flightDataPath.slice(0, -4)
fillLazyItemsTillLeafWithHead(cache, existingCache, treePatch, head) fillLazyItemsTillLeafWithHead(cache, existingCache, treePatch, head)
const expectedCache: CacheNode = { const expectedCache: CacheNode = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment