import { describe, it, expect, beforeEach, vi } from 'vitest'; import { useUserStore } from '../userStore'; import { authService } from '../../services/authService'; import api from '../../services/api'; vi.mock('../../services/authService', () => ({ authService: { updateProfile: vi.fn(), logout: vi.fn(), getProfile: vi.fn(), }, })); vi.mock('../../services/api', () => ({ default: { post: vi.fn(), patch: vi.fn(), delete: vi.fn(), }, })); describe('userStore', () => { beforeEach(() => { vi.clearAllMocks(); useUserStore.setState({ role: 'User_Guest', isLoggedIn: false, profile: { firstName: '', lastName: '', email: '', mobile: '', walletBalance: 0, charityDonationTotal: 0, addresses: [], transactions: [], }, }); }); it('should logout correctly', () => { useUserStore.setState({ role: 'User_PetOwner', isLoggedIn: true, profile: { firstName: 'Ali', lastName: 'Ahmadi', email: 'ali@example.com', mobile: '0912', walletBalance: 100, charityDonationTotal: 10, addresses: [], transactions: [], }, }); useUserStore.getState().logout(); expect(authService.logout).toHaveBeenCalled(); const state = useUserStore.getState(); expect(state.isLoggedIn).toBe(false); expect(state.role).toBe('User_Guest'); expect(state.profile.firstName).toBe(''); }); it('should fetch profile successfully', async () => { const mockProfileData = { firstName: 'Ali', lastName: 'Ahmadi', email: 'ali@example.com', mobile: '09123456789', role: 'User_PetOwner', walletBalance: '1500.00', charityDonationTotal: '50.00', addresses: [], pets: [], walletTransactions: [], }; vi.mocked(authService.getProfile).mockResolvedValue(mockProfileData); const store = useUserStore.getState(); await store.fetchProfile(); expect(authService.getProfile).toHaveBeenCalled(); const state = useUserStore.getState(); expect(state.isLoggedIn).toBe(true); expect(state.role).toBe('User_PetOwner'); expect(state.profile.firstName).toBe('Ali'); expect(state.profile.walletBalance).toBe(1500); }); it('should update profile correctly', async () => { const mockUpdatedData = { firstName: 'Ali Updated', lastName: 'Ahmadi', email: 'ali@example.com', mobile: '09123456789', }; vi.mocked(authService.updateProfile).mockResolvedValue(mockUpdatedData); const store = useUserStore.getState(); await store.updateProfile({ firstName: 'Ali Updated' }); expect(authService.updateProfile).toHaveBeenCalledWith({ firstName: 'Ali Updated' }); const state = useUserStore.getState(); expect(state.profile.firstName).toBe('Ali Updated'); }); it('should add address and call fetchProfile', async () => { const address = { id: 'addr-1', title: 'Home', receptorName: 'Ali', phone: '0912', province: 'Teh', city: 'Teh', detail: 'Det', zipCode: '123', isDefault: false, }; const fetchSpy = vi.spyOn(useUserStore.getState(), 'fetchProfile').mockResolvedValue(undefined); vi.mocked(api.post).mockResolvedValue({ data: { success: true } }); const store = useUserStore.getState(); await store.addAddress(address); expect(api.post).toHaveBeenCalled(); expect(fetchSpy).toHaveBeenCalled(); }); });