|
| 1 | +import { useAuthStore } from '../authStore'; |
| 2 | +import type { User } from '../../types'; |
| 3 | + |
| 4 | +describe('useAuthStore', () => { |
| 5 | + const testUser: User = { |
| 6 | + id: 'merchant-1', |
| 7 | + email: 'merchant@example.com', |
| 8 | + name: 'Merchant User', |
| 9 | + role: 'merchant', |
| 10 | + businessName: 'Merchant Co', |
| 11 | + }; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + useAuthStore.setState({ |
| 15 | + user: null, |
| 16 | + token: null, |
| 17 | + role: null, |
| 18 | + isAuthenticated: false, |
| 19 | + }); |
| 20 | + localStorage.clear(); |
| 21 | + jest.restoreAllMocks(); |
| 22 | + }); |
| 23 | + |
| 24 | + it('starts with null auth data and unauthenticated defaults', () => { |
| 25 | + expect(useAuthStore.getState()).toMatchObject({ |
| 26 | + user: null, |
| 27 | + token: null, |
| 28 | + role: null, |
| 29 | + isAuthenticated: false, |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + it('login stores the user, token, role, and authenticated state in memory', () => { |
| 34 | + useAuthStore.getState().login('session-token', testUser); |
| 35 | + |
| 36 | + expect(useAuthStore.getState()).toMatchObject({ |
| 37 | + user: testUser, |
| 38 | + token: 'session-token', |
| 39 | + role: 'merchant', |
| 40 | + isAuthenticated: true, |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + it('logout clears auth state and asks the session endpoint to clear the auth cookie', () => { |
| 45 | + const fetchMock = jest.fn().mockResolvedValue({ ok: true }); |
| 46 | + global.fetch = fetchMock; |
| 47 | + useAuthStore.getState().login('session-token', testUser); |
| 48 | + |
| 49 | + useAuthStore.getState().logout(); |
| 50 | + |
| 51 | + expect(useAuthStore.getState()).toMatchObject({ |
| 52 | + user: null, |
| 53 | + token: null, |
| 54 | + role: null, |
| 55 | + isAuthenticated: false, |
| 56 | + }); |
| 57 | + expect(fetchMock).toHaveBeenCalledWith('/api/auth/session', { |
| 58 | + method: 'DELETE', |
| 59 | + credentials: 'include', |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + it('logout still clears auth state when the session endpoint request fails', () => { |
| 64 | + global.fetch = jest.fn().mockRejectedValue(new Error('network unavailable')); |
| 65 | + useAuthStore.getState().login('session-token', testUser); |
| 66 | + |
| 67 | + expect(() => useAuthStore.getState().logout()).not.toThrow(); |
| 68 | + |
| 69 | + expect(useAuthStore.getState()).toMatchObject({ |
| 70 | + user: null, |
| 71 | + token: null, |
| 72 | + role: null, |
| 73 | + isAuthenticated: false, |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + it('persist partialize only stores the non-sensitive role', () => { |
| 78 | + const partialize = useAuthStore.persist.getOptions().partialize; |
| 79 | + |
| 80 | + expect(partialize).toBeDefined(); |
| 81 | + expect( |
| 82 | + partialize?.({ |
| 83 | + ...useAuthStore.getState(), |
| 84 | + user: testUser, |
| 85 | + token: 'session-token', |
| 86 | + role: 'admin', |
| 87 | + isAuthenticated: true, |
| 88 | + }) |
| 89 | + ).toEqual({ role: 'admin' }); |
| 90 | + }); |
| 91 | +}); |
0 commit comments