Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test for shouldHardNavigate #45303

Merged
merged 2 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,7 @@ function clientReducer(
shouldHardNavigate(
// TODO-APP: remove ''
['', ...flightSegmentPath],
state.tree,
newTree
state.tree
)

if (hardNavigate) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import React from 'react'
import type { FlightData, FlightRouterState } from '../../../server/app-render'
import { shouldHardNavigate } from './should-hard-navigate'

describe('shouldHardNavigate', () => {
it('should return false if the segments match', () => {
const getInitialRouterStateTree = (): FlightRouterState => [
'',
{
children: [
'linking',
{
children: ['', {}],
},
],
},
undefined,
undefined,
true,
]
const initialRouterStateTree = getInitialRouterStateTree()
const getFlightData = (): FlightData => {
return [
[
'children',
'linking',
'children',
'about',
[
'about',
{
children: ['', {}],
},
],
<h1>About Page!</h1>,
<>
<title>About page!</title>
</>,
],
]
}
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)

const result = shouldHardNavigate(
['', ...flightSegmentPath],
initialRouterStateTree
)

expect(result).toBe(false)
})

it('should return false if segments are dynamic and match', () => {
const getInitialRouterStateTree = (): FlightRouterState => [
'',
{
children: [
'link-hard-push',
{
children: [
['id', '123', 'd'],
{
children: ['', {}],
},
],
},
],
},
null,
null,
true,
]
const initialRouterStateTree = getInitialRouterStateTree()
const getFlightData = (): FlightData => {
return [
[
'children',
'link-hard-push',
'children',
['id', '123', 'd'],
[
['id', '123', 'd'],
{
children: ['', {}],
},
],
null,
null,
],
]
}
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)

const result = shouldHardNavigate(
['', ...flightSegmentPath],
initialRouterStateTree
)

expect(result).toBe(false)
})

it('should return true if segments are dynamic and mismatch', () => {
const getInitialRouterStateTree = (): FlightRouterState => [
'',
{
children: [
'link-hard-push',
{
children: [
['id', '456', 'd'],
{
children: ['', {}],
},
],
},
],
},
null,
null,
true,
]
const initialRouterStateTree = getInitialRouterStateTree()
const getFlightData = (): FlightData => {
return [
[
'children',
'link-hard-push',
'children',
['id', '123', 'd'],
[
['id', '123', 'd'],
{
children: ['', {}],
},
],
null,
null,
],
]
}
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)

const result = shouldHardNavigate(
['', ...flightSegmentPath],
initialRouterStateTree
)

expect(result).toBe(true)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {
} from '../../../server/app-render'
import { matchSegment } from '../match-segments'

// TODO-APP: flightSegmentPath will be empty in case of static response, needs to be handled.
export function shouldHardNavigate(
flightSegmentPath: FlightDataPath,
flightRouterState: FlightRouterState,
treePatch: FlightRouterState
flightRouterState: FlightRouterState
): boolean {
const [segment, parallelRoutes] = flightRouterState
// TODO-APP: Check if `as` can be replaced.
Expand All @@ -35,7 +35,6 @@ export function shouldHardNavigate(

return shouldHardNavigate(
flightSegmentPath.slice(2),
parallelRoutes[parallelRouteKey],
treePatch
parallelRoutes[parallelRouteKey]
)
}