From e2e7bacca0472d0f271d5f71a4f0bb8f1e1cbc56 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Fri, 13 Sep 2024 11:18:37 +0200 Subject: [PATCH] test: improve test --- src/composables/useArrayData.js | 2 +- .../components/common/VnSelect.spec.js | 55 +++++++++++++++++-- 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/src/composables/useArrayData.js b/src/composables/useArrayData.js index a198c157d..a7bc23520 100644 --- a/src/composables/useArrayData.js +++ b/src/composables/useArrayData.js @@ -234,7 +234,7 @@ export function useArrayData(key = useRoute().meta.moduleName, userOptions) { async function loadMore() { if (!store.hasMoreData) return; - const isCurrentFilter = JSON.parse(store.currentFilter.filter); + const isCurrentFilter = JSON.parse(store?.currentFilter?.filter ?? '{}'); const limit = isCurrentFilter ? isCurrentFilter.limit ?? store.limit diff --git a/test/vitest/__tests__/components/common/VnSelect.spec.js b/test/vitest/__tests__/components/common/VnSelect.spec.js index bce7e2e14..c92b37588 100644 --- a/test/vitest/__tests__/components/common/VnSelect.spec.js +++ b/test/vitest/__tests__/components/common/VnSelect.spec.js @@ -43,7 +43,54 @@ describe('VnSelect use options as arguments', () => { }); }); -describe('VnSelect FETCH URL', () => { +describe('VnSelect CALL FETCH URL', () => { + let vm; + + beforeAll(async () => { + vi.spyOn(axios, 'get').mockResolvedValue({ + data: [ + { id: 1, name: 'Tony Stark' }, + { id: 2, name: 'Jessica Jones' }, + { id: 3, name: 'Bruce Wayne' }, + ], + }); + vm = createWrapper(VnSelect, { + propsData: { + optionLabel: 'name', + optionValue: 'id', + url: 'Suppliers', + modelValue: '1', + sortBy: 'name DESC', + limit: '320', + fields: ['name'], + }, + global: { + stubs: ['FetchData'], + mocks: { fetch: vi.fn() }, + }, + }).vm; + }); + afterEach(() => { + vi.clearAllMocks(); + }); + + it('should CALL URL', async () => { + vm.selectValue = ''; + expect(vm.options.length).toEqual(0); + expect(vm.useURL).toBeTruthy(); + expect(vm.myOptions.length).toEqual(3); + + const canceller = new AbortController(); + expect(axios.get).toHaveBeenCalledWith('Suppliers', { + params: { + filter: '{"limit":"320","where":{"id":{"like":"%1%"}},"order":"name DESC","include":null,"fields":["name"],"skip":0}', + }, + signal: canceller.signal, + }); + }); +}); + +describe('VnSelect NOT CALL FETCH URL', () => { let vm; beforeAll(async () => { @@ -73,14 +120,14 @@ describe('VnSelect FETCH URL', () => { vi.clearAllMocks(); }); - it('should CALL URL', async () => { + it('should NOT CALL URL because no modelValue exist', async () => { vm.selectValue = ''; expect(vm.options.length).toEqual(0); expect(vm.useURL).toBeTruthy(); - expect(vm.myOptions.length).toEqual(3); + expect(vm.myOptions.length).toEqual(0); const canceller = new AbortController(); - expect(axios.get).toHaveBeenCalledWith('Suppliers', { + expect(axios.get).not.toHaveBeenCalledWith('Suppliers', { params: { filter: '{"limit":"320","where":{},"order":"name DESC","include":null,"fields":["name"],"skip":0}', },