Unverified Commit a1c15d84 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Add test for createOptimisticTree (#45330)

<!--
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:
-->

Additional unit tests for the router reducer.

## 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 433a580b
import { createOptimisticTree } from './create-optimistic-tree'
import type { FlightRouterState } from '../../../server/app-render'
const getInitialRouterStateTree = (): FlightRouterState => [
'',
{
children: [
'linking',
{
children: ['', {}],
},
],
},
undefined,
undefined,
true,
]
describe('createOptimisticTree', () => {
it('should create an optimistic tree', () => {
const initialRouterStateTree = getInitialRouterStateTree()
const pathname = '/dashboard/settings'
const segments = pathname.split('/')
// TODO-APP: figure out something better for index pages
segments.push('')
const result = createOptimisticTree(segments, initialRouterStateTree, false)
expect(result).toMatchObject([
'',
{
children: [
'dashboard',
{
children: [
'settings',
{
children: ['', {}],
},
],
},
undefined,
'refetch',
],
},
])
})
})
import React from 'react'
import type { FlightData } from '../../../server/app-render'
import { invalidateCacheBelowFlightSegmentPath } from './invalidate-cache-below-flight-segmentpath'
import { CacheStates, CacheNode } from '../../../shared/lib/app-router-context'
import { fillCacheWithNewSubTreeData } from './fill-cache-with-new-subtree-data'
const getFlightData = (): FlightData => {
return [
[
'children',
'linking',
'children',
'about',
[
'about',
{
children: ['', {}],
},
],
<h1>About Page!</h1>,
<>
<title>About page!</title>
</>,
],
]
}
describe('invalidateCacheBelowFlightSegmentPath', () => {
it('should invalidate cache below flight segment path', () => {
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]
const flightSegmentPath = flightDataPath.slice(0, -3)
// @ts-expect-error TODO-APP: investigate why this is not a TS error in router-reducer.
cache.status = CacheStates.READY
// Copy subTreeData for the root node of the cache.
// @ts-expect-error TODO-APP: investigate why this is not a TS error in router-reducer.
cache.subTreeData = existingCache.subTreeData
// Create a copy of the existing cache with the subTreeData applied.
fillCacheWithNewSubTreeData(cache, existingCache, flightDataPath)
// Invalidate the cache below the flight segment path. This should remove the 'about' node.
invalidateCacheBelowFlightSegmentPath(
cache,
existingCache,
flightSegmentPath
)
const expectedCache: CacheNode = {
data: null,
parallelRoutes: new Map([
[
'children',
new Map([
[
'linking',
{
data: null,
parallelRoutes: new Map([
[
'children',
new Map([
[
'',
{
data: null,
parallelRoutes: new Map(),
status: CacheStates.READY,
subTreeData: <React.Fragment>Page</React.Fragment>,
},
],
]),
],
]),
status: CacheStates.READY,
subTreeData: <React.Fragment>Linking</React.Fragment>,
},
],
]),
],
]),
status: CacheStates.READY,
subTreeData: <>Root layout</>,
}
expect(cache).toMatchObject(expectedCache)
})
})
import React from 'react'
import { invalidateCacheByRouterState } from './invalidate-cache-by-router-state'
import { CacheStates, CacheNode } from '../../../shared/lib/app-router-context'
import { FlightRouterState } from '../../../server/app-render'
describe('invalidateCacheByRouterState', () => {
it('should invalidate the cache by router state', () => {
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 routerState: FlightRouterState = [
'',
{
children: [
'linking',
{
children: [
'about',
{
children: ['', {}],
},
],
},
],
},
null,
null,
true,
]
invalidateCacheByRouterState(cache, existingCache, routerState)
const expectedCache: CacheNode = {
data: null,
status: CacheStates.LAZY_INITIALIZED,
subTreeData: null,
parallelRoutes: new Map([['children', new Map()]]),
}
expect(cache).toMatchObject(expectedCache)
})
})
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