Skip to content

Commit ef15b5d

Browse files
Added tests for useGetSideNavItems
1 parent f551e18 commit ef15b5d

File tree

2 files changed

+84
-44
lines changed

2 files changed

+84
-44
lines changed

src/components/layout/SideNav/hooks/__tests__/useGetSideNavItems.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,4 +305,38 @@ describe('useGetSideNavItems', () => {
305305

306306
expect(result.current).not.toContain('TENANT_CERTIFIER')
307307
})
308+
309+
it("should not include 'DELEGATIONS' routes if the user is not a PA", () => {
310+
mockUseJwt({
311+
jwt: {
312+
externalId: {
313+
origin: 'test',
314+
value: 'value',
315+
},
316+
},
317+
currentRoles: ['admin'],
318+
})
319+
320+
const { result } = renderHook(() => useGetSideNavItems())
321+
322+
expect(result.current.some((routeKey) => routeKey.children?.includes('DELEGATIONS'))).toBe(
323+
false
324+
)
325+
})
326+
327+
it("should include 'DELEGATIONS' routes if the user is a PA", () => {
328+
mockUseJwt({
329+
jwt: {
330+
externalId: {
331+
origin: 'IPA',
332+
value: 'value',
333+
},
334+
},
335+
currentRoles: ['admin'],
336+
})
337+
338+
const { result } = renderHook(() => useGetSideNavItems())
339+
340+
expect(result.current.some((routeKey) => routeKey.children?.includes('DELEGATIONS'))).toBe(true)
341+
})
308342
})

src/router/components/RoutesWrapper/__tests__/AuthGuard.test.tsx

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -128,51 +128,57 @@ describe('AuthGuard', () => {
128128
expect(getByText('children')).toBeInTheDocument()
129129
})
130130

131-
it('Should able to access when user try to access on delegations routes and he is a PA', () => {
132-
const props: AuthGuardTestProps = {
133-
...defaultProps,
134-
isSupport: false,
131+
it.each(['DELEGATIONS', 'DELEGATION_DETAILS', 'CREATE_DELEGATION'] as const)(
132+
'Should able to access when user try to access on %s route and he is a PA',
133+
(routeKey) => {
134+
const props: AuthGuardTestProps = {
135+
...defaultProps,
136+
isSupport: false,
137+
}
138+
139+
useAuthGuardSpy.mockReturnValue({
140+
isPublic: false,
141+
authLevels: [],
142+
isUserAuthorized: () => true,
143+
})
144+
mockUseCurrentRoute({
145+
routeKey,
146+
})
147+
mockUseGetActiveUserParty({
148+
data: {
149+
externalId: { origin: 'IPA' },
150+
},
151+
})
152+
153+
const { getByText } = renderAuthGuard(props)
154+
expect(getByText('children')).toBeInTheDocument()
135155
}
156+
)
136157

137-
useAuthGuardSpy.mockReturnValue({
138-
isPublic: false,
139-
authLevels: [],
140-
isUserAuthorized: () => true,
141-
})
142-
mockUseCurrentRoute({
143-
routeKey: 'DELEGATIONS' || 'DELEGATION_DETAILS' || 'CREATE_DELEGATION',
144-
})
145-
mockUseGetActiveUserParty({
146-
data: {
147-
externalId: { origin: 'IPA' },
148-
},
149-
})
150-
151-
const { getByText } = renderAuthGuard(props)
152-
expect(getByText('children')).toBeInTheDocument()
153-
})
154-
155-
it('Should render Error component when user try to access on delegations routes and he is not a PA', () => {
156-
const props: AuthGuardTestProps = {
157-
...defaultProps,
158-
isSupport: false,
158+
it.each(['DELEGATIONS', 'DELEGATION_DETAILS', 'CREATE_DELEGATION'] as const)(
159+
'Should render Error component when user try to access on delegations routes and he is not a PA',
160+
(routeKey) => {
161+
const props: AuthGuardTestProps = {
162+
...defaultProps,
163+
isSupport: false,
164+
}
165+
166+
useAuthGuardSpy.mockReturnValue({
167+
isPublic: false,
168+
authLevels: [],
169+
isUserAuthorized: () => false,
170+
})
171+
mockUseCurrentRoute({
172+
routeKey,
173+
})
174+
mockUseGetActiveUserParty({
175+
data: {
176+
externalId: { origin: '' },
177+
},
178+
})
179+
180+
const { getByText } = renderAuthGuard(props)
181+
expect(getByText('error')).toBeInTheDocument()
159182
}
160-
161-
useAuthGuardSpy.mockReturnValue({
162-
isPublic: false,
163-
authLevels: [],
164-
isUserAuthorized: () => false,
165-
})
166-
mockUseCurrentRoute({
167-
routeKey: 'DELEGATIONS' || 'DELEGATION_DETAILS' || 'CREATE_DELEGATION',
168-
})
169-
mockUseGetActiveUserParty({
170-
data: {
171-
externalId: { origin: '' },
172-
},
173-
})
174-
175-
const { getByText } = renderAuthGuard(props)
176-
expect(getByText('error')).toBeInTheDocument()
177-
})
183+
)
178184
})

0 commit comments

Comments
 (0)