-
Notifications
You must be signed in to change notification settings - Fork 26.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit test for shouldHardNavigate
- Loading branch information
1 parent
98b43a0
commit e14d8c7
Showing
3 changed files
with
177 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
173 changes: 173 additions & 0 deletions
173
packages/next/src/client/components/router-reducer/should-hard-navigate.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters