import { describe, it, expect, vi, beforeEach } from 'vitest' import { useAuthApi } from '../use-auth-api' import { useUserStore } from '#lib/stores' import { setTokenCookie, setIsLogged } from '#lib/utils' import { useResetStore, useAuthService } from '#lib/composables' import type { UserLoginRequest } from '#lib/types' import { useNuxtApp } from 'nuxt/app' // Mock dependencies vi.mock('#lib/stores', () => ({ useUserStore: vi.fn(), })) vi.mock('#lib/utils', () => ({ setTokenCookie: vi.fn(), setIsLogged: vi.fn(), })) vi.mock('#lib/composables', () => ({ useResetStore: vi.fn(), useAuthService: vi.fn(), })) vi.mock('nuxt/app', () => ({ useNuxtApp: vi.fn().mockReturnValue({ $appCookies: vi.mocked({ token: { value: 'mock-token' } }), }), })) describe('useAuthApi', () => { let mockUserStore: any let mockAuthService: any let mockResetStore: any let $appCookies: any beforeEach(() => { vi.clearAllMocks() // Reset the mocks before each test mockUserStore = { setUser: vi.fn(), resetUser: vi.fn(), } mockAuthService = { requestLogin: vi.fn(), requestLogout: vi.fn(), } mockResetStore = { all: vi.fn(), } $appCookies = useNuxtApp().$appCookies vi.mocked(useUserStore).mockReturnValue(mockUserStore) vi.mocked(useAuthService).mockReturnValue(mockAuthService) vi.mocked(useResetStore).mockReturnValue(mockResetStore) }) it('should handle login successfully', async () => { const mockResponse = { data: { value: { data: { token: 'mock-token', name: 'John Doe', }, }, }, error: { value: null }, } mockAuthService.requestLogin.mockResolvedValue(mockResponse) const { onLogin } = useAuthApi() const mockLoginRequest: UserLoginRequest = { username: 'john', password: 'password123', } await onLogin(mockLoginRequest) // Check that token cookie is set expect($appCookies.token.value).toEqual('mock-token') // Check that user store is updated expect(mockUserStore.setUser).toHaveBeenCalledWith({ token: 'mock-token', name: 'John Doe', }) // Check that user is marked as logged in expect(setIsLogged).toHaveBeenCalledWith(true) }) it('should throw error on login failure', async () => { const mockError = new Error('Invalid credentials') const mockResponse = { data: { value: null }, error: { value: mockError }, } mockAuthService.requestLogin.mockResolvedValue(mockResponse) const { onLogin } = useAuthApi() const mockLoginRequest: UserLoginRequest = { username: 'john', password: 'wrongpassword', } await expect(onLogin(mockLoginRequest)).rejects.toThrow( 'Invalid credentials', ) // Ensure user store and cookies are not updated expect(mockUserStore.setUser).not.toHaveBeenCalled() expect(setIsLogged).not.toHaveBeenCalled() }) it('should handle logout successfully', async () => { const mockResponse = { error: { value: null }, } mockAuthService.requestLogout.mockResolvedValue(mockResponse) const { onLogout } = useAuthApi() await onLogout() // Check that reset operations are performed expect(mockResetStore.all).toHaveBeenCalled() expect(mockUserStore.resetUser).toHaveBeenCalled() // Check that token cookie is cleared expect($appCookies.token.value).toEqual(null) }) it('should throw error on logout failure', async () => { const mockError = new Error('Logout failed') const mockResponse = { error: { value: mockError }, } mockAuthService.requestLogout.mockResolvedValue(mockResponse) const { onLogout } = useAuthApi() await expect(onLogout()).rejects.toThrow('Logout failed') // Ensure reset operations are not performed expect(mockResetStore.all).not.toHaveBeenCalled() expect(mockUserStore.resetUser).not.toHaveBeenCalled() }) })