Add store tests with mocked axios and apiError
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
83
tests/stores/performance.test.js
Normal file
83
tests/stores/performance.test.js
Normal file
@@ -0,0 +1,83 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { setActivePinia, createPinia } from 'pinia';
|
||||
|
||||
vi.mock('@/module/apiError.js', () => ({
|
||||
default: vi.fn(),
|
||||
}));
|
||||
|
||||
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();
|
||||
});
|
||||
|
||||
it('has correct default state', () => {
|
||||
expect(store.allPerformanceData).toBeNull();
|
||||
expect(store.freqChartData).toBeNull();
|
||||
});
|
||||
|
||||
it('performanceData getter returns allPerformanceData', () => {
|
||||
store.allPerformanceData = { time: {}, freq: {} };
|
||||
expect(store.performanceData).toEqual({ time: {}, freq: {} });
|
||||
});
|
||||
|
||||
describe('getPerformance', () => {
|
||||
it('fetches log performance data', async () => {
|
||||
const mockData = { time: { charts: [] }, freq: { charts: [] } };
|
||||
mockAxios.get.mockResolvedValue({ data: mockData });
|
||||
|
||||
await store.getPerformance('log', 1);
|
||||
|
||||
expect(mockAxios.get).toHaveBeenCalledWith(
|
||||
'/api/logs/1/performance',
|
||||
);
|
||||
expect(store.allPerformanceData).toEqual(mockData);
|
||||
});
|
||||
|
||||
it('fetches filter performance data', async () => {
|
||||
mockAxios.get.mockResolvedValue({ data: { time: {} } });
|
||||
|
||||
await store.getPerformance('filter', 5);
|
||||
|
||||
expect(mockAxios.get).toHaveBeenCalledWith(
|
||||
'/api/filters/5/performance',
|
||||
);
|
||||
});
|
||||
|
||||
it('does not throw on API failure', async () => {
|
||||
mockAxios.get.mockRejectedValue(new Error('Network error'));
|
||||
|
||||
// Should not throw - apiError handles it
|
||||
await expect(store.getPerformance('log', 1))
|
||||
.resolves.not.toThrow();
|
||||
expect(store.allPerformanceData).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
it('setFreqChartData sets data', () => {
|
||||
const data = { labels: [], datasets: [] };
|
||||
store.setFreqChartData(data);
|
||||
expect(store.freqChartData).toEqual(data);
|
||||
});
|
||||
|
||||
it('setFreqChartOptions sets options', () => {
|
||||
const opts = { responsive: true };
|
||||
store.setFreqChartOptions(opts);
|
||||
expect(store.freqChartOptions).toEqual(opts);
|
||||
});
|
||||
|
||||
it('setFreqChartXData sets x data', () => {
|
||||
const xData = { minX: 0, maxX: 100 };
|
||||
store.setFreqChartXData(xData);
|
||||
expect(store.freqChartXData).toEqual(xData);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user