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' });
    });
});
