Add store tests with mocked axios and apiError
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
179
tests/stores/conformance.test.js
Normal file
179
tests/stores/conformance.test.js
Normal file
@@ -0,0 +1,179 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { setActivePinia, createPinia } from 'pinia';
|
||||
|
||||
vi.mock('@/module/apiError.js', () => ({
|
||||
default: vi.fn(),
|
||||
}));
|
||||
|
||||
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();
|
||||
});
|
||||
|
||||
it('has correct default state', () => {
|
||||
expect(store.conformanceLogId).toBeNull();
|
||||
expect(store.conformanceFilterId).toBeNull();
|
||||
expect(store.allConformanceTask).toEqual([]);
|
||||
expect(store.selectedRuleType).toBe('Have activity');
|
||||
});
|
||||
|
||||
describe('getConformanceParams', () => {
|
||||
it('fetches log check params when no filter', async () => {
|
||||
store.conformanceLogId = 1;
|
||||
store.conformanceFilterId = null;
|
||||
const mockData = {
|
||||
tasks: [{ label: 'A' }],
|
||||
sources: ['A'],
|
||||
sinks: ['B'],
|
||||
processing_time: {},
|
||||
waiting_time: {},
|
||||
cycle_time: {},
|
||||
};
|
||||
mockAxios.get.mockResolvedValue({ data: mockData });
|
||||
|
||||
await store.getConformanceParams();
|
||||
|
||||
expect(mockAxios.get).toHaveBeenCalledWith(
|
||||
'/api/log-checks/params?log_id=1',
|
||||
);
|
||||
expect(store.allConformanceTask).toEqual([{ label: 'A' }]);
|
||||
expect(store.allCfmSeqStart).toEqual(['A']);
|
||||
});
|
||||
|
||||
it('fetches filter check params when filter set', async () => {
|
||||
store.conformanceFilterId = 5;
|
||||
const mockData = {
|
||||
tasks: [],
|
||||
sources: [],
|
||||
sinks: [],
|
||||
processing_time: {},
|
||||
waiting_time: {},
|
||||
cycle_time: {},
|
||||
};
|
||||
mockAxios.get.mockResolvedValue({ data: mockData });
|
||||
|
||||
await store.getConformanceParams();
|
||||
|
||||
expect(mockAxios.get).toHaveBeenCalledWith(
|
||||
'/api/filter-checks/params?filter_id=5',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('addConformanceCheckId', () => {
|
||||
it('posts to log temp-check and stores id', async () => {
|
||||
store.conformanceLogId = 1;
|
||||
store.conformanceFilterId = null;
|
||||
mockAxios.post.mockResolvedValue({ data: { id: 42 } });
|
||||
|
||||
await store.addConformanceCheckId({ rule: 'test' });
|
||||
|
||||
expect(mockAxios.post).toHaveBeenCalledWith(
|
||||
'/api/temp-log-checks?log_id=1',
|
||||
{ rule: 'test' },
|
||||
);
|
||||
expect(store.conformanceLogTempCheckId).toBe(42);
|
||||
});
|
||||
|
||||
it('posts to filter temp-check when filter set', async () => {
|
||||
store.conformanceFilterId = 3;
|
||||
mockAxios.post.mockResolvedValue({ data: { id: 99 } });
|
||||
|
||||
await store.addConformanceCheckId({ rule: 'test' });
|
||||
|
||||
expect(mockAxios.post).toHaveBeenCalledWith(
|
||||
'/api/temp-filter-checks?filter_id=3',
|
||||
{ rule: 'test' },
|
||||
);
|
||||
expect(store.conformanceFilterTempCheckId).toBe(99);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getConformanceReport', () => {
|
||||
it('fetches temp log check report', async () => {
|
||||
store.conformanceLogTempCheckId = 10;
|
||||
const mockData = { file: {}, charts: {} };
|
||||
mockAxios.get.mockResolvedValue({ data: mockData });
|
||||
|
||||
await store.getConformanceReport();
|
||||
|
||||
expect(mockAxios.get).toHaveBeenCalledWith(
|
||||
'/api/temp-log-checks/10',
|
||||
);
|
||||
expect(store.allConformanceTempReportData).toEqual(mockData);
|
||||
});
|
||||
|
||||
it('stores routeFile when getRouteFile=true', async () => {
|
||||
store.conformanceLogTempCheckId = 10;
|
||||
const mockData = { file: { name: 'test.csv' } };
|
||||
mockAxios.get.mockResolvedValue({ data: mockData });
|
||||
|
||||
await store.getConformanceReport(true);
|
||||
|
||||
expect(store.allRouteFile).toEqual({ name: 'test.csv' });
|
||||
expect(store.allConformanceTempReportData).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('addConformanceCreateCheckId', () => {
|
||||
it('creates log check and clears temp id', async () => {
|
||||
store.conformanceLogId = 1;
|
||||
store.conformanceFilterId = null;
|
||||
store.conformanceLogTempCheckId = 10;
|
||||
store.conformanceRuleData = { type: 'test' };
|
||||
mockAxios.post.mockResolvedValue({ data: { id: 100 } });
|
||||
|
||||
await store.addConformanceCreateCheckId('myRule');
|
||||
|
||||
expect(mockAxios.post).toHaveBeenCalledWith(
|
||||
'/api/log-checks?log_id=1',
|
||||
{ name: 'myRule', rule: { type: 'test' } },
|
||||
);
|
||||
expect(store.conformanceLogCreateCheckId).toBe(100);
|
||||
expect(store.conformanceLogTempCheckId).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('updataConformance', () => {
|
||||
it('updates existing log check', async () => {
|
||||
store.conformanceLogCreateCheckId = 50;
|
||||
store.conformanceRuleData = { type: 'updated' };
|
||||
mockAxios.put.mockResolvedValue({ status: 200 });
|
||||
|
||||
await store.updataConformance();
|
||||
|
||||
expect(mockAxios.put).toHaveBeenCalledWith(
|
||||
'/api/log-checks/50',
|
||||
{ type: 'updated' },
|
||||
);
|
||||
expect(store.isUpdataConformance).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
it('setConformanceLogCreateCheckId sets value', () => {
|
||||
store.setConformanceLogCreateCheckId('abc');
|
||||
expect(store.conformanceLogCreateCheckId).toBe('abc');
|
||||
});
|
||||
|
||||
describe('getters', () => {
|
||||
it('conformanceTask returns labels', () => {
|
||||
store.allConformanceTask = [
|
||||
{ label: 'A' }, { label: 'B' },
|
||||
];
|
||||
expect(store.conformanceTask).toEqual(['A', 'B']);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user