diff --git a/test/vitest/__tests__/components/common/VnSearchBar.spec.js b/test/vitest/__tests__/components/common/VnSearchBar.spec.js deleted file mode 100644 index 0938ae9c0..000000000 --- a/test/vitest/__tests__/components/common/VnSearchBar.spec.js +++ /dev/null @@ -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' }); - }); -}); diff --git a/test/vitest/__tests__/composables/useRedirect.spec.js b/test/vitest/__tests__/composables/useRedirect.spec.js new file mode 100644 index 000000000..ce56189b9 --- /dev/null +++ b/test/vitest/__tests__/composables/useRedirect.spec.js @@ -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' }); + }); +});