Unverified Commit 9ac6c4ab authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

remove reducer unit tests (#62766)

These tests were added back when the router would suspend in reducers,
primarily to test that things were working as expected during concurrent
rendering. Now that we don't suspend in reducers, most of these tests
aren't actually adding much value, and instead introduce friction
anytime we want to change the router state since they're currently
relying on snapshotting.

Before removing these tests, I verified that we have a corresponding e2e
test that covers the expected behaviors more explicitly. Specifically:

- `test/e2e/app-dir/actions` (server actions) 
- `test/e2e/app-dir/navigation` (navigation tests & router.refresh)
- `test/e2e/app-dir/app-client-cache` and
`test/e2e/app-dir/app-prefetch` (prefetch & router cache behavior)
- `test/e2e/app-dir/router-autoscroll` (navigation auto-scrolling
behavior)

<!-- 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(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->


Closes NEXT-2669
parent 91a84b3e
import React from 'react'
import type { FlightRouterState } from '../../../../server/app-render/types'
import type { CacheNode } from '../../../../shared/lib/app-router-context.shared-runtime'
import { createInitialRouterState } from '../create-initial-router-state'
import {
ACTION_RESTORE,
PrefetchCacheEntryStatus,
PrefetchKind,
} from '../router-reducer-types'
import type { RestoreAction } from '../router-reducer-types'
import { restoreReducer } from './restore-reducer'
const buildId = 'development'
const getInitialRouterStateTree = (): FlightRouterState => [
'',
{
children: [
'linking',
{
children: ['about', { children: ['', {}] }],
},
],
},
undefined,
undefined,
true,
]
async function runPromiseThrowChain(fn: any): Promise<any> {
try {
return await fn()
} catch (err) {
if (err instanceof Promise) {
await err
return await runPromiseThrowChain(fn)
}
throw err
}
}
describe('serverPatchReducer', () => {
it('should apply server patch', 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',
{
parallelRoutes: new Map([
[
'children',
new Map([
[
'',
{
lazyData: null,
rsc: <>Linking page</>,
prefetchRsc: null,
parallelRoutes: new Map(),
},
],
]),
],
]),
lazyData: null,
rsc: <>Linking layout level</>,
prefetchRsc: null,
},
],
]),
],
])
const state = createInitialRouterState({
buildId,
initialTree,
initialHead: null,
initialCanonicalUrl,
initialSeedData: ['', {}, children],
initialParallelRoutes,
location: new URL('/linking', 'https://localhost') as any,
})
const action: RestoreAction = {
type: ACTION_RESTORE,
url: new URL('/linking/about', 'https://localhost'),
tree: [
'',
{
children: [
'linking',
{
children: [
'about',
{
children: ['', {}],
},
],
},
],
},
null,
null,
true,
],
}
const newState = await runPromiseThrowChain(() =>
restoreReducer(state, action)
)
const expectedState: ReturnType<typeof restoreReducer> = {
buildId,
prefetchCache: new Map([
[
'/linking',
{
key: '/linking',
data: expect.any(Promise),
prefetchTime: expect.any(Number),
kind: PrefetchKind.AUTO,
lastUsedTime: null,
treeAtTimeOfPrefetch: initialTree,
status: PrefetchCacheEntryStatus.fresh,
},
],
]),
pushRef: {
mpaNavigation: false,
pendingPush: false,
preserveCustomHistoryState: true,
},
focusAndScrollRef: {
apply: false,
onlyHashChange: false,
hashFragment: null,
segmentPaths: [],
},
canonicalUrl: '/linking/about',
nextUrl: '/linking/about',
cache: {
lazyData: null,
rsc: (
<html>
<head></head>
<body>Root layout</body>
</html>
),
prefetchRsc: null,
parallelRoutes: new Map([
[
'children',
new Map([
[
'linking',
{
parallelRoutes: new Map([
[
'children',
new Map([
[
'',
{
lazyData: null,
rsc: <>Linking page</>,
prefetchRsc: null,
parallelRoutes: new Map(),
},
],
]),
],
]),
lazyData: null,
rsc: <>Linking layout level</>,
prefetchRsc: null,
},
],
]),
],
]),
},
tree: [
'',
{
children: [
'linking',
{
children: ['about', { children: ['', {}] }],
},
],
},
null,
null,
true,
],
}
expect(newState).toMatchObject(expectedState)
})
it('should apply server patch (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',
{
parallelRoutes: new Map([
[
'children',
new Map([
[
'',
{
lazyData: null,
rsc: <>Linking page</>,
prefetchRsc: null,
parallelRoutes: new Map(),
},
],
]),
],
]),
lazyData: null,
rsc: <>Linking layout level</>,
prefetchRsc: null,
},
],
]),
],
])
const state = createInitialRouterState({
buildId,
initialTree,
initialHead: null,
initialCanonicalUrl,
initialSeedData: ['', {}, children],
initialParallelRoutes,
location: new URL('/linking', 'https://localhost') as any,
})
const state2 = createInitialRouterState({
buildId,
initialTree,
initialHead: null,
initialCanonicalUrl,
initialSeedData: ['', {}, children],
initialParallelRoutes,
location: new URL('/linking', 'https://localhost') as any,
})
const action: RestoreAction = {
type: ACTION_RESTORE,
url: new URL('/linking/about', 'https://localhost'),
tree: [
'',
{
children: [
'linking',
{
children: [
'about',
{
children: ['', {}],
},
],
},
],
},
null,
null,
true,
],
}
await runPromiseThrowChain(() => restoreReducer(state, action))
const newState = await runPromiseThrowChain(() =>
restoreReducer(state2, action)
)
const expectedState: ReturnType<typeof restoreReducer> = {
buildId,
prefetchCache: new Map([
[
'/linking',
{
key: '/linking',
data: expect.any(Promise),
prefetchTime: expect.any(Number),
kind: PrefetchKind.AUTO,
lastUsedTime: null,
treeAtTimeOfPrefetch: initialTree,
status: PrefetchCacheEntryStatus.fresh,
},
],
]),
pushRef: {
mpaNavigation: false,
pendingPush: false,
preserveCustomHistoryState: true,
},
focusAndScrollRef: {
apply: false,
onlyHashChange: false,
hashFragment: null,
segmentPaths: [],
},
canonicalUrl: '/linking/about',
nextUrl: '/linking/about',
cache: {
lazyData: null,
rsc: (
<html>
<head></head>
<body>Root layout</body>
</html>
),
prefetchRsc: null,
parallelRoutes: new Map([
[
'children',
new Map([
[
'linking',
{
parallelRoutes: new Map([
[
'children',
new Map([
[
'',
{
lazyData: null,
rsc: <>Linking page</>,
prefetchRsc: null,
parallelRoutes: new Map(),
},
],
]),
],
]),
lazyData: null,
rsc: <>Linking layout level</>,
prefetchRsc: null,
},
],
]),
],
]),
},
tree: [
'',
{
children: [
'linking',
{
children: ['about', { children: ['', {}] }],
},
],
},
null,
null,
true,
],
}
expect(newState).toMatchObject(expectedState)
})
})
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