import { api } from '@/lib/api'; // Mock localStorage const localStorageMock = (() => { let store: Record = {}; return { getItem: (key: string) => store[key] || null, setItem: (key: string, value: string) => { store[key] = value.toString(); }, removeItem: (key: string) => { delete store[key]; }, clear: () => { store = {}; }, }; })(); Object.defineProperty(window, 'localStorage', { value: localStorageMock, }); // Mock crypto.getRandomValues Object.defineProperty(global, 'crypto', { value: { getRandomValues: (arr: Uint8Array) => { for (let i = 0; i < arr.length; i++) { arr[i] = Math.floor(Math.random() * 256); } return arr; }, }, }); describe('API Service', () => { beforeEach(() => { localStorageMock.clear(); jest.clearAllMocks(); }); describe('Token Management', () => { it('should save and retrieve access token', async () => { const testToken = 'test-jwt-token'; localStorageMock.setItem('accessToken', testToken); const token = await api.getToken(); expect(token).toBe(testToken); }); it('should return null when no token exists', async () => { const token = await api.getToken(); expect(token).toBeNull(); }); }); describe('Email Management', () => { it('should save and retrieve email', async () => { const testEmail = 'test@example.com'; await api.saveEmail(testEmail); const email = await api.getStoredEmail(); expect(email).toBe(testEmail); }); it('should return null when no email is stored', async () => { const email = await api.getStoredEmail(); expect(email).toBeNull(); }); }); describe('Onboarding Status', () => { it('should save and retrieve onboarding completed flag', async () => { await api.setOnboardingCompleted(true); let isCompleted = await api.isOnboardingCompleted(); expect(isCompleted).toBe(true); await api.setOnboardingCompleted(false); isCompleted = await api.isOnboardingCompleted(); expect(isCompleted).toBe(false); }); it('should return false when onboarding flag is not set', async () => { const isCompleted = await api.isOnboardingCompleted(); expect(isCompleted).toBe(false); }); }); describe('Authentication Status', () => { it('should return true when token exists', async () => { localStorageMock.setItem('accessToken', 'test-token'); const isAuth = await api.isAuthenticated(); expect(isAuth).toBe(true); }); it('should return false when token does not exist', async () => { const isAuth = await api.isAuthenticated(); expect(isAuth).toBe(false); }); }); describe('Logout', () => { it('should clear all stored data', async () => { // Set up some data localStorageMock.setItem('accessToken', 'token'); localStorageMock.setItem('userId', '123'); localStorageMock.setItem('userEmail', 'test@example.com'); localStorageMock.setItem('legacyAccessToken', 'legacy-token'); await api.logout(); // Verify all data is cleared expect(localStorageMock.getItem('accessToken')).toBeNull(); expect(localStorageMock.getItem('userId')).toBeNull(); expect(localStorageMock.getItem('userEmail')).toBeNull(); expect(localStorageMock.getItem('legacyAccessToken')).toBeNull(); }); }); describe('Check Email', () => { it('should call the API with correct endpoint', async () => { global.fetch = jest.fn(() => Promise.resolve({ ok: true, json: () => Promise.resolve({ exists: true }), }) ) as jest.Mock; const result = await api.checkEmail('test@example.com'); expect(global.fetch).toHaveBeenCalledWith( 'https://wellnuo.smartlaunchhub.com/api/auth/check-email', expect.objectContaining({ method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ email: 'test@example.com' }), }) ); expect(result.ok).toBe(true); expect(result.data?.exists).toBe(true); }); it('should handle network errors', async () => { global.fetch = jest.fn(() => Promise.reject(new Error('Network error')) ) as jest.Mock; const result = await api.checkEmail('test@example.com'); expect(result.ok).toBe(false); expect(result.error?.message).toContain('Network error'); }); }); });