Add centralized API client with axios interceptors, remove vue-axios

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-06 12:44:33 +08:00
parent 6af7253d08
commit 147b16ca34
29 changed files with 301 additions and 270 deletions

View File

@@ -5,18 +5,19 @@ vi.mock('@/module/apiError.js', () => ({
default: vi.fn(),
}));
const { mockGet } = vi.hoisted(() => ({ mockGet: vi.fn() }));
vi.mock('@/api/client.js', () => ({
default: { get: mockGet },
}));
import useCompareStore from '@/stores/compare.js';
describe('compareStore', () => {
let store;
const mockAxios = {
get: vi.fn(),
};
beforeEach(() => {
setActivePinia(createPinia());
store = useCompareStore();
store.$axios = mockAxios;
vi.clearAllMocks();
});
@@ -33,19 +34,19 @@ describe('compareStore', () => {
it('fetches compare data with encoded params', async () => {
const params = [{ type: 'log', id: 1 }];
const mockData = { time: {}, freq: {} };
mockAxios.get.mockResolvedValue({ data: mockData });
mockGet.mockResolvedValue({ data: mockData });
await store.getCompare(params);
const encoded = encodeURIComponent(JSON.stringify(params));
expect(mockAxios.get).toHaveBeenCalledWith(
expect(mockGet).toHaveBeenCalledWith(
`/api/compare?datasets=${encoded}`,
);
expect(store.allCompareDashboardData).toEqual(mockData);
});
it('does not throw on API failure', async () => {
mockAxios.get.mockRejectedValue(new Error('fail'));
mockGet.mockRejectedValue(new Error('fail'));
await expect(store.getCompare([]))
.resolves.not.toThrow();
@@ -54,26 +55,26 @@ describe('compareStore', () => {
describe('getStateData', () => {
it('fetches log discover stats', async () => {
mockAxios.get.mockResolvedValue({
mockGet.mockResolvedValue({
data: { stats: { cases: 100 } },
});
const result = await store.getStateData('log', 1);
expect(mockAxios.get).toHaveBeenCalledWith(
expect(mockGet).toHaveBeenCalledWith(
'/api/logs/1/discover',
);
expect(result).toEqual({ cases: 100 });
});
it('fetches filter discover stats', async () => {
mockAxios.get.mockResolvedValue({
mockGet.mockResolvedValue({
data: { stats: { cases: 50 } },
});
const result = await store.getStateData('filter', 3);
expect(mockAxios.get).toHaveBeenCalledWith(
expect(mockGet).toHaveBeenCalledWith(
'/api/filters/3/discover',
);
expect(result).toEqual({ cases: 50 });
@@ -82,7 +83,7 @@ describe('compareStore', () => {
describe('getFileName', () => {
it('finds file name by id', async () => {
mockAxios.get.mockResolvedValue({
mockGet.mockResolvedValue({
data: [
{ id: 1, name: 'file1.csv' },
{ id: 2, name: 'file2.csv' },
@@ -95,7 +96,7 @@ describe('compareStore', () => {
});
it('returns undefined for non-existent id', async () => {
mockAxios.get.mockResolvedValue({
mockGet.mockResolvedValue({
data: [{ id: 1, name: 'file1.csv' }],
});