import { describe, it, expect, vi, beforeEach } from 'vitest' import { setIsLogged } from '#lib/utils' import { useAuth } from '../use-auth' import { AppStorageEnum } from '#lib/enums' const mockNadal = { auth: { signIn: vi.fn().mockResolvedValue({ token: 'test-token' }), signUp: vi.fn().mockResolvedValue({ token: 'test-token' }), signOut: vi.fn().mockResolvedValue({ success: true }), }, } const mockPromotion = { fetchUserPromotion: vi.fn(), } vi.mock('#lib/stores', () => ({ useUserStore: () => ({ setUser: vi.fn(), resetUser: vi.fn(), }), })) vi.mock('#lib/composables', () => ({ useResetStore: () => ({ all: vi.fn(), }), usePromotion: () => mockPromotion, useNadal: () => mockNadal, })) vi.mock('#lib/utils', () => ({ setIsLogged: vi.fn(), })) vi.mock('nuxt/app', () => ({ useNuxtApp: vi.fn().mockReturnValue({ $appCookies: vi.mocked({ setTokenCookie: vi.fn(), token: { value: null, }, }), }), useCookie: vi.fn(), })) describe('useAuth', () => { let auth: any beforeEach(() => { auth = useAuth() }) it('should open the authentication modal', () => { auth.openAuthModal() expect(auth.isOpenAuthentication.value).toBe(true) }) it('should open the expired modal', () => { auth.openExpiredModal() expect(auth.isOpenExpiredModal.value).toBe(true) }) it('should handle login', async () => { const requestUser = { username: 'test', password: 'test' } window.dispatchEvent = vi.fn() await auth.onLogin(requestUser) expect(setIsLogged).toHaveBeenCalledWith(true) expect(window.dispatchEvent).toHaveBeenCalledWith(new Event(AppStorageEnum.Storage)) expect(mockPromotion.fetchUserPromotion).toHaveBeenCalledTimes(1) }) it('should handle logout', async () => { await auth.onLogout() }) it('should handle registration', async () => { const data = { username: 'test', password: 'test' } const mockUser = { token: 'test-token' } await auth.onRegister(data) }) })