|
| 1 | +import { act, ReactNode } from 'react'; |
| 2 | +import { renderHook, waitFor } from '@testing-library/react'; |
| 3 | +import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; |
| 4 | +import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth'; |
| 5 | +import { useValidateUserPermissions } from './hooks'; |
| 6 | + |
| 7 | +jest.mock('@edx/frontend-platform/auth', () => ({ |
| 8 | + getAuthenticatedHttpClient: jest.fn(), |
| 9 | +})); |
| 10 | + |
| 11 | +const createWrapper = () => { |
| 12 | + const queryClient = new QueryClient({ |
| 13 | + defaultOptions: { |
| 14 | + queries: { |
| 15 | + retry: false, |
| 16 | + }, |
| 17 | + }, |
| 18 | + }); |
| 19 | + |
| 20 | + const wrapper = ({ children }: { children: ReactNode }) => ( |
| 21 | + <QueryClientProvider client={queryClient}> |
| 22 | + {children} |
| 23 | + </QueryClientProvider> |
| 24 | + ); |
| 25 | + |
| 26 | + return wrapper; |
| 27 | +}; |
| 28 | + |
| 29 | +const permissions = [ |
| 30 | + { |
| 31 | + action: 'act:read', |
| 32 | + object: 'lib:test-lib', |
| 33 | + scope: 'org:OpenedX', |
| 34 | + }, |
| 35 | +]; |
| 36 | + |
| 37 | +const mockValidPermissions = [ |
| 38 | + { action: 'act:read', object: 'lib:test-lib', allowed: true }, |
| 39 | +]; |
| 40 | + |
| 41 | +const mockInvalidPermissions = [ |
| 42 | + { action: 'act:read', object: 'lib:test-lib', allowed: false }, |
| 43 | +]; |
| 44 | + |
| 45 | +describe('useValidateUserPermissions', () => { |
| 46 | + beforeEach(() => { |
| 47 | + jest.clearAllMocks(); |
| 48 | + }); |
| 49 | + |
| 50 | + it('returns allowed true when permissions are valid', async () => { |
| 51 | + getAuthenticatedHttpClient.mockReturnValue({ |
| 52 | + post: jest.fn().mockResolvedValueOnce({ data: mockValidPermissions }), |
| 53 | + }); |
| 54 | + |
| 55 | + const { result } = renderHook(() => useValidateUserPermissions(permissions), { |
| 56 | + wrapper: createWrapper(), |
| 57 | + }); |
| 58 | + |
| 59 | + await waitFor(() => expect(result.current).toBeDefined()); |
| 60 | + await waitFor(() => expect(result.current.data).toBeDefined()); |
| 61 | + |
| 62 | + expect(getAuthenticatedHttpClient).toHaveBeenCalled(); |
| 63 | + expect(result.current.data![0].allowed).toBe(true); |
| 64 | + }); |
| 65 | + |
| 66 | + it('returns allowed false when permissions are invalid', async () => { |
| 67 | + getAuthenticatedHttpClient.mockReturnValue({ |
| 68 | + post: jest.fn().mockResolvedValue({ data: mockInvalidPermissions }), |
| 69 | + }); |
| 70 | + |
| 71 | + const { result } = renderHook(() => useValidateUserPermissions(permissions), { |
| 72 | + wrapper: createWrapper(), |
| 73 | + }); |
| 74 | + await waitFor(() => expect(result.current).toBeDefined()); |
| 75 | + await waitFor(() => expect(result.current.data).toBeDefined()); |
| 76 | + |
| 77 | + expect(getAuthenticatedHttpClient).toHaveBeenCalled(); |
| 78 | + expect(result.current.data![0].allowed).toBe(false); |
| 79 | + }); |
| 80 | + |
| 81 | + it('handles error when the API call fails', async () => { |
| 82 | + const mockError = new Error('API Error'); |
| 83 | + |
| 84 | + getAuthenticatedHttpClient.mockReturnValue({ |
| 85 | + post: jest.fn().mockRejectedValue(new Error('API Error')), |
| 86 | + }); |
| 87 | + |
| 88 | + try { |
| 89 | + act(() => { |
| 90 | + renderHook(() => useValidateUserPermissions(permissions), { |
| 91 | + wrapper: createWrapper(), |
| 92 | + }); |
| 93 | + }); |
| 94 | + } catch (error) { |
| 95 | + expect(error).toEqual(mockError); // Check for the expected error |
| 96 | + } |
| 97 | + }); |
| 98 | +}); |
0 commit comments