From ef8e77904d78772a4a2517829f71a205c0b907f3 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Tue, 9 Jan 2024 16:54:07 +0100 Subject: [PATCH] refs #6384 test: vitest --- .../components/common/VnSearchBar.spec.js | 70 +++++++++++++++++++ test/vitest/helper.js | 20 +----- 2 files changed, 71 insertions(+), 19 deletions(-) create mode 100644 test/vitest/__tests__/components/common/VnSearchBar.spec.js diff --git a/test/vitest/__tests__/components/common/VnSearchBar.spec.js b/test/vitest/__tests__/components/common/VnSearchBar.spec.js new file mode 100644 index 000000000..24111e90e --- /dev/null +++ b/test/vitest/__tests__/components/common/VnSearchBar.spec.js @@ -0,0 +1,70 @@ +import { vi, describe, expect, it, beforeAll, beforeEach, afterEach } from 'vitest'; +import { createWrapper, axios } from 'app/test/vitest/helper'; +import VnSearchbar from 'components/ui/VnSearchbar.vue'; + +vi.mock('vue-router', () => ({ + useRouter: () => ({ + push: vi.fn( ), + currentRoute: { + value: { + params: { + id: 11, + }, + }, + }, + }), + useRoute: () => ({ + matched: [ + { + path: '/', + }, + { + path: '/customer', + }, + { + path: '/customer/:id', + }, + { + path: '/customer/:id/basic-data', + }, + ], + query: {}, + params: {}, + }), +})); + +describe('VnSearchBar', () => { + let vm; + let wrapper; + + beforeAll(() => { + wrapper = createWrapper(VnSearchbar, { + propsData: { + dataKey: 'CustomerList', + label: 'Search customer', + info: 'Info customer', + }, + }); + vm = wrapper.vm; + + }); + + afterEach(() => { + vi.clearAllMocks(); + }); + it('should be defined', async () => { + expect(vm.searchText).toBeDefined(); + expect(vm.searchText).toEqual(''); + }); + it('should redirect', async () => { + vi.spyOn(vm.router,'push'); + vm.searchText = '1'; + await vm.search(); + expect(vm.router.push).toHaveBeenCalledWith('/customer/1/basic-data'); + vm.searchText = '1112'; + expect(vm.searchText).toEqual('1112'); + vi.spyOn(vm.router,'push'); + await vm.search(); + expect(vm.router.push).toHaveBeenCalledWith('/customer/1112/basic-data'); + }); +}); diff --git a/test/vitest/helper.js b/test/vitest/helper.js index 89cc640fd..2b8c70a5f 100644 --- a/test/vitest/helper.js +++ b/test/vitest/helper.js @@ -14,25 +14,6 @@ installQuasarPlugin({ }, }); const pinia = createTestingPinia({ createSpy: vi.fn, stubActions: false }); -const mockPush = vi.fn(); - -vi.mock('vue-router', () => ({ - useRouter: () => ({ - push: mockPush, - currentRoute: { - value: { - params: { - id: 1, - }, - }, - }, - }), - useRoute: () => ({ - matched: [], - query: {}, - params: {}, - }), -})); vi.mock('axios'); @@ -74,6 +55,7 @@ export function createWrapper(component, options) { mocks: { t: (tKey) => tKey, $t: (tKey) => tKey, + customRouter: false }, };