salix-front/test/vitest/__tests__/components/common/VnSearchBar.spec.js

63 lines
1.9 KiB
JavaScript
Raw Normal View History

import { vi, describe, expect, it, beforeAll, afterEach, beforeEach } from 'vitest';
import { createWrapper } from 'app/test/vitest/helper';
2024-01-09 15:54:07 +00:00
import VnSearchbar from 'components/ui/VnSearchbar.vue';
// Probar a importar como plugin vue-router en archivo helper
2024-01-09 15:54:07 +00:00
describe('VnSearchBar', () => {
let vm;
let wrapper;
let pushSpy;
2024-01-09 15:54:07 +00:00
beforeAll(() => {
wrapper = createWrapper(VnSearchbar, {
propsData: {
dataKey: 'CustomerList',
label: 'Search customer',
info: 'Info customer',
},
});
vm = wrapper.vm;
vm.router.currentRoute.value.matched = [
2024-01-09 16:01:11 +00:00
{
path: '/',
},
{
path: '/customer',
},
{
path: '/customer/:id',
},
{
path: '/customer/:id/basic-data',
},
];
2024-01-09 15:54:07 +00:00
pushSpy = vi.spyOn(vm.router, 'push');
vi.spyOn(vm.arrayData, 'applyFilter');
2024-01-09 15:54:07 +00:00
});
beforeEach(() => (vm.store.data = [{ id: 1112, name: 'Trash' }]));
afterEach(() => vi.clearAllMocks());
2024-01-09 15:54:07 +00:00
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();
2024-01-09 15:54:07 +00:00
await vm.search();
expect(pushSpy).toHaveBeenCalledWith({ path: '/customer/list' });
});
it('should redirect to basic-data page if there is only one result', async () => {
2024-01-09 15:54:07 +00:00
await vm.search();
expect(pushSpy).toHaveBeenCalledWith({ path: '/customer/1112/basic-data' });
});
2024-01-09 15:54:07 +00:00
});