|
| 1 | +/* eslint-disable max-lines-per-function */ |
| 2 | +import { act, screen, waitFor } from '@testing-library/react-native'; |
| 3 | +import React from 'react'; |
| 4 | + |
| 5 | +import { render } from '@/core/test-utils'; |
| 6 | + |
| 7 | +import { AuthProvider, authStorage, HEADER_KEYS, useAuth } from './auth'; |
| 8 | + |
| 9 | +jest.mock('@/api', () => { |
| 10 | + const originalModule = jest.requireActual('@/api'); // Import the original module |
| 11 | + const mockStore: Record<string, string> = {}; |
| 12 | + |
| 13 | + return { |
| 14 | + ...originalModule, // Spread the original module to keep other exports |
| 15 | + authStorage: { |
| 16 | + getString: jest.fn((key: string) => mockStore[key] || null), |
| 17 | + set: jest.fn((key: string, value: string) => { |
| 18 | + mockStore[key] = value; |
| 19 | + }), |
| 20 | + delete: jest.fn((key: string) => { |
| 21 | + delete mockStore[key]; |
| 22 | + }), |
| 23 | + }, |
| 24 | + client: { |
| 25 | + interceptors: { |
| 26 | + request: { use: jest.fn(), eject: jest.fn() }, |
| 27 | + response: { use: jest.fn(), eject: jest.fn() }, |
| 28 | + }, |
| 29 | + }, |
| 30 | + }; |
| 31 | +}); |
| 32 | + |
| 33 | +const TestComponent: React.FC = () => { |
| 34 | + const { token, isAuthenticated, loading, ready, logout } = useAuth(); |
| 35 | + |
| 36 | + return ( |
| 37 | + <div> |
| 38 | + <p data-testid="token">{token}</p> |
| 39 | + <p data-testid="isAuthenticated">{isAuthenticated ? 'true' : 'false'}</p> |
| 40 | + <p data-testid="loading">{loading ? 'true' : 'false'}</p> |
| 41 | + <p data-testid="ready">{ready ? 'true' : 'false'}</p> |
| 42 | + <button data-testid="logout" onClick={logout}> |
| 43 | + Logout |
| 44 | + </button> |
| 45 | + </div> |
| 46 | + ); |
| 47 | +}; |
| 48 | + |
| 49 | +describe('AuthProvider', () => { |
| 50 | + render( |
| 51 | + <AuthProvider> |
| 52 | + <TestComponent /> |
| 53 | + </AuthProvider>, |
| 54 | + ); |
| 55 | + |
| 56 | + afterEach(() => { |
| 57 | + jest.clearAllMocks(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should initialize with loading and ready states', async () => { |
| 61 | + (authStorage.getString as jest.Mock).mockImplementation((key) => { |
| 62 | + if (key === HEADER_KEYS.ACCESS_TOKEN) { |
| 63 | + return 'mockToken'; |
| 64 | + } |
| 65 | + if (key === HEADER_KEYS.EXPIRY) { |
| 66 | + return '2100-01-01T00:00:00.000Z'; |
| 67 | + } |
| 68 | + return null; |
| 69 | + }); |
| 70 | + |
| 71 | + expect(screen.getByTestId('loading').textContent).toBe('true'); |
| 72 | + expect(screen.getByTestId('ready').textContent).toBe('false'); |
| 73 | + |
| 74 | + await waitFor(() => |
| 75 | + expect(screen.getByTestId('loading').textContent).toBe('false'), |
| 76 | + ); |
| 77 | + expect(screen.getByTestId('ready').textContent).toBe('true'); |
| 78 | + expect(screen.getByTestId('isAuthenticated').textContent).toBe('true'); |
| 79 | + expect(screen.getByTestId('token').textContent).toBe('mockToken'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should handle expired token', async () => { |
| 83 | + (authStorage.getString as jest.Mock).mockImplementation((key) => { |
| 84 | + if (key === HEADER_KEYS.ACCESS_TOKEN) { |
| 85 | + return 'expiredToken'; |
| 86 | + } |
| 87 | + if (key === HEADER_KEYS.EXPIRY) { |
| 88 | + return '2000-01-01T00:00:00.000Z'; |
| 89 | + } |
| 90 | + return null; |
| 91 | + }); |
| 92 | + |
| 93 | + await waitFor(() => |
| 94 | + expect(screen.getByTestId('loading').textContent).toBe('false'), |
| 95 | + ); |
| 96 | + expect(screen.getByTestId('isAuthenticated').textContent).toBe('false'); |
| 97 | + expect(screen.getByTestId('token').textContent).toBe(''); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should clear storage and state on logout', async () => { |
| 101 | + (authStorage.getString as jest.Mock).mockImplementation((key) => { |
| 102 | + if (key === HEADER_KEYS.ACCESS_TOKEN) { |
| 103 | + return 'mockToken'; |
| 104 | + } |
| 105 | + if (key === HEADER_KEYS.EXPIRY) { |
| 106 | + return '2100-01-01T00:00:00.000Z'; |
| 107 | + } |
| 108 | + return null; |
| 109 | + }); |
| 110 | + |
| 111 | + await waitFor(() => |
| 112 | + expect(screen.getByTestId('loading').textContent).toBe('false'), |
| 113 | + ); |
| 114 | + |
| 115 | + act(() => { |
| 116 | + screen.getByTestId('logout').click(); |
| 117 | + }); |
| 118 | + |
| 119 | + expect(authStorage.delete).toHaveBeenCalledWith(HEADER_KEYS.ACCESS_TOKEN); |
| 120 | + expect(authStorage.delete).toHaveBeenCalledWith(HEADER_KEYS.REFRESH_TOKEN); |
| 121 | + expect(authStorage.delete).toHaveBeenCalledWith(HEADER_KEYS.USER_ID); |
| 122 | + expect(authStorage.delete).toHaveBeenCalledWith(HEADER_KEYS.EXPIRY); |
| 123 | + |
| 124 | + expect(screen.getByTestId('isAuthenticated').textContent).toBe('false'); |
| 125 | + expect(screen.getByTestId('token').textContent).toBe(''); |
| 126 | + }); |
| 127 | +}); |
0 commit comments