test: add unit tests for useArrayDataStore and mock axios in axios.spec.js

This commit is contained in:
Javier Segarra 2025-04-03 01:24:00 +02:00
parent d406715a70
commit d78460a438
2 changed files with 120 additions and 1 deletions

View File

@ -9,6 +9,30 @@ vi.mock('src/composables/useSession', () => ({
}),
}));
// Mock axios
vi.mock('axios', () => ({
default: {
create: vi.fn(() => ({
interceptors: {
request: { use: vi.fn() },
response: { use: vi.fn() },
},
})),
interceptors: {
request: { use: vi.fn() },
response: { use: vi.fn() },
},
defaults: {
baseURL: '',
},
},
}));
vi.mock('src/router', () => ({
Router: {
push: vi.fn(),
},
}));
vi.mock('src/stores/useStateQueryStore', () => ({
useStateQueryStore: () => ({
add: () => vi.fn(),
@ -29,7 +53,7 @@ describe('Axios boot', () => {
'Accept-Language': 'en-US',
Authorization: 'DEFAULT_TOKEN',
},
})
}),
);
});
});

View File

@ -0,0 +1,95 @@
import { describe, expect, it, beforeEach } from 'vitest';
import { setActivePinia, createPinia } from 'pinia';
import { useArrayDataStore } from '../useArrayDataStore';
describe('useArrayDataStore', () => {
beforeEach(() => {
setActivePinia(createPinia());
});
it('should get undefined for non-existent key', () => {
const store = useArrayDataStore();
expect(store.get('nonExistent')).toBeUndefined();
});
it('should set default state for new key', () => {
const store = useArrayDataStore();
store.set('test');
const state = store.get('test');
expect(state).toMatchObject({
filter: {},
userFilter: {},
userParams: {},
url: '',
limit: 20,
skip: 0,
order: '',
isLoading: false,
userParamsChanged: false,
exprBuilder: null,
searchUrl: 'params',
navigate: null,
page: 1,
mapKey: 'id',
oneRecord: false,
});
});
it('should clear state for specific key', () => {
const store = useArrayDataStore();
store.set('test');
store.clear('test');
expect(store.get('test')).toBeUndefined();
});
it('should reset all properties when no options provided', () => {
const store = useArrayDataStore();
store.set('test');
const state = store.get('test');
state.limit = 50;
state.page = 3;
store.reset('test');
expect(store.get('test').limit).toBe(20);
expect(store.get('test').page).toBe(1);
});
it('should reset only specified properties', () => {
const store = useArrayDataStore();
store.set('test');
const state = store.get('test');
state.limit = 50;
state.page = 3;
state.url = 'test-url';
store.reset('test', ['limit', 'page']);
expect(state.limit).toBe(20);
expect(state.page).toBe(1);
expect(state.url).toBe('test-url');
});
it('should reset nested properties', () => {
const store = useArrayDataStore();
store.set('test');
const state = store.get('test');
state.filter.skip = 10;
store.reset('test', ['filter.skip']);
expect(state.filter.skip).toBe(0);
});
it('should reset pagination properties', () => {
const store = useArrayDataStore();
store.set('test');
const state = store.get('test');
state.skip = 20;
state.filter.skip = 20;
state.page = 3;
store.resetPagination('test');
expect(state.skip).toBe(0);
expect(state.filter.skip).toBe(0);
expect(state.page).toBe(1);
});
});