0
0
Fork 0

test(arrayData) add front test

This commit is contained in:
Alex Moreno 2024-06-19 09:42:08 +02:00
parent 92cac607e4
commit 40842f4ba6
1 changed files with 40 additions and 23 deletions

View File

@ -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 { axios, flushPromises } from 'app/test/vitest/helper';
import { useArrayData } from 'composables/useArrayData'; import { useArrayData } from 'composables/useArrayData';
import { useRouter } from 'vue-router'; import { useRouter } from 'vue-router';
import * as vueRouter from 'vue-router';
describe('useArrayData', () => { describe('useArrayData', () => {
beforeAll(() => { const filter = '{"order":"","limit":10,"skip":0}';
const params = { supplierFk: 2 };
beforeEach(() => {
vi.spyOn(useRouter(), 'replace'); vi.spyOn(useRouter(), 'replace');
vi.spyOn(useRouter(), 'push'); vi.spyOn(useRouter(), 'push');
}); });
@ -18,8 +21,6 @@ describe('useArrayData', () => {
const arrayData = useArrayData('ArrayData', { url: 'mockUrl' }); const arrayData = useArrayData('ArrayData', { url: 'mockUrl' });
const filter = '{"order":"","limit":10,"skip":0}';
const params = { supplierFk: 2 };
arrayData.store.userParams = params; arrayData.store.userParams = params;
arrayData.fetch({}); arrayData.fetch({});
@ -41,8 +42,6 @@ describe('useArrayData', () => {
const arrayData = useArrayData('ArrayData', { url: 'mockUrl', navigate: {} }); const arrayData = useArrayData('ArrayData', { url: 'mockUrl', navigate: {} });
const filter = '{"order":"","limit":10,"skip":0}';
const params = { supplierFk: 2 };
arrayData.store.userParams = params; arrayData.store.userParams = params;
arrayData.fetch({}); arrayData.fetch({});
@ -57,25 +56,43 @@ describe('useArrayData', () => {
expect(routerPush.query).toBeUndefined(); expect(routerPush.query).toBeUndefined();
}); });
// it('Should get data and send new URL keeping parameters, if you have more than one record', async () => { 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 }] }); 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 arrayData = useArrayData('ArrayData', { url: 'mockUrl', navigate: {} });
// const params = { supplierFk: 2 };
// arrayData.store.userParams = params;
// arrayData.fetch({});
// await flushPromises(); arrayData.store.userParams = params;
// const routerPush = useRouter().push.mock.calls[0][0]; arrayData.fetch({});
// console.log('routerPush: ', routerPush);
// expect(axios.get.mock.calls[0][1].params).toEqual({ await flushPromises();
// filter, const routerPush = useRouter().push.mock.calls[0][0];
// supplierFk: 2,
// }); expect(axios.get.mock.calls[0][1].params).toEqual({
// expect(routerPush.path).toEqual('mockName/1'); filter,
// expect(routerPush.query).toBeUndefined(); supplierFk: 2,
// }); });
expect(routerPush.path).toEqual('mockName/');
expect(routerPush.query.params).toBeDefined();
});
}); });