Skip to content

Commit

Permalink
chore(router): Add missing tests in analyze routes (#9274)
Browse files Browse the repository at this point in the history
  • Loading branch information
dac09 authored and jtoar committed Oct 28, 2023
1 parent c6b1668 commit 7b81850
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
68 changes: 60 additions & 8 deletions packages/router/src/__tests__/analyzeRoutes.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ describe('AnalyzeRoutes: with homePage and Children', () => {
expect(namedRoutesMap.rdSimple()).toBe('/rdSimple')
})

test('Redirect routes analysis', () => {
test('Nested sets, and authentication logic', () => {
const HomePage = () => <h1>Home Page</h1>
const PrivateAdminPage = () => <h1>Private Admin Page</h1>
const PrivateEmployeePage = () => <h1>Private Employee Page</h1>
Expand Down Expand Up @@ -373,14 +373,66 @@ describe('AnalyzeRoutes: with homePage and Children', () => {
}
)

console.log(JSON.stringify({ pathRouteMap, namedRoutesMap }, null, 3))
// Level 1: wrapped with private
expect(pathRouteMap).toMatchObject({
'/no-roles-assigned': {
redirect: null,
setProps: expect.arrayContaining([
expect.objectContaining({
unauthenticated: 'home',
private: true,
}),
]),
},
})

expect(Object.keys(namedRoutesMap).length).toBe(4)

// expect(pathRouteMap['/simple'].redirect).toBe('/rdSimple')
// expect(pathRouteMap['/rdSimple'].redirect).toBeFalsy()
// Level 2: wrapped in 2 private sets
expect(pathRouteMap).toMatchObject({
'/employee': {
redirect: null,
setProps: expect.arrayContaining([
// Should have the first one, but also..
expect.objectContaining({
unauthenticated: 'home',
private: true,
}),
// ...the second private set's props
expect.objectContaining({
private: true,
unauthenticated: 'noRolesAssigned',
roles: ['ADMIN', 'EMPLOYEE'],
}),
]),
},
})

// // @TODO true for now, but we may not allow names on a redirect route
// expect(Object.keys(namedRoutesMap).length).toBe(2)
// expect(namedRoutesMap.simple()).toBe('/simple')
// expect(namedRoutesMap.rdSimple()).toBe('/rdSimple')
// Level 3: wrapped in 3 private sets
expect(pathRouteMap).toMatchObject({
'/admin': {
redirect: null,
setProps: expect.arrayContaining([
// Should have the first one, but also..
expect.objectContaining({
unauthenticated: 'home',
private: true,
}),
// ...the second private set's props
expect.objectContaining({
private: true,
unauthenticated: 'noRolesAssigned',
roles: ['ADMIN', 'EMPLOYEE'],
}),

// ...and the third private set's props
expect.objectContaining({
unauthenticated: 'employee',
roles: 'ADMIN',
private: true,
}),
]),
},
})
})
})
2 changes: 0 additions & 2 deletions packages/router/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -612,8 +612,6 @@ export function analyzeRoutes(
// @MARK note unintuitive, but intentional
// You cannot make a nested set public if the parent is private
// i.e. the private prop cannot be overriden by a child Set

// @TODO: Double check this logic is correct for privatge logic in previous set props
const privateProps =
isPrivateNode(node) || previousSetProps.some((props) => props.private)
? { private: true }
Expand Down

0 comments on commit 7b81850

Please sign in to comment.