0
0
Fork 0

feat: refs #6891 unit test

This commit is contained in:
Jorge Penadés 2024-05-13 15:41:35 +02:00
parent d55dc638d9
commit 07b91febb6
2 changed files with 52 additions and 62 deletions

View File

@ -1,62 +0,0 @@
import { vi, describe, expect, it, beforeAll, afterEach, beforeEach } from 'vitest';
import { createWrapper } from 'app/test/vitest/helper';
import VnSearchbar from 'components/ui/VnSearchbar.vue';
// Probar a importar como plugin vue-router en archivo helper
describe('VnSearchBar', () => {
let vm;
let wrapper;
let pushSpy;
beforeAll(() => {
wrapper = createWrapper(VnSearchbar, {
propsData: {
dataKey: 'CustomerList',
label: 'Search customer',
info: 'Info customer',
},
});
vm = wrapper.vm;
vm.router.currentRoute.value.matched = [
{
path: '/',
},
{
path: '/customer',
},
{
path: '/customer/:id',
},
{
path: '/customer/:id/basic-data',
},
];
pushSpy = vi.spyOn(vm.router, 'push');
vi.spyOn(vm.arrayData, 'applyFilter');
});
beforeEach(() => (vm.store.data = [{ id: 1112, name: 'Trash' }]));
afterEach(() => vi.clearAllMocks());
it('should be defined', async () => {
expect(vm.searchText).toBeDefined();
expect(vm.searchText).toEqual('');
});
it('should redirect to list page if there are several results', async () => {
vm.store.data.push({ id: 1, name: 'employee' });
await vm.search();
expect(pushSpy).toHaveBeenCalledWith({ path: '/customer/list' });
});
it('should redirect to list page if there is no results', async () => {
vm.store.data.pop();
await vm.search();
expect(pushSpy).toHaveBeenCalledWith({ path: '/customer/list' });
});
it('should redirect to basic-data page if there is only one result', async () => {
await vm.search();
expect(pushSpy).toHaveBeenCalledWith({ path: '/customer/1112/basic-data' });
});
});

View File

@ -0,0 +1,52 @@
import { vi, describe, expect, it, beforeEach, beforeAll } from 'vitest';
import useRedirect from 'src/composables/useRedirect';
import { useRouter } from 'vue-router';
vi.mock('vue-router');
describe('useRedirect', () => {
useRouter.mockReturnValue({
push: vi.fn(),
currentRoute: {
value: {
matched: [
{ path: '/' },
{ path: '/customer' },
{ path: '/customer/:id' },
{ path: '/customer/:id/basic-data' },
],
},
},
});
const data = [];
let navigate;
let spy;
beforeAll(() => {
const { navigate: navigateFn } = useRedirect();
navigate = navigateFn;
spy = useRouter().push;
});
beforeEach(() => {
data.length = 0;
spy.mockReset();
});
it('should redirect to list page if there are several results', async () => {
data.push({ id: 1, name: 'employee' }, { id: 2, name: 'boss' });
navigate(data, {});
expect(spy).toHaveBeenCalledWith({ path: '/customer/' });
});
it('should redirect to list page if there is no results', async () => {
navigate(data, {});
expect(spy).toHaveBeenCalledWith({ path: '/customer/' });
});
it('should redirect to basic-data page if there is only one result', async () => {
data.push({ id: 1, name: 'employee' });
navigate(data, {});
expect(spy).toHaveBeenCalledWith({ path: '/customer/1/basic-data' });
});
});