import { describe, it, expect, vi, beforeEach } from 'vitest' import { useAccount } from '../use-account' import { useResetStore, useUser } from '#lib/composables' import { useUserStore } from '#lib/stores' import { createError, useNuxtApp, useRoute } from 'nuxt/app' import { ResponseStatus } from '#lib/enums/app' const mockNadal = { account: { updatePassword: vi.fn(), checkEmail: vi.fn(), updateProfile: vi.fn(), sendPasswordResetLink: vi.fn(), resetPassword: vi.fn(), }, } vi.mock('#lib/composables', () => ({ useUser: vi.fn().mockReturnValue({ token: { value: 'test-token', }, user: { value: { id: 1, email: 'new@example.com', }, }, }), useResetStore: vi.fn().mockReturnValue({ all: vi.fn(), }), useRoute: vi.fn(), useNadal: () => mockNadal, })) vi.mock('#lib/stores', () => ({ useUserStore: vi.fn().mockReturnValue({ setUser: vi.fn(), }), })) vi.mock('nuxt/app', () => ({ useNuxtApp: vi.fn().mockReturnValue({ $appCookies: vi.mocked({ getTokenCookie: vi.fn(), setTokenCookie: vi.fn(), token: { value: null, }, }), }), useRoute: vi.fn().mockReturnValue({}), createError: vi.fn(), })) describe('use-account', () => { let account: any let resetStore: any let store: any let $appCookies: any let user: any let route: any beforeEach(() => { account = useAccount() resetStore = useResetStore() store = useUserStore() user = useUser().user route = useRoute() $appCookies = useNuxtApp().$appCookies }) it('should update password and reset store', async () => { const data = { oldPassword: 'old', newPassword: 'new' } await account.onUpdatePassword(data) expect(mockNadal.account.updatePassword).toHaveBeenCalledWith( { newPassword: 'new', oldPassword: 'old' }, 'test-token', ) }) it('should throw an error if email already exists', async () => { const data = { email: 'existing@example.com' } mockNadal.account.checkEmail.mockResolvedValueOnce(true) // @ts-ignore createError.mockReturnValueOnce(new Error('EMAIL_ALREADY_EXIST')) await expect(account.onUpdateProfile(data)).rejects.toThrow( 'EMAIL_ALREADY_EXIST', ) }) it('should update profile successfully', async () => { const data = { email: 'new@example.com', name: 'New Name' } mockNadal.account.checkEmail.mockResolvedValueOnce(false) mockNadal.account.updateProfile.mockResolvedValueOnce(ResponseStatus.OK) await account.onUpdateProfile(data) expect(store.setUser).toHaveBeenCalledWith({ ...user.value, ...data, }) }) it('should send password reset link successfully', async () => { const email = 'test@example.com' await account.onForgotPassword(email) expect(mockNadal.account.sendPasswordResetLink).toHaveBeenCalled() }) it('should throw an error if sending password reset link fails', async () => { const email = 'test@example.com' mockNadal.account.sendPasswordResetLink = vi .fn() .mockRejectedValueOnce(new Error('SEND_RESET_LINK_FAILED')) await expect(account.onForgotPassword(email)).rejects.toThrow( 'SEND_RESET_LINK_FAILED', ) }) it('should reset password successfully', async () => { const data = { password: 'new-password' } const token = 'reset-token' route.query = { token } mockNadal.account.resetPassword.mockResolvedValueOnce({}) await account.onResetPassword(data) expect(mockNadal.account.resetPassword).toHaveBeenCalledWith(token, data) expect(resetStore.all).toHaveBeenCalled() }) it('should throw an error if resetting password fails', async () => { const data = { password: 'new-password' } const token = 'reset-token' route.query = { token } mockNadal.account.resetPassword.mockRejectedValueOnce( new Error('RESET_PASSWORD_FAILED'), ) await expect(account.onResetPassword(data)).rejects.toThrow( 'RESET_PASSWORD_FAILED', ) }) })