Skip to content

Commit

Permalink
Add test for fillCacheWithNewSubTreeData (#45298)
Browse files Browse the repository at this point in the history
<!--
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
timneutkens committed Jan 27, 2023
1 parent a94b9db commit 0d635df
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 2 deletions.
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)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ describe('fillLazyItemsTillLeafWithHead', () => {
const flightDataPath = flightData[0]
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [treePatch, _subTreeData, head] = flightDataPath.slice(-3)
// const flightSegmentPath = flightDataPath.slice(0, -4)

fillLazyItemsTillLeafWithHead(cache, existingCache, treePatch, head)

const expectedCache: CacheNode = {
Expand Down

0 comments on commit 0d635df

Please sign in to comment.