[fix] Update firebaseAuthStore tests to mock axios client

- Move axios mock setup before store import to ensure client is mocked
- Set up default successful responses for customer API endpoints
- Fix TypeScript issues with isAxiosError mock type
This commit is contained in:
bymyself
2025-08-15 14:40:29 -07:00
parent e19f0b2da9
commit 81ddfc2f58

View File

@@ -1,3 +1,4 @@
import axios from 'axios'
import * as firebaseAuth from 'firebase/auth'
import { createPinia, setActivePinia } from 'pinia'
import { beforeEach, describe, expect, it, vi } from 'vitest'
@@ -5,6 +6,25 @@ import * as vuefire from 'vuefire'
import { useFirebaseAuthStore } from '@/stores/firebaseAuthStore'
// Mock axios before any imports that use it
vi.mock('axios')
const mockedAxios = vi.mocked(axios)
// Set up axios mock with default instance
const mockAxiosInstance = {
get: vi.fn().mockResolvedValue({
data: { balance: { credits: 0 } },
status: 200
}),
post: vi.fn().mockResolvedValue({
data: { id: 'test-customer-id' },
status: 201
})
}
mockedAxios.create = vi.fn().mockReturnValue(mockAxiosInstance)
mockedAxios.isAxiosError = vi.fn().mockImplementation(() => false) as any
// Mock fetch
const mockFetch = vi.fn()
vi.stubGlobal('fetch', mockFetch)
@@ -91,7 +111,18 @@ describe('useFirebaseAuthStore', () => {
}
beforeEach(() => {
vi.resetAllMocks()
vi.clearAllMocks()
// Reset axios mock responses to defaults
mockAxiosInstance.get.mockResolvedValue({
data: { balance: { credits: 0 } },
status: 200
})
mockAxiosInstance.post.mockResolvedValue({
data: { id: 'test-customer-id' },
status: 201
})
mockedAxios.isAxiosError = vi.fn().mockImplementation(() => false) as any
// Mock useFirebaseAuth to return our mock auth object
vi.mocked(vuefire.useFirebaseAuth).mockReturnValue(mockAuth as any)