test: front test for arrayData
gitea/salix-front/pipeline/pr-dev This commit looks good
Details
gitea/salix-front/pipeline/pr-dev This commit looks good
Details
This commit is contained in:
parent
8085e1e518
commit
92cac607e4
|
@ -1,31 +1,81 @@
|
||||||
import { describe, expect, it, beforeAll } from 'vitest';
|
import { describe, expect, it, beforeAll, afterEach, vi } from 'vitest';
|
||||||
import { axios } 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';
|
||||||
|
|
||||||
describe('useArrayData', () => {
|
describe('useArrayData', () => {
|
||||||
let arrayData;
|
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
axios.get.mockResolvedValue({ data: [] });
|
vi.spyOn(useRouter(), 'replace');
|
||||||
arrayData = useArrayData('InvoiceIn', { url: 'invoice-in/list' });
|
vi.spyOn(useRouter(), 'push');
|
||||||
Object.defineProperty(window.location, 'href', {
|
});
|
||||||
writable: true,
|
|
||||||
value: 'localhost:9000/invoice-in/list',
|
afterEach(() => {
|
||||||
|
vi.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should fetch and repalce url with new params', async () => {
|
||||||
|
vi.spyOn(axios, 'get').mockReturnValueOnce({ data: [] });
|
||||||
|
|
||||||
|
const arrayData = useArrayData('ArrayData', { url: 'mockUrl' });
|
||||||
|
|
||||||
|
const filter = '{"order":"","limit":10,"skip":0}';
|
||||||
|
const params = { supplierFk: 2 };
|
||||||
|
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');
|
||||||
// Mock the window.history.pushState method within useArrayData
|
expect(JSON.parse(routerReplace.query.params)).toEqual(
|
||||||
window.history.pushState = (data, title, url) => (window.location.href = url);
|
expect.objectContaining(params)
|
||||||
|
);
|
||||||
// Mock the URL constructor within useArrayData
|
|
||||||
global.URL = class URL {
|
|
||||||
constructor(url) {
|
|
||||||
this.hash = url.split('localhost:9000/')[1];
|
|
||||||
}
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should add the params to the url', async () => {
|
it('Should get data and send new URL without keeping parameters, if there is only one record', async () => {
|
||||||
arrayData.store.userParams = { supplierFk: 2 };
|
vi.spyOn(axios, 'get').mockReturnValueOnce({ data: [{ id: 1 }] });
|
||||||
arrayData.updateStateParams();
|
|
||||||
expect(window.location.href).contain('params=%7B%22supplierFk%22%3A2%7D');
|
const arrayData = useArrayData('ArrayData', { url: 'mockUrl', navigate: {} });
|
||||||
|
|
||||||
|
const filter = '{"order":"","limit":10,"skip":0}';
|
||||||
|
const params = { supplierFk: 2 };
|
||||||
|
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();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 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: {} });
|
||||||
|
|
||||||
|
// const filter = '{"order":"","limit":10,"skip":0}';
|
||||||
|
// const params = { supplierFk: 2 };
|
||||||
|
// arrayData.store.userParams = params;
|
||||||
|
// arrayData.fetch({});
|
||||||
|
|
||||||
|
// await flushPromises();
|
||||||
|
// const routerPush = useRouter().push.mock.calls[0][0];
|
||||||
|
// console.log('routerPush: ', routerPush);
|
||||||
|
|
||||||
|
// expect(axios.get.mock.calls[0][1].params).toEqual({
|
||||||
|
// filter,
|
||||||
|
// supplierFk: 2,
|
||||||
|
// });
|
||||||
|
// expect(routerPush.path).toEqual('mockName/1');
|
||||||
|
// expect(routerPush.query).toBeUndefined();
|
||||||
|
// });
|
||||||
});
|
});
|
||||||
|
|
|
@ -15,16 +15,19 @@ installQuasarPlugin({
|
||||||
});
|
});
|
||||||
const pinia = createTestingPinia({ createSpy: vi.fn, stubActions: false });
|
const pinia = createTestingPinia({ createSpy: vi.fn, stubActions: false });
|
||||||
const mockPush = vi.fn();
|
const mockPush = vi.fn();
|
||||||
|
const mockReplace = vi.fn();
|
||||||
|
|
||||||
vi.mock('vue-router', () => ({
|
vi.mock('vue-router', () => ({
|
||||||
useRouter: () => ({
|
useRouter: () => ({
|
||||||
push: mockPush,
|
push: mockPush,
|
||||||
|
replace: mockReplace,
|
||||||
currentRoute: {
|
currentRoute: {
|
||||||
value: {
|
value: {
|
||||||
params: {
|
params: {
|
||||||
id: 1,
|
id: 1,
|
||||||
},
|
},
|
||||||
meta: { moduleName: 'mockName' },
|
meta: { moduleName: 'mockName' },
|
||||||
|
matched: [{ path: 'mockName/list' }],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
@ -33,6 +36,7 @@ vi.mock('vue-router', () => ({
|
||||||
query: {},
|
query: {},
|
||||||
params: {},
|
params: {},
|
||||||
meta: { moduleName: 'mockName' },
|
meta: { moduleName: 'mockName' },
|
||||||
|
path: 'mockSection/list',
|
||||||
}),
|
}),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue