Skip to content

Commit

Permalink
Add test for router.refresh preserving unaffected segments (#46687)
Browse files Browse the repository at this point in the history
Adds a test for a current behavior we're looking at changing. Will do a follow-up PR that changes this test. Want to make sure it's tested first.


## 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 Mar 3, 2023
1 parent 9acbf6e commit ff2159d
Showing 1 changed file with 204 additions and 0 deletions.
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)
})
})

0 comments on commit ff2159d

Please sign in to comment.