rsnext/test/unit/page-route-sorter.test.js
Natalie Marleny b8aee7af77 fix: Add error message for duplicate route param name (#8047)
* Add error message for duplicate slug name within a dynamic path

* Update based on feedback

Co-authored-by: Tim Neutkens <tim@timneutkens.nl>

* WIP - committed with no-verify - progress

Co-authored-by: Tim Neutkens <tim@timneutkens.nl>

* Remove old test placeholder

* Add test for re-used names
2019-07-30 16:21:36 -05:00

51 lines
1.3 KiB
JavaScript

/* eslint-env jest */
import { getSortedRoutes } from 'next-server/dist/lib/router/utils/sorted-routes'
describe('getSortedRoutes', () => {
it('does not add extra routes', () => {
expect(getSortedRoutes(['/posts'])).toEqual(['/posts'])
expect(getSortedRoutes(['/posts/[id]'])).toEqual(['/posts/[id]'])
expect(getSortedRoutes(['/posts/[id]/foo'])).toEqual(['/posts/[id]/foo'])
expect(getSortedRoutes(['/posts/[id]/[foo]/bar'])).toEqual([
'/posts/[id]/[foo]/bar'
])
expect(getSortedRoutes(['/posts/[id]/baz/[foo]/bar'])).toEqual([
'/posts/[id]/baz/[foo]/bar'
])
})
it('correctly sorts required slugs', () => {
expect(
getSortedRoutes([
'/posts',
'/[root-slug]',
'/',
'/posts/[id]',
'/blog/[id]/comments/[cid]',
'/blog/[id]',
'/foo/[d]/bar/baz/[f]',
'/apples/[ab]/[cd]/ef'
])
).toMatchSnapshot()
})
it('catches mismatched param names', () => {
expect(() =>
getSortedRoutes([
'/',
'/blog',
'/blog/[id]',
'/blog/[id]/comments/[cid]',
'/blog/[cid]'
])
).toThrowError(/different slug names/)
})
it('catches reused param names', () => {
expect(() =>
getSortedRoutes(['/', '/blog', '/blog/[id]/comments/[id]', '/blog/[id]'])
).toThrowError(/the same slug name/)
})
})