From d78460a438c99e878523ec26b444ab950d5c25eb Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Thu, 3 Apr 2025 01:24:00 +0200 Subject: [PATCH] test: add unit tests for useArrayDataStore and mock axios in axios.spec.js --- src/boot/__tests__/axios.spec.js | 26 ++++- .../__tests__/useArrayDataStore.spec.js | 95 +++++++++++++++++++ 2 files changed, 120 insertions(+), 1 deletion(-) create mode 100644 src/stores/__tests__/useArrayDataStore.spec.js diff --git a/src/boot/__tests__/axios.spec.js b/src/boot/__tests__/axios.spec.js index 7dffaefc1..85d578517 100644 --- a/src/boot/__tests__/axios.spec.js +++ b/src/boot/__tests__/axios.spec.js @@ -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', }, - }) + }), ); }); }); diff --git a/src/stores/__tests__/useArrayDataStore.spec.js b/src/stores/__tests__/useArrayDataStore.spec.js new file mode 100644 index 000000000..79f17cf69 --- /dev/null +++ b/src/stores/__tests__/useArrayDataStore.spec.js @@ -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); + }); +});