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,21 +5,22 @@ vi.mock('@/module/apiError.js', () => ({
default: vi.fn(),
}));
const { mockGet, mockPost, mockPut } = vi.hoisted(() => ({
mockGet: vi.fn(), mockPost: vi.fn(), mockPut: vi.fn(),
}));
vi.mock('@/api/client.js', () => ({
default: { get: mockGet, post: mockPost, put: mockPut },
}));
import apiError from '@/module/apiError.js';
import useConformanceStore from '@/stores/conformance.js';
describe('conformanceStore', () => {
let store;
const mockAxios = {
get: vi.fn(),
post: vi.fn(),
put: vi.fn(),
};
beforeEach(() => {
setActivePinia(createPinia());
store = useConformanceStore();
store.$axios = mockAxios;
vi.clearAllMocks();
});
@@ -42,11 +43,11 @@ describe('conformanceStore', () => {
waiting_time: {},
cycle_time: {},
};
mockAxios.get.mockResolvedValue({ data: mockData });
mockGet.mockResolvedValue({ data: mockData });
await store.getConformanceParams();
expect(mockAxios.get).toHaveBeenCalledWith(
expect(mockGet).toHaveBeenCalledWith(
'/api/log-checks/params?log_id=1',
);
expect(store.allConformanceTask).toEqual([{ label: 'A' }]);
@@ -63,11 +64,11 @@ describe('conformanceStore', () => {
waiting_time: {},
cycle_time: {},
};
mockAxios.get.mockResolvedValue({ data: mockData });
mockGet.mockResolvedValue({ data: mockData });
await store.getConformanceParams();
expect(mockAxios.get).toHaveBeenCalledWith(
expect(mockGet).toHaveBeenCalledWith(
'/api/filter-checks/params?filter_id=5',
);
});
@@ -77,11 +78,11 @@ describe('conformanceStore', () => {
it('posts to log temp-check and stores id', async () => {
store.conformanceLogId = 1;
store.conformanceFilterId = null;
mockAxios.post.mockResolvedValue({ data: { id: 42 } });
mockPost.mockResolvedValue({ data: { id: 42 } });
await store.addConformanceCheckId({ rule: 'test' });
expect(mockAxios.post).toHaveBeenCalledWith(
expect(mockPost).toHaveBeenCalledWith(
'/api/temp-log-checks?log_id=1',
{ rule: 'test' },
);
@@ -90,11 +91,11 @@ describe('conformanceStore', () => {
it('posts to filter temp-check when filter set', async () => {
store.conformanceFilterId = 3;
mockAxios.post.mockResolvedValue({ data: { id: 99 } });
mockPost.mockResolvedValue({ data: { id: 99 } });
await store.addConformanceCheckId({ rule: 'test' });
expect(mockAxios.post).toHaveBeenCalledWith(
expect(mockPost).toHaveBeenCalledWith(
'/api/temp-filter-checks?filter_id=3',
{ rule: 'test' },
);
@@ -106,11 +107,11 @@ describe('conformanceStore', () => {
it('fetches temp log check report', async () => {
store.conformanceLogTempCheckId = 10;
const mockData = { file: {}, charts: {} };
mockAxios.get.mockResolvedValue({ data: mockData });
mockGet.mockResolvedValue({ data: mockData });
await store.getConformanceReport();
expect(mockAxios.get).toHaveBeenCalledWith(
expect(mockGet).toHaveBeenCalledWith(
'/api/temp-log-checks/10',
);
expect(store.allConformanceTempReportData).toEqual(mockData);
@@ -119,7 +120,7 @@ describe('conformanceStore', () => {
it('stores routeFile when getRouteFile=true', async () => {
store.conformanceLogTempCheckId = 10;
const mockData = { file: { name: 'test.csv' } };
mockAxios.get.mockResolvedValue({ data: mockData });
mockGet.mockResolvedValue({ data: mockData });
await store.getConformanceReport(true);
@@ -134,11 +135,11 @@ describe('conformanceStore', () => {
store.conformanceFilterId = null;
store.conformanceLogTempCheckId = 10;
store.conformanceRuleData = { type: 'test' };
mockAxios.post.mockResolvedValue({ data: { id: 100 } });
mockPost.mockResolvedValue({ data: { id: 100 } });
await store.addConformanceCreateCheckId('myRule');
expect(mockAxios.post).toHaveBeenCalledWith(
expect(mockPost).toHaveBeenCalledWith(
'/api/log-checks?log_id=1',
{ name: 'myRule', rule: { type: 'test' } },
);
@@ -151,11 +152,11 @@ describe('conformanceStore', () => {
it('updates existing log check', async () => {
store.conformanceLogCreateCheckId = 50;
store.conformanceRuleData = { type: 'updated' };
mockAxios.put.mockResolvedValue({ status: 200 });
mockPut.mockResolvedValue({ status: 200 });
await store.updateConformance();
expect(mockAxios.put).toHaveBeenCalledWith(
expect(mockPut).toHaveBeenCalledWith(
'/api/log-checks/50',
{ type: 'updated' },
);