Add store tests with mocked axios and apiError
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
107
tests/stores/compare.test.js
Normal file
107
tests/stores/compare.test.js
Normal file
@@ -0,0 +1,107 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { setActivePinia, createPinia } from 'pinia';
|
||||
|
||||
vi.mock('@/module/apiError.js', () => ({
|
||||
default: vi.fn(),
|
||||
}));
|
||||
|
||||
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();
|
||||
});
|
||||
|
||||
it('has correct default state', () => {
|
||||
expect(store.allCompareDashboardData).toBeNull();
|
||||
});
|
||||
|
||||
it('compareDashboardData getter returns state', () => {
|
||||
store.allCompareDashboardData = { time: {} };
|
||||
expect(store.compareDashboardData).toEqual({ time: {} });
|
||||
});
|
||||
|
||||
describe('getCompare', () => {
|
||||
it('fetches compare data with encoded params', async () => {
|
||||
const params = [{ type: 'log', id: 1 }];
|
||||
const mockData = { time: {}, freq: {} };
|
||||
mockAxios.get.mockResolvedValue({ data: mockData });
|
||||
|
||||
await store.getCompare(params);
|
||||
|
||||
const encoded = encodeURIComponent(JSON.stringify(params));
|
||||
expect(mockAxios.get).toHaveBeenCalledWith(
|
||||
`/api/compare?datasets=${encoded}`,
|
||||
);
|
||||
expect(store.allCompareDashboardData).toEqual(mockData);
|
||||
});
|
||||
|
||||
it('does not throw on API failure', async () => {
|
||||
mockAxios.get.mockRejectedValue(new Error('fail'));
|
||||
|
||||
await expect(store.getCompare([]))
|
||||
.resolves.not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe('getStateData', () => {
|
||||
it('fetches log discover stats', async () => {
|
||||
mockAxios.get.mockResolvedValue({
|
||||
data: { stats: { cases: 100 } },
|
||||
});
|
||||
|
||||
const result = await store.getStateData('log', 1);
|
||||
|
||||
expect(mockAxios.get).toHaveBeenCalledWith(
|
||||
'/api/logs/1/discover',
|
||||
);
|
||||
expect(result).toEqual({ cases: 100 });
|
||||
});
|
||||
|
||||
it('fetches filter discover stats', async () => {
|
||||
mockAxios.get.mockResolvedValue({
|
||||
data: { stats: { cases: 50 } },
|
||||
});
|
||||
|
||||
const result = await store.getStateData('filter', 3);
|
||||
|
||||
expect(mockAxios.get).toHaveBeenCalledWith(
|
||||
'/api/filters/3/discover',
|
||||
);
|
||||
expect(result).toEqual({ cases: 50 });
|
||||
});
|
||||
});
|
||||
|
||||
describe('getFileName', () => {
|
||||
it('finds file name by id', async () => {
|
||||
mockAxios.get.mockResolvedValue({
|
||||
data: [
|
||||
{ id: 1, name: 'file1.csv' },
|
||||
{ id: 2, name: 'file2.csv' },
|
||||
],
|
||||
});
|
||||
|
||||
const result = await store.getFileName(1);
|
||||
|
||||
expect(result).toBe('file1.csv');
|
||||
});
|
||||
|
||||
it('returns undefined for non-existent id', async () => {
|
||||
mockAxios.get.mockResolvedValue({
|
||||
data: [{ id: 1, name: 'file1.csv' }],
|
||||
});
|
||||
|
||||
const result = await store.getFileName(99);
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user