test: improve test

This commit is contained in:
Javier Segarra 2024-09-13 11:18:37 +02:00
parent ffed7125d0
commit e2e7bacca0
2 changed files with 52 additions and 5 deletions

View File

@ -234,7 +234,7 @@ export function useArrayData(key = useRoute().meta.moduleName, userOptions) {
async function loadMore() {
if (!store.hasMoreData) return;
const isCurrentFilter = JSON.parse(store.currentFilter.filter);
const isCurrentFilter = JSON.parse(store?.currentFilter?.filter ?? '{}');
const limit = isCurrentFilter
? isCurrentFilter.limit ?? store.limit

View File

@ -43,7 +43,54 @@ describe('VnSelect use options as arguments', () => {
});
});
describe('VnSelect FETCH URL', () => {
describe('VnSelect CALL FETCH URL', () => {
let vm;
beforeAll(async () => {
vi.spyOn(axios, 'get').mockResolvedValue({
data: [
{ id: 1, name: 'Tony Stark' },
{ id: 2, name: 'Jessica Jones' },
{ id: 3, name: 'Bruce Wayne' },
],
});
vm = createWrapper(VnSelect, {
propsData: {
optionLabel: 'name',
optionValue: 'id',
url: 'Suppliers',
modelValue: '1',
sortBy: 'name DESC',
limit: '320',
fields: ['name'],
},
global: {
stubs: ['FetchData'],
mocks: { fetch: vi.fn() },
},
}).vm;
});
afterEach(() => {
vi.clearAllMocks();
});
it('should CALL URL', async () => {
vm.selectValue = '';
expect(vm.options.length).toEqual(0);
expect(vm.useURL).toBeTruthy();
expect(vm.myOptions.length).toEqual(3);
const canceller = new AbortController();
expect(axios.get).toHaveBeenCalledWith('Suppliers', {
params: {
filter: '{"limit":"320","where":{"id":{"like":"%1%"}},"order":"name DESC","include":null,"fields":["name"],"skip":0}',
},
signal: canceller.signal,
});
});
});
describe('VnSelect NOT CALL FETCH URL', () => {
let vm;
beforeAll(async () => {
@ -73,14 +120,14 @@ describe('VnSelect FETCH URL', () => {
vi.clearAllMocks();
});
it('should CALL URL', async () => {
it('should NOT CALL URL because no modelValue exist', async () => {
vm.selectValue = '';
expect(vm.options.length).toEqual(0);
expect(vm.useURL).toBeTruthy();
expect(vm.myOptions.length).toEqual(3);
expect(vm.myOptions.length).toEqual(0);
const canceller = new AbortController();
expect(axios.get).toHaveBeenCalledWith('Suppliers', {
expect(axios.get).not.toHaveBeenCalledWith('Suppliers', {
params: {
filter: '{"limit":"320","where":{},"order":"name DESC","include":null,"fields":["name"],"skip":0}',
},