From 40842f4ba630e09e44762490173a1a777d64b0d3 Mon Sep 17 00:00:00 2001 From: alexm Date: Wed, 19 Jun 2024 09:42:08 +0200 Subject: [PATCH] test(arrayData) add front test --- .../composables/useArrayData.spec.js | 63 ++++++++++++------- 1 file changed, 40 insertions(+), 23 deletions(-) diff --git a/test/vitest/__tests__/composables/useArrayData.spec.js b/test/vitest/__tests__/composables/useArrayData.spec.js index dc3f77927..5e4d12560 100644 --- a/test/vitest/__tests__/composables/useArrayData.spec.js +++ b/test/vitest/__tests__/composables/useArrayData.spec.js @@ -1,10 +1,13 @@ -import { describe, expect, it, beforeAll, afterEach, vi } from 'vitest'; +import { describe, expect, it, beforeEach, afterEach, vi } from 'vitest'; import { axios, flushPromises } from 'app/test/vitest/helper'; import { useArrayData } from 'composables/useArrayData'; import { useRouter } from 'vue-router'; +import * as vueRouter from 'vue-router'; describe('useArrayData', () => { - beforeAll(() => { + const filter = '{"order":"","limit":10,"skip":0}'; + const params = { supplierFk: 2 }; + beforeEach(() => { vi.spyOn(useRouter(), 'replace'); vi.spyOn(useRouter(), 'push'); }); @@ -18,8 +21,6 @@ describe('useArrayData', () => { const arrayData = useArrayData('ArrayData', { url: 'mockUrl' }); - const filter = '{"order":"","limit":10,"skip":0}'; - const params = { supplierFk: 2 }; arrayData.store.userParams = params; arrayData.fetch({}); @@ -41,8 +42,6 @@ describe('useArrayData', () => { const arrayData = useArrayData('ArrayData', { url: 'mockUrl', navigate: {} }); - const filter = '{"order":"","limit":10,"skip":0}'; - const params = { supplierFk: 2 }; arrayData.store.userParams = params; arrayData.fetch({}); @@ -57,25 +56,43 @@ describe('useArrayData', () => { expect(routerPush.query).toBeUndefined(); }); - // it('Should get data and send new URL keeping parameters, if you have more than one record', async () => { - // vi.spyOn(axios, 'get').mockReturnValueOnce({ data: [{ id: 1 }, { id: 2 }] }); + it('Should get data and send new URL keeping parameters, if you have more than one record', async () => { + vi.spyOn(axios, 'get').mockReturnValueOnce({ data: [{ id: 1 }, { id: 2 }] }); - // const arrayData = useArrayData('ArrayData', { url: 'mockUrl', navigate: {} }); + vi.spyOn(vueRouter, 'useRoute').mockReturnValue({ + matched: [], + query: {}, + params: {}, + meta: { moduleName: 'mockName' }, + path: 'mockName/1', + }); + vi.spyOn(vueRouter, 'useRouter').mockReturnValue({ + push: vi.fn(), + replace: vi.fn(), + currentRoute: { + value: { + params: { + id: 1, + }, + meta: { moduleName: 'mockName' }, + matched: [{ path: 'mockName/:id' }], + }, + }, + }); - // const filter = '{"order":"","limit":10,"skip":0}'; - // const params = { supplierFk: 2 }; - // arrayData.store.userParams = params; - // arrayData.fetch({}); + const arrayData = useArrayData('ArrayData', { url: 'mockUrl', navigate: {} }); - // await flushPromises(); - // const routerPush = useRouter().push.mock.calls[0][0]; - // console.log('routerPush: ', routerPush); + arrayData.store.userParams = params; + arrayData.fetch({}); - // expect(axios.get.mock.calls[0][1].params).toEqual({ - // filter, - // supplierFk: 2, - // }); - // expect(routerPush.path).toEqual('mockName/1'); - // expect(routerPush.query).toBeUndefined(); - // }); + await flushPromises(); + const routerPush = useRouter().push.mock.calls[0][0]; + + expect(axios.get.mock.calls[0][1].params).toEqual({ + filter, + supplierFk: 2, + }); + expect(routerPush.path).toEqual('mockName/'); + expect(routerPush.query.params).toBeDefined(); + }); });