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,6 +5,13 @@ vi.mock('@/module/apiError.js', () => ({
default: vi.fn(),
}));
const { mockGet, mockPost, mockPut, mockDelete } = vi.hoisted(() => ({
mockGet: vi.fn(), mockPost: vi.fn(), mockPut: vi.fn(), mockDelete: vi.fn(),
}));
vi.mock('@/api/client.js', () => ({
default: { get: mockGet, post: mockPost, put: mockPut, delete: mockDelete },
}));
// Mock login store to avoid its side effects
vi.mock('@/stores/login.ts', () => {
const { defineStore } = require('pinia');
@@ -24,17 +31,10 @@ import useAcctMgmtStore from '@/stores/acctMgmt.ts';
describe('acctMgmtStore', () => {
let store;
const mockAxios = {
get: vi.fn(),
post: vi.fn(),
put: vi.fn(),
delete: vi.fn(),
};
beforeEach(() => {
setActivePinia(createPinia());
store = useAcctMgmtStore();
store.$axios = mockAxios;
vi.clearAllMocks();
});
@@ -84,12 +84,12 @@ describe('acctMgmtStore', () => {
describe('createNewAccount', () => {
it('posts to /api/users and sets flag on success', async () => {
mockAxios.post.mockResolvedValue({ status: 200 });
mockPost.mockResolvedValue({ status: 200 });
const user = { username: 'newuser', password: 'pass' };
await store.createNewAccount(user);
expect(mockAxios.post).toHaveBeenCalledWith(
expect(mockPost).toHaveBeenCalledWith(
'/api/users', user,
);
expect(store.isOneAccountJustCreate).toBe(true);
@@ -99,18 +99,18 @@ describe('acctMgmtStore', () => {
describe('deleteAccount', () => {
it('returns true on success', async () => {
mockAxios.delete.mockResolvedValue({ status: 200 });
mockDelete.mockResolvedValue({ status: 200 });
const result = await store.deleteAccount('alice');
expect(mockAxios.delete).toHaveBeenCalledWith(
expect(mockDelete).toHaveBeenCalledWith(
'/api/users/alice',
);
expect(result).toBe(true);
});
it('returns false on error', async () => {
mockAxios.delete.mockRejectedValue(new Error('fail'));
mockDelete.mockRejectedValue(new Error('fail'));
const result = await store.deleteAccount('alice');
@@ -120,7 +120,7 @@ describe('acctMgmtStore', () => {
describe('editAccount', () => {
it('puts edited data', async () => {
mockAxios.put.mockResolvedValue({ status: 200 });
mockPut.mockResolvedValue({ status: 200 });
const detail = {
username: 'alice',
password: 'newpw',
@@ -130,7 +130,7 @@ describe('acctMgmtStore', () => {
const result = await store.editAccount('alice', detail);
expect(mockAxios.put).toHaveBeenCalledWith(
expect(mockPut).toHaveBeenCalledWith(
'/api/users/alice',
expect.objectContaining({ password: 'newpw' }),
);
@@ -140,11 +140,11 @@ describe('acctMgmtStore', () => {
describe('addRoleToUser', () => {
it('puts role assignment', async () => {
mockAxios.put.mockResolvedValue({ status: 200 });
mockPut.mockResolvedValue({ status: 200 });
const result = await store.addRoleToUser('alice', 'admin');
expect(mockAxios.put).toHaveBeenCalledWith(
expect(mockPut).toHaveBeenCalledWith(
'/api/users/alice/roles/admin',
);
expect(result).toBe(true);
@@ -153,11 +153,11 @@ describe('acctMgmtStore', () => {
describe('deleteRoleToUser', () => {
it('deletes role', async () => {
mockAxios.delete.mockResolvedValue({ status: 200 });
mockDelete.mockResolvedValue({ status: 200 });
const result = await store.deleteRoleToUser('alice', 'admin');
expect(mockAxios.delete).toHaveBeenCalledWith(
expect(mockDelete).toHaveBeenCalledWith(
'/api/users/alice/roles/admin',
);
expect(result).toBe(true);
@@ -166,7 +166,7 @@ describe('acctMgmtStore', () => {
describe('getUserDetail', () => {
it('fetches user and sets admin flag', async () => {
mockAxios.get.mockResolvedValue({
mockGet.mockResolvedValue({
status: 200,
data: {
username: 'alice',
@@ -181,7 +181,7 @@ describe('acctMgmtStore', () => {
});
it('returns false on error', async () => {
mockAxios.get.mockRejectedValue(new Error('not found'));
mockGet.mockRejectedValue(new Error('not found'));
const result = await store.getUserDetail('ghost');

View File

@@ -5,20 +5,21 @@ 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 useAllMapDataStore from '@/stores/allMapData.js';
describe('allMapDataStore', () => {
let store;
const mockAxios = {
get: vi.fn(),
post: vi.fn(),
put: vi.fn(),
};
beforeEach(() => {
setActivePinia(createPinia());
store = useAllMapDataStore();
store.$axios = mockAxios;
vi.clearAllMocks();
});
@@ -37,11 +38,11 @@ describe('allMapDataStore', () => {
stats: { cases: 10 },
insights: {},
};
mockAxios.get.mockResolvedValue({ data: mockData });
mockGet.mockResolvedValue({ data: mockData });
await store.getAllMapData();
expect(mockAxios.get).toHaveBeenCalledWith(
expect(mockGet).toHaveBeenCalledWith(
'/api/logs/1/discover',
);
expect(store.allProcessMap).toEqual({ nodes: [] });
@@ -51,7 +52,7 @@ describe('allMapDataStore', () => {
it('fetches temp filter discover data when set', async () => {
store.logId = 1;
store.tempFilterId = 5;
mockAxios.get.mockResolvedValue({
mockGet.mockResolvedValue({
data: {
process_map: {},
bpmn: {},
@@ -62,7 +63,7 @@ describe('allMapDataStore', () => {
await store.getAllMapData();
expect(mockAxios.get).toHaveBeenCalledWith(
expect(mockGet).toHaveBeenCalledWith(
'/api/temp-filters/5/discover',
);
});
@@ -70,7 +71,7 @@ describe('allMapDataStore', () => {
it('fetches created filter discover data', async () => {
store.logId = 1;
store.createFilterId = 3;
mockAxios.get.mockResolvedValue({
mockGet.mockResolvedValue({
data: {
process_map: {},
bpmn: {},
@@ -81,14 +82,14 @@ describe('allMapDataStore', () => {
await store.getAllMapData();
expect(mockAxios.get).toHaveBeenCalledWith(
expect(mockGet).toHaveBeenCalledWith(
'/api/filters/3/discover',
);
});
it('does not throw on API failure', async () => {
store.logId = 1;
mockAxios.get.mockRejectedValue(new Error('fail'));
mockGet.mockRejectedValue(new Error('fail'));
await expect(store.getAllMapData())
.resolves.not.toThrow();
@@ -111,11 +112,11 @@ describe('allMapDataStore', () => {
trace: [],
attrs: [],
};
mockAxios.get.mockResolvedValue({ data: mockData });
mockGet.mockResolvedValue({ data: mockData });
await store.getFilterParams();
expect(mockAxios.get).toHaveBeenCalledWith(
expect(mockGet).toHaveBeenCalledWith(
'/api/filters/params?log_id=1',
);
expect(store.allFilterTask).toEqual(['A', 'B']);
@@ -129,13 +130,13 @@ describe('allMapDataStore', () => {
it('posts rule data and stores result', async () => {
store.logId = 1;
store.postRuleData = [{ type: 'task' }];
mockAxios.post.mockResolvedValue({
mockPost.mockResolvedValue({
data: { result: true },
});
await store.checkHasResult();
expect(mockAxios.post).toHaveBeenCalledWith(
expect(mockPost).toHaveBeenCalledWith(
'/api/filters/has-result?log_id=1',
[{ type: 'task' }],
);
@@ -147,7 +148,7 @@ describe('allMapDataStore', () => {
it('creates temp filter and stores id', async () => {
store.logId = 1;
store.postRuleData = [];
mockAxios.post.mockResolvedValue({ data: { id: 77 } });
mockPost.mockResolvedValue({ data: { id: 77 } });
await store.addTempFilterId();
@@ -160,11 +161,11 @@ describe('allMapDataStore', () => {
store.logId = 1;
store.tempFilterId = 77;
store.postRuleData = [{ type: 'rule' }];
mockAxios.post.mockResolvedValue({ data: { id: 88 } });
mockPost.mockResolvedValue({ data: { id: 88 } });
await store.addFilterId('myFilter');
expect(mockAxios.post).toHaveBeenCalledWith(
expect(mockPost).toHaveBeenCalledWith(
'/api/filters?log_id=1',
{ name: 'myFilter', rules: [{ type: 'rule' }] },
);
@@ -178,11 +179,11 @@ describe('allMapDataStore', () => {
store.createFilterId = 88;
store.tempFilterId = 77;
store.postRuleData = [{ type: 'updated' }];
mockAxios.put.mockResolvedValue({ status: 200 });
mockPut.mockResolvedValue({ status: 200 });
await store.updateFilter();
expect(mockAxios.put).toHaveBeenCalledWith(
expect(mockPut).toHaveBeenCalledWith(
'/api/filters/88',
[{ type: 'updated' }],
);
@@ -195,7 +196,7 @@ describe('allMapDataStore', () => {
it('does not crash when baseLogId is falsy', async () => {
store.logId = 1;
store.baseLogId = null;
mockAxios.get.mockResolvedValue({ data: [{ id: 1 }] });
mockGet.mockResolvedValue({ data: [{ id: 1 }] });
await store.getAllTrace();

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' }],
});

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' },
);

View File

@@ -21,13 +21,14 @@ vi.mock('@/router/index.ts', () => ({
default: { push: vi.fn(), currentRoute: { value: { path: '/' } } },
}));
import axios from 'axios';
import useFilesStore from '@/stores/files.js';
const { mockGet, mockPost, mockPut, mockDelete } = vi.hoisted(() => ({
mockGet: vi.fn(), mockPost: vi.fn(), mockPut: vi.fn(), mockDelete: vi.fn(),
}));
vi.mock('@/api/client.js', () => ({
default: { get: mockGet, post: mockPost, put: mockPut, delete: mockDelete },
}));
vi.spyOn(axios, 'get').mockImplementation(vi.fn());
vi.spyOn(axios, 'post').mockImplementation(vi.fn());
vi.spyOn(axios, 'put').mockImplementation(vi.fn());
vi.spyOn(axios, 'delete').mockImplementation(vi.fn());
import useFilesStore from '@/stores/files.js';
describe('filesStore', () => {
let store;
@@ -63,7 +64,7 @@ describe('filesStore', () => {
describe('fetchAllFiles', () => {
it('fetches and transforms file data', async () => {
axios.get.mockResolvedValue({
mockGet.mockResolvedValue({
data: [
{
type: 'log',
@@ -85,7 +86,7 @@ describe('filesStore', () => {
await store.fetchAllFiles();
expect(axios.get).toHaveBeenCalledWith('/api/files');
expect(mockGet).toHaveBeenCalledWith('/api/files');
expect(store.allEventFiles).toHaveLength(2);
expect(store.allEventFiles[0].fileType).toBe('Log');
expect(store.allEventFiles[0].icon).toBe('work_history');
@@ -96,19 +97,19 @@ describe('filesStore', () => {
});
it('does not throw on API failure', async () => {
axios.get.mockRejectedValue(new Error('Network error'));
mockGet.mockRejectedValue(new Error('Network error'));
await expect(store.fetchAllFiles()).resolves.toBeUndefined();
});
});
describe('upload', () => {
it('uploads file and navigates to Upload page', async () => {
axios.post.mockResolvedValue({ data: { id: 42 } });
mockPost.mockResolvedValue({ data: { id: 42 } });
const formData = new FormData();
await store.upload(formData);
expect(axios.post).toHaveBeenCalledWith(
expect(mockPost).toHaveBeenCalledWith(
'/api/logs/csv-uploads',
formData,
expect.objectContaining({
@@ -123,25 +124,25 @@ describe('filesStore', () => {
describe('getUploadDetail', () => {
it('fetches upload preview', async () => {
store.uploadId = 10;
axios.get.mockResolvedValue({
mockGet.mockResolvedValue({
data: { preview: { columns: ['a', 'b'] } },
});
await store.getUploadDetail();
expect(axios.get).toHaveBeenCalledWith('/api/logs/csv-uploads/10');
expect(mockGet).toHaveBeenCalledWith('/api/logs/csv-uploads/10');
expect(store.allUploadDetail).toEqual({ columns: ['a', 'b'] });
});
});
describe('rename', () => {
it('renames a log file', async () => {
axios.put.mockResolvedValue({});
axios.get.mockResolvedValue({ data: [] });
mockPut.mockResolvedValue({});
mockGet.mockResolvedValue({ data: [] });
await store.rename('log', 5, 'new-name');
expect(axios.put).toHaveBeenCalledWith('/api/logs/5/name', {
expect(mockPut).toHaveBeenCalledWith('/api/logs/5/name', {
name: 'new-name',
});
});
@@ -149,30 +150,30 @@ describe('filesStore', () => {
describe('getDependents', () => {
it('fetches dependents for a log', async () => {
axios.get.mockResolvedValue({ data: [{ id: 1 }, { id: 2 }] });
mockGet.mockResolvedValue({ data: [{ id: 1 }, { id: 2 }] });
await store.getDependents('log', 7);
expect(axios.get).toHaveBeenCalledWith('/api/logs/7/dependents');
expect(mockGet).toHaveBeenCalledWith('/api/logs/7/dependents');
expect(store.allDependentsData).toEqual([{ id: 1 }, { id: 2 }]);
});
});
describe('deleteFile', () => {
it('calls axios.delete before fetchAllFiles', async () => {
it('calls mockDelete before fetchAllFiles', async () => {
const callOrder = [];
axios.delete.mockImplementation(async () => {
mockDelete.mockImplementation(async () => {
callOrder.push('delete');
return {};
});
axios.get.mockImplementation(async () => {
mockGet.mockImplementation(async () => {
callOrder.push('get');
return { data: [] };
});
await store.deleteFile('log', 1);
expect(axios.delete).toHaveBeenCalledWith('/api/logs/1');
expect(mockDelete).toHaveBeenCalledWith('/api/logs/1');
expect(callOrder.indexOf('delete')).toBeLessThan(
callOrder.indexOf('get'),
);
@@ -183,37 +184,37 @@ describe('filesStore', () => {
store.deleteFile('log', null),
).resolves.toBeUndefined();
expect(axios.delete).not.toHaveBeenCalled();
expect(mockDelete).not.toHaveBeenCalled();
});
});
describe('deletionRecord', () => {
it('deletes a deletion record', async () => {
axios.delete.mockResolvedValue({});
mockDelete.mockResolvedValue({});
await store.deletionRecord(5);
expect(axios.delete).toHaveBeenCalledWith('/api/deletion/5');
expect(mockDelete).toHaveBeenCalledWith('/api/deletion/5');
});
});
describe('downloadFileCSV', () => {
it('downloads CSV for a log', async () => {
axios.get.mockResolvedValue({ data: 'col1,col2\na,b' });
mockGet.mockResolvedValue({ data: 'col1,col2\na,b' });
window.URL.createObjectURL = vi.fn().mockReturnValue('blob:test');
window.URL.revokeObjectURL = vi.fn();
await store.downloadFileCSV('log', 3, 'my-file');
expect(axios.get).toHaveBeenCalledWith('/api/logs/3/csv');
expect(mockGet).toHaveBeenCalledWith('/api/logs/3/csv');
expect(window.URL.revokeObjectURL).toHaveBeenCalledWith('blob:test');
});
it('returns early for unsupported type', async () => {
await store.downloadFileCSV('log-check', 3, 'file');
expect(axios.get).not.toHaveBeenCalled();
expect(mockGet).not.toHaveBeenCalled();
});
});
});

View File

@@ -7,11 +7,16 @@ vi.mock('@/module/apiError.js', () => ({
}));
import axios from 'axios';
const { mockClientGet } = vi.hoisted(() => ({ mockClientGet: vi.fn() }));
vi.mock('@/api/client.js', () => ({
default: { get: mockClientGet },
}));
import useLoginStore from '@/stores/login.ts';
// Mock axios methods
// Mock axios methods (used for signIn/refreshToken which call plain axios)
vi.spyOn(axios, 'post').mockImplementation(vi.fn());
vi.spyOn(axios, 'get').mockImplementation(vi.fn());
describe('loginStore', () => {
let store;
@@ -118,13 +123,13 @@ describe('loginStore', () => {
describe('getUserData', () => {
it('stores user data on success', async () => {
axios.get.mockResolvedValue({
mockClientGet.mockResolvedValue({
data: { username: 'testuser', name: 'Test User' },
});
await store.getUserData();
expect(axios.get).toHaveBeenCalledWith('/api/my-account');
expect(mockClientGet).toHaveBeenCalledWith('/api/my-account');
expect(store.userData).toEqual({
username: 'testuser',
name: 'Test User',
@@ -134,7 +139,7 @@ describe('loginStore', () => {
describe('checkLogin', () => {
it('does not redirect on success', async () => {
axios.get.mockResolvedValue({ data: {} });
mockClientGet.mockResolvedValue({ data: {} });
await store.checkLogin();
@@ -142,7 +147,7 @@ describe('loginStore', () => {
});
it('redirects to login on error', async () => {
axios.get.mockRejectedValue(new Error('401'));
mockClientGet.mockRejectedValue(new Error('401'));
await store.checkLogin();
@@ -186,10 +191,6 @@ describe('loginStore', () => {
}),
);
// Should update axios default Authorization header
expect(axios.defaults.headers.common['Authorization'])
.toBe('Bearer new-access-token');
// Verify cookies were set with Secure flag
const cookieSetter = vi.spyOn(document, 'cookie', 'set');
vi.clearAllMocks();

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 usePerformanceStore from '@/stores/performance.js';
describe('performanceStore', () => {
let store;
const mockAxios = {
get: vi.fn(),
};
beforeEach(() => {
setActivePinia(createPinia());
store = usePerformanceStore();
store.$axios = mockAxios;
vi.clearAllMocks();
});
@@ -33,28 +34,28 @@ describe('performanceStore', () => {
describe('getPerformance', () => {
it('fetches log performance data', async () => {
const mockData = { time: { charts: [] }, freq: { charts: [] } };
mockAxios.get.mockResolvedValue({ data: mockData });
mockGet.mockResolvedValue({ data: mockData });
await store.getPerformance('log', 1);
expect(mockAxios.get).toHaveBeenCalledWith(
expect(mockGet).toHaveBeenCalledWith(
'/api/logs/1/performance',
);
expect(store.allPerformanceData).toEqual(mockData);
});
it('fetches filter performance data', async () => {
mockAxios.get.mockResolvedValue({ data: { time: {} } });
mockGet.mockResolvedValue({ data: { time: {} } });
await store.getPerformance('filter', 5);
expect(mockAxios.get).toHaveBeenCalledWith(
expect(mockGet).toHaveBeenCalledWith(
'/api/filters/5/performance',
);
});
it('does not throw on API failure', async () => {
mockAxios.get.mockRejectedValue(new Error('Network error'));
mockGet.mockRejectedValue(new Error('Network error'));
// Should not throw - apiError handles it
await expect(store.getPerformance('log', 1))