Skip to content

Commit

Permalink
Added tests for useGetSideNavItems
Browse files Browse the repository at this point in the history
  • Loading branch information
martinaCampoli committed Dec 12, 2024
1 parent f551e18 commit ef15b5d
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,38 @@ describe('useGetSideNavItems', () => {

expect(result.current).not.toContain('TENANT_CERTIFIER')
})

it("should not include 'DELEGATIONS' routes if the user is not a PA", () => {
mockUseJwt({
jwt: {
externalId: {
origin: 'test',
value: 'value',
},
},
currentRoles: ['admin'],
})

const { result } = renderHook(() => useGetSideNavItems())

expect(result.current.some((routeKey) => routeKey.children?.includes('DELEGATIONS'))).toBe(
false
)
})

it("should include 'DELEGATIONS' routes if the user is a PA", () => {
mockUseJwt({
jwt: {
externalId: {
origin: 'IPA',
value: 'value',
},
},
currentRoles: ['admin'],
})

const { result } = renderHook(() => useGetSideNavItems())

expect(result.current.some((routeKey) => routeKey.children?.includes('DELEGATIONS'))).toBe(true)
})
})
94 changes: 50 additions & 44 deletions src/router/components/RoutesWrapper/__tests__/AuthGuard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,51 +128,57 @@ describe('AuthGuard', () => {
expect(getByText('children')).toBeInTheDocument()
})

it('Should able to access when user try to access on delegations routes and he is a PA', () => {
const props: AuthGuardTestProps = {
...defaultProps,
isSupport: false,
it.each(['DELEGATIONS', 'DELEGATION_DETAILS', 'CREATE_DELEGATION'] as const)(
'Should able to access when user try to access on %s route and he is a PA',
(routeKey) => {
const props: AuthGuardTestProps = {
...defaultProps,
isSupport: false,
}

useAuthGuardSpy.mockReturnValue({
isPublic: false,
authLevels: [],
isUserAuthorized: () => true,
})
mockUseCurrentRoute({
routeKey,
})
mockUseGetActiveUserParty({
data: {
externalId: { origin: 'IPA' },
},
})

const { getByText } = renderAuthGuard(props)
expect(getByText('children')).toBeInTheDocument()
}
)

useAuthGuardSpy.mockReturnValue({
isPublic: false,
authLevels: [],
isUserAuthorized: () => true,
})
mockUseCurrentRoute({
routeKey: 'DELEGATIONS' || 'DELEGATION_DETAILS' || 'CREATE_DELEGATION',
})
mockUseGetActiveUserParty({
data: {
externalId: { origin: 'IPA' },
},
})

const { getByText } = renderAuthGuard(props)
expect(getByText('children')).toBeInTheDocument()
})

it('Should render Error component when user try to access on delegations routes and he is not a PA', () => {
const props: AuthGuardTestProps = {
...defaultProps,
isSupport: false,
it.each(['DELEGATIONS', 'DELEGATION_DETAILS', 'CREATE_DELEGATION'] as const)(
'Should render Error component when user try to access on delegations routes and he is not a PA',
(routeKey) => {
const props: AuthGuardTestProps = {
...defaultProps,
isSupport: false,
}

useAuthGuardSpy.mockReturnValue({
isPublic: false,
authLevels: [],
isUserAuthorized: () => false,
})
mockUseCurrentRoute({
routeKey,
})
mockUseGetActiveUserParty({
data: {
externalId: { origin: '' },
},
})

const { getByText } = renderAuthGuard(props)
expect(getByText('error')).toBeInTheDocument()
}

useAuthGuardSpy.mockReturnValue({
isPublic: false,
authLevels: [],
isUserAuthorized: () => false,
})
mockUseCurrentRoute({
routeKey: 'DELEGATIONS' || 'DELEGATION_DETAILS' || 'CREATE_DELEGATION',
})
mockUseGetActiveUserParty({
data: {
externalId: { origin: '' },
},
})

const { getByText } = renderAuthGuard(props)
expect(getByText('error')).toBeInTheDocument()
})
)
})

0 comments on commit ef15b5d

Please sign in to comment.