Skip to content

Commit

Permalink
getDuplicateRoutes: Ignore redirects and other unnamed routes (#7564)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobbe authored Feb 9, 2023
1 parent 2fc8dce commit 2813199
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
50 changes: 50 additions & 0 deletions packages/internal/src/__tests__/routes-mocked.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
jest.mock('../paths', () => ({
getPaths: () => ({
base: '',
}),
}))

jest.mock('@redwoodjs/structure/dist/model/RWRoute', () => ({
RWRoute: {},
}))

jest.mock('@redwoodjs/structure', () => {
return {
getProject: () => {
return {
router: {
// <Router useAuth={useAuth}>
// <Route path="/login" page={LoginPage} name="login" />
// <Route path="/" redirect="/dashboard" />
// <Route notfound page={NotFoundPage} />
// </Router>
routes: [
{
name: 'login',
page_identifier_str: 'LoginPage',
path: '/login',
},
{
path: '/',
},
{
page_identifier_str: 'NotFoundPage',
},
],
},
}
},
}
})

import { getDuplicateRoutes, warningForDuplicateRoutes } from '../routes'

describe('notfound and redirect routes', () => {
it('Detects no duplicate routes', () => {
expect(getDuplicateRoutes()).toStrictEqual([])
})

it('Produces the correct warning message', () => {
expect(warningForDuplicateRoutes()).toBe('')
})
})
8 changes: 6 additions & 2 deletions packages/internal/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ export function getDuplicateRoutes() {
const duplicateRoutes: RouteInformation[] = []
const allRoutes: (typeof RWRoute)[] = getProject(getPaths().base).router
.routes
const uniquePathNames = new Set(allRoutes.map((route) => route.name))
uniquePathNames.forEach((name) => {
const uniqueNames = new Set(
allRoutes
.filter((route) => route.name !== undefined)
.map((route) => route.name)
)
uniqueNames.forEach((name) => {
const routesWithName = allRoutes.filter((route) => {
return route.name === name
})
Expand Down

0 comments on commit 2813199

Please sign in to comment.