2024-03-27 10:14:54 +00:00
|
|
|
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';
|
2024-03-27 10:14:54 +00:00
|
|
|
// Probar a importar como plugin vue-router en archivo helper
|
2024-01-09 15:54:07 +00:00
|
|
|
describe('VnSearchBar', () => {
|
|
|
|
let vm;
|
|
|
|
let wrapper;
|
2024-03-27 10:14:54 +00:00
|
|
|
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;
|
2024-03-27 10:14:54 +00:00
|
|
|
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
|
|
|
|
2024-03-27 10:14:54 +00:00
|
|
|
pushSpy = vi.spyOn(vm.router, 'push');
|
|
|
|
vi.spyOn(vm.arrayData, 'applyFilter');
|
2024-01-09 15:54:07 +00:00
|
|
|
});
|
2024-03-27 10:14:54 +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('');
|
|
|
|
});
|
2024-03-27 10:14:54 +00:00
|
|
|
|
|
|
|
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();
|
2024-03-27 10:14:54 +00:00
|
|
|
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();
|
2024-03-27 10:14:54 +00:00
|
|
|
expect(pushSpy).toHaveBeenCalledWith({ path: '/customer/1112/basic-data' });
|
|
|
|
});
|
2024-01-09 15:54:07 +00:00
|
|
|
});
|