test: arrayData
gitea/salix-front/pipeline/pr-dev This commit is unstable
Details
gitea/salix-front/pipeline/pr-dev This commit is unstable
Details
This commit is contained in:
parent
d78460a438
commit
c08b3648f2
|
@ -1,16 +1,39 @@
|
||||||
import { describe, expect, it, beforeEach, afterEach, vi } from 'vitest';
|
import { describe, expect, it, beforeEach, afterEach, vi } from 'vitest';
|
||||||
import axios from 'axios';
|
import { default as axios } from 'axios';
|
||||||
import { 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';
|
import * as vueRouter from 'vue-router';
|
||||||
|
import { setActivePinia, createPinia } from 'pinia';
|
||||||
|
|
||||||
describe('useArrayData', () => {
|
describe('useArrayData', () => {
|
||||||
const filter = '{"limit":20,"skip":0}';
|
const filter = '{"limit":20,"skip":0}';
|
||||||
const params = { supplierFk: 2 };
|
const params = { supplierFk: 2 };
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.spyOn(useRouter(), 'replace');
|
setActivePinia(createPinia());
|
||||||
vi.spyOn(useRouter(), 'push');
|
|
||||||
|
// Mock route
|
||||||
|
vi.spyOn(vueRouter, 'useRoute').mockReturnValue({
|
||||||
|
path: 'mockSection/list',
|
||||||
|
matched: [],
|
||||||
|
query: {},
|
||||||
|
params: {},
|
||||||
|
meta: { moduleName: 'mockName' },
|
||||||
|
});
|
||||||
|
|
||||||
|
// Mock router
|
||||||
|
vi.spyOn(vueRouter, 'useRouter').mockReturnValue({
|
||||||
|
push: vi.fn(),
|
||||||
|
replace: vi.fn(),
|
||||||
|
currentRoute: {
|
||||||
|
value: {
|
||||||
|
path: 'mockSection/list',
|
||||||
|
params: { id: 1 },
|
||||||
|
meta: { moduleName: 'mockName' },
|
||||||
|
matched: [{ path: 'mockName/:id' }],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
|
@ -18,103 +41,69 @@ describe('useArrayData', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should fetch and replace url with new params', async () => {
|
it('should fetch and replace url with new params', async () => {
|
||||||
vi.spyOn(axios, 'get').mockReturnValueOnce({ data: [] });
|
vi.spyOn(axios, 'get').mockResolvedValueOnce({ data: [] });
|
||||||
|
|
||||||
const arrayData = useArrayData('ArrayData', { url: 'mockUrl' });
|
const arrayData = useArrayData('ArrayData', {
|
||||||
|
url: 'mockUrl',
|
||||||
|
searchUrl: 'params',
|
||||||
|
});
|
||||||
|
|
||||||
arrayData.store.userParams = params;
|
arrayData.store.userParams = params;
|
||||||
arrayData.fetch({});
|
await arrayData.fetch({});
|
||||||
|
|
||||||
await flushPromises();
|
const routerReplace = useRouter().replace.mock.calls[0][0];
|
||||||
let routerReplace = useRouter().replace;
|
|
||||||
routerReplace = routerReplace.mock.calls[0][0];
|
expect(axios.get).toHaveBeenCalledWith('mockUrl', {
|
||||||
expect(axios.get.mock.calls[0][1].params).toEqual({
|
signal: expect.any(Object),
|
||||||
filter,
|
params: {
|
||||||
supplierFk: 2,
|
filter,
|
||||||
|
supplierFk: 2,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
expect(routerReplace.path).toEqual('mockSection/list');
|
|
||||||
|
expect(routerReplace.path).toBe('mockSection/list');
|
||||||
expect(JSON.parse(routerReplace.query.params)).toEqual(
|
expect(JSON.parse(routerReplace.query.params)).toEqual(
|
||||||
expect.objectContaining(params),
|
expect.objectContaining(params),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should get data and send new URL without keeping parameters, if there is only one record', async () => {
|
it('should redirect to detail when single record is returned with navigation', async () => {
|
||||||
vi.spyOn(axios, 'get').mockReturnValueOnce({ data: [{ id: 1 }] });
|
vi.spyOn(axios, 'get').mockResolvedValueOnce({
|
||||||
|
data: [{ id: 1 }],
|
||||||
|
});
|
||||||
|
|
||||||
const arrayData = useArrayData('ArrayData', { url: 'mockUrl', navigate: {} });
|
const arrayData = useArrayData('ArrayData', {
|
||||||
|
url: 'mockUrl',
|
||||||
|
navigate: {},
|
||||||
|
});
|
||||||
|
|
||||||
arrayData.store.userParams = params;
|
arrayData.store.userParams = params;
|
||||||
arrayData.fetch({});
|
await arrayData.fetch({});
|
||||||
|
|
||||||
await flushPromises();
|
|
||||||
const routerPush = useRouter().push.mock.calls[0][0];
|
const routerPush = useRouter().push.mock.calls[0][0];
|
||||||
|
|
||||||
expect(axios.get.mock.calls[0][1].params).toEqual({
|
expect(routerPush.path).toBe('mockName/1');
|
||||||
filter,
|
|
||||||
supplierFk: 2,
|
|
||||||
});
|
|
||||||
expect(routerPush.path).toEqual('mockName/1');
|
|
||||||
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 return one record when oneRecord is true', async () => {
|
||||||
vi.spyOn(axios, 'get').mockReturnValueOnce({ data: [{ id: 1 }, { id: 2 }] });
|
vi.spyOn(axios, 'get').mockResolvedValueOnce({
|
||||||
|
|
||||||
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 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/');
|
|
||||||
expect(routerPush.query.params).toBeDefined();
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should return one record', async () => {
|
|
||||||
vi.spyOn(axios, 'get').mockReturnValueOnce({
|
|
||||||
data: [
|
data: [
|
||||||
{ id: 1, name: 'Entity 1' },
|
{ id: 1, name: 'Entity 1' },
|
||||||
{ id: 2, name: 'Entity 2' },
|
{ id: 2, name: 'Entity 2' },
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
const arrayData = useArrayData('ArrayData', { url: 'mockUrl', oneRecord: true });
|
|
||||||
|
const arrayData = useArrayData('ArrayData', {
|
||||||
|
url: 'mockUrl',
|
||||||
|
oneRecord: true,
|
||||||
|
});
|
||||||
|
|
||||||
await arrayData.fetch({});
|
await arrayData.fetch({});
|
||||||
|
|
||||||
expect(arrayData.store.data).toEqual({ id: 1, name: 'Entity 1' });
|
expect(arrayData.store.data).toEqual({
|
||||||
});
|
id: 1,
|
||||||
|
name: 'Entity 1',
|
||||||
it('should handle empty data gracefully if has to return one record', async () => {
|
});
|
||||||
vi.spyOn(axios, 'get').mockReturnValueOnce({ data: [] });
|
|
||||||
const arrayData = useArrayData('ArrayData', { url: 'mockUrl', oneRecord: true });
|
|
||||||
await arrayData.fetch({});
|
|
||||||
|
|
||||||
expect(arrayData.store.data).toBeUndefined();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -5,12 +5,11 @@ import { useArrayDataStore } from 'stores/useArrayDataStore';
|
||||||
import { buildFilter } from 'filters/filterPanel';
|
import { buildFilter } from 'filters/filterPanel';
|
||||||
import { isDialogOpened } from 'src/filters';
|
import { isDialogOpened } from 'src/filters';
|
||||||
|
|
||||||
const arrayDataStore = useArrayDataStore();
|
|
||||||
|
|
||||||
export function useArrayData(key, userOptions) {
|
export function useArrayData(key, userOptions) {
|
||||||
key ??= useRoute().meta.moduleName;
|
key ??= useRoute().meta.moduleName;
|
||||||
|
|
||||||
if (!key) throw new Error('ArrayData: A key is required to use this composable');
|
if (!key) throw new Error('ArrayData: A key is required to use this composable');
|
||||||
|
const arrayDataStore = useArrayDataStore(); // Move inside function
|
||||||
|
|
||||||
if (!arrayDataStore.get(key)) arrayDataStore.set(key);
|
if (!arrayDataStore.get(key)) arrayDataStore.set(key);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue