salix-front/test/vitest/__tests__/composables/useArrayData.spec.js

99 lines
3.2 KiB
JavaScript
Raw Normal View History

2024-06-19 07:42:08 +00:00
import { describe, expect, it, beforeEach, afterEach, vi } from 'vitest';
2024-06-18 13:20:07 +00:00
import { axios, flushPromises } from 'app/test/vitest/helper';
2024-04-08 14:38:36 +00:00
import { useArrayData } from 'composables/useArrayData';
2024-06-18 13:20:07 +00:00
import { useRouter } from 'vue-router';
2024-06-19 07:42:08 +00:00
import * as vueRouter from 'vue-router';
2024-04-08 14:38:36 +00:00
describe('useArrayData', () => {
2024-07-11 08:22:02 +00:00
const filter = '{"limit":10,"skip":0}';
2024-06-19 07:42:08 +00:00
const params = { supplierFk: 2 };
beforeEach(() => {
2024-06-18 13:20:07 +00:00
vi.spyOn(useRouter(), 'replace');
vi.spyOn(useRouter(), 'push');
});
afterEach(() => {
vi.clearAllMocks();
});
it('should fetch and repalce url with new params', async () => {
vi.spyOn(axios, 'get').mockReturnValueOnce({ data: [] });
2024-04-09 13:08:54 +00:00
2024-06-18 13:20:07 +00:00
const arrayData = useArrayData('ArrayData', { url: 'mockUrl' });
2024-04-09 13:08:54 +00:00
2024-06-18 13:20:07 +00:00
arrayData.store.userParams = params;
arrayData.fetch({});
await flushPromises();
const routerReplace = useRouter().replace.mock.calls[0][0];
expect(axios.get.mock.calls[0][1].params).toEqual({
filter,
supplierFk: 2,
});
expect(routerReplace.path).toEqual('mockSection/list');
expect(JSON.parse(routerReplace.query.params)).toEqual(
expect.objectContaining(params)
);
2024-04-08 14:38:36 +00:00
});
2024-06-18 13:20:07 +00:00
it('Should get data and send new URL without keeping parameters, if there is only one record', async () => {
vi.spyOn(axios, 'get').mockReturnValueOnce({ data: [{ id: 1 }] });
const arrayData = useArrayData('ArrayData', { url: 'mockUrl', navigate: {} });
arrayData.store.userParams = params;
arrayData.fetch({});
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/1');
expect(routerPush.query).toBeUndefined();
2024-04-08 14:38:36 +00:00
});
2024-06-18 13:20:07 +00:00
2024-06-19 07:42:08 +00:00
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 }] });
2024-06-18 13:20:07 +00:00
2024-06-19 07:42:08 +00:00
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' }],
},
},
});
2024-06-18 13:20:07 +00:00
2024-06-19 07:42:08 +00:00
const arrayData = useArrayData('ArrayData', { url: 'mockUrl', navigate: {} });
2024-06-18 13:20:07 +00:00
2024-06-19 07:42:08 +00:00
arrayData.store.userParams = params;
arrayData.fetch({});
2024-06-18 13:20:07 +00:00
2024-06-19 07:42:08 +00:00
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();
});
2024-04-08 14:38:36 +00:00
});