From b08778aed7f5907e6e6fd2e42871037699a28d21 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Thu, 27 Feb 2025 20:41:32 +0100 Subject: [PATCH] fix: refs #8647 warnings common --- src/components/CrudModel.vue | 4 +- src/components/VnTable/VnVisibleColumn.vue | 2 +- src/components/__tests__/CrudModel.spec.js | 72 +++++++++++-------- .../ui/__tests__/CardSummary.spec.js | 6 ++ .../ui/__tests__/VnSearchbar.spec.js | 17 +++-- src/components/ui/__tests__/VnSms.spec.js | 5 +- src/pages/Login/__tests__/Login.spec.js | 2 +- test/vitest/helper.js | 19 +++-- 8 files changed, 82 insertions(+), 45 deletions(-) diff --git a/src/components/CrudModel.vue b/src/components/CrudModel.vue index 93a2ac96a..acc9fcb3f 100644 --- a/src/components/CrudModel.vue +++ b/src/components/CrudModel.vue @@ -61,11 +61,11 @@ const $props = defineProps({ default: null, }, saveFn: { - type: Function, + type: [String, Function], default: null, }, beforeSaveFn: { - type: Function, + type: [String, Function], default: null, }, goTo: { diff --git a/src/components/VnTable/VnVisibleColumn.vue b/src/components/VnTable/VnVisibleColumn.vue index 6d15c585e..7d7c0dd29 100644 --- a/src/components/VnTable/VnVisibleColumn.vue +++ b/src/components/VnTable/VnVisibleColumn.vue @@ -58,7 +58,7 @@ async function getConfig(url, filter) { const response = await axios.get(url, { params: { filter: filter }, }); - return response.data && response.data.length > 0 ? response.data[0] : null; + return response?.data && response?.data?.length > 0 ? response.data[0] : null; } async function fetchViewConfigData() { diff --git a/src/components/__tests__/CrudModel.spec.js b/src/components/__tests__/CrudModel.spec.js index e0afd30ad..f1ffcb200 100644 --- a/src/components/__tests__/CrudModel.spec.js +++ b/src/components/__tests__/CrudModel.spec.js @@ -1,5 +1,6 @@ import { createWrapper, axios } from 'app/test/vitest/helper'; import CrudModel from 'components/CrudModel.vue'; +import keyShortcut from 'src/boot/keyShortcut'; import { vi, afterEach, beforeEach, beforeAll, describe, expect, it } from 'vitest'; describe('CrudModel', () => { @@ -15,10 +16,14 @@ describe('CrudModel', () => { 'arrayData', 'useStateStore', 'vue-i18n', + 'RouterLink', ], mocks: { validate: vi.fn(), }, + directives: { + shortcut: keyShortcut, + }, }, propsData: { dataRequired: { @@ -29,9 +34,12 @@ describe('CrudModel', () => { url: 'crudModelUrl', saveFn: '', }, + directives: { + shortcut: keyShortcut, // Registrar la directiva en los tests + }, }); - wrapper=wrapper.wrapper; - vm=wrapper.vm; + wrapper = wrapper.wrapper; + vm = wrapper.vm; }); beforeEach(() => { @@ -143,14 +151,14 @@ describe('CrudModel', () => { }); it('should return true if object is empty', async () => { - dummyObj ={}; - result = vm.isEmpty(dummyObj); + dummyObj = {}; + result = vm.isEmpty(dummyObj); expect(result).toBe(true); }); it('should return false if object is not empty', async () => { - dummyObj = {a:1, b:2, c:3}; + dummyObj = { a: 1, b: 2, c: 3 }; result = vm.isEmpty(dummyObj); expect(result).toBe(false); @@ -158,29 +166,31 @@ describe('CrudModel', () => { it('should return true if array is empty', async () => { dummyArray = []; - result = vm.isEmpty(dummyArray); + result = vm.isEmpty(dummyArray); expect(result).toBe(true); }); - + it('should return false if array is not empty', async () => { - dummyArray = [1,2,3]; + dummyArray = [1, 2, 3]; result = vm.isEmpty(dummyArray); expect(result).toBe(false); - }) + }); }); describe('resetData()', () => { it('should add $index to elements in data[] and sets originalData and formData with data', async () => { - data = [{ - name: 'Tony', - lastName: 'Stark', - age: 42, - }]; + data = [ + { + name: 'Tony', + lastName: 'Stark', + age: 42, + }, + ]; vm.resetData(data); - + expect(vm.originalData).toEqual(data); expect(vm.originalData[0].$index).toEqual(0); expect(vm.formData).toEqual(data); @@ -200,7 +210,7 @@ describe('CrudModel', () => { lastName: 'Stark', age: 42, }; - + vm.resetData(data); expect(vm.originalData).toEqual(data); @@ -210,17 +220,19 @@ describe('CrudModel', () => { }); describe('saveChanges()', () => { - data = [{ - name: 'Tony', - lastName: 'Stark', - age: 42, - }]; + data = [ + { + name: 'Tony', + lastName: 'Stark', + age: 42, + }, + ]; it('should call saveFn if exists', async () => { await wrapper.setProps({ saveFn: vi.fn() }); vm.saveChanges(data); - + expect(vm.saveFn).toHaveBeenCalledOnce(); expect(vm.isLoading).toBe(false); expect(vm.hasChanges).toBe(false); @@ -229,13 +241,15 @@ describe('CrudModel', () => { }); it("should use default url if there's not saveFn", async () => { - const postMock =vi.spyOn(axios, 'post'); - - vm.formData = [{ - name: 'Bruce', - lastName: 'Wayne', - age: 45, - }] + const postMock = vi.spyOn(axios, 'post'); + + vm.formData = [ + { + name: 'Bruce', + lastName: 'Wayne', + age: 45, + }, + ]; await vm.saveChanges(data); diff --git a/src/components/ui/__tests__/CardSummary.spec.js b/src/components/ui/__tests__/CardSummary.spec.js index 2f7f90882..567a14310 100644 --- a/src/components/ui/__tests__/CardSummary.spec.js +++ b/src/components/ui/__tests__/CardSummary.spec.js @@ -21,6 +21,12 @@ describe('CardSummary', () => { beforeEach(() => { wrapper = createWrapper(CardSummary, { + global: { + stubs: ['RouterLink'], + mocks: { + validate: vi.fn(), + }, + }, propsData: { dataKey: 'cardSummaryKey', url: 'cardSummaryUrl', diff --git a/src/components/ui/__tests__/VnSearchbar.spec.js b/src/components/ui/__tests__/VnSearchbar.spec.js index 25649194d..b1820e37d 100644 --- a/src/components/ui/__tests__/VnSearchbar.spec.js +++ b/src/components/ui/__tests__/VnSearchbar.spec.js @@ -7,10 +7,12 @@ describe('VnSearchbar', () => { let wrapper; let applyFilterSpy; const searchText = 'Bolas de madera'; - const userParams = {staticKey: 'staticValue'}; + const userParams = { staticKey: 'staticValue' }; beforeEach(async () => { wrapper = createWrapper(VnSearchbar, { + global: { stubs: ['RouterLink'] }, + propsData: { dataKey: 'testKey', filter: null, @@ -23,8 +25,9 @@ describe('VnSearchbar', () => { vm.searchText = searchText; vm.arrayData.store.userParams = userParams; - applyFilterSpy = vi.spyOn(vm.arrayData, 'applyFilter').mockImplementation(() => {}); - + applyFilterSpy = vi + .spyOn(vm.arrayData, 'applyFilter') + .mockImplementation(() => {}); }); afterEach(() => { @@ -32,7 +35,9 @@ describe('VnSearchbar', () => { }); it('search resets pagination and applies filter', async () => { - const resetPaginationSpy = vi.spyOn(vm.arrayData, 'resetPagination').mockImplementation(() => {}); + const resetPaginationSpy = vi + .spyOn(vm.arrayData, 'resetPagination') + .mockImplementation(() => {}); await vm.search(); expect(resetPaginationSpy).toHaveBeenCalled(); @@ -48,7 +53,7 @@ describe('VnSearchbar', () => { expect(applyFilterSpy).toHaveBeenCalledWith({ params: { staticKey: 'staticValue', search: searchText }, - filter: {skip: 0}, + filter: { skip: 0 }, }); }); @@ -68,4 +73,4 @@ describe('VnSearchbar', () => { }); expect(vm.to.query.searchParam).toBe(expectedQuery); }); -}); \ No newline at end of file +}); diff --git a/src/components/ui/__tests__/VnSms.spec.js b/src/components/ui/__tests__/VnSms.spec.js index e0f8c1868..b71d8ccb0 100644 --- a/src/components/ui/__tests__/VnSms.spec.js +++ b/src/components/ui/__tests__/VnSms.spec.js @@ -1,5 +1,5 @@ import { vi, describe, expect, it, beforeAll, afterEach } from 'vitest'; -import { createWrapper, axios } from 'app/test/vitest/helper'; +import { createWrapper } from 'app/test/vitest/helper'; import VnSms from 'src/components/ui/VnSms.vue'; describe('VnSms', () => { @@ -11,6 +11,9 @@ describe('VnSms', () => { stubs: ['VnPaginate'], mocks: {}, }, + propsData: { + url: 'SmsUrl', + }, }).vm; }); diff --git a/src/pages/Login/__tests__/Login.spec.js b/src/pages/Login/__tests__/Login.spec.js index e90a8ee53..acddec61f 100644 --- a/src/pages/Login/__tests__/Login.spec.js +++ b/src/pages/Login/__tests__/Login.spec.js @@ -5,7 +5,7 @@ import Login from 'pages/Login/LoginMain.vue'; describe('Login', () => { let vm; beforeAll(() => { - vm = createWrapper(Login).vm; + vm = createWrapper(Login, { global: { stubs: ['RouterLink'] } }).vm; }); afterEach(() => { diff --git a/test/vitest/helper.js b/test/vitest/helper.js index 18aacd6c4..66d0dc5a6 100644 --- a/test/vitest/helper.js +++ b/test/vitest/helper.js @@ -5,6 +5,7 @@ import { vi } from 'vitest'; import { i18n } from 'src/boot/i18n'; import { Notify, Dialog } from 'quasar'; import axios from 'axios'; +import keyShortcut from 'src/boot/keyShortcut'; import * as useValidator from 'src/composables/useValidator'; installQuasarPlugin({ @@ -17,6 +18,18 @@ const pinia = createTestingPinia({ createSpy: vi.fn, stubActions: false }); const mockPush = vi.fn(); const mockReplace = vi.fn(); +vi.mock('vue', async (importOriginal) => { + const actual = await importOriginal(); + return { + ...actual, + inject: vi.fn((key) => { + if (key === 'app') { + return {}; + } + return actual.inject(key); + }), + }; +}); vi.mock('vue-router', () => ({ useRouter: () => ({ push: mockPush, @@ -91,18 +104,14 @@ export function createWrapper(component, options) { global: { plugins: [i18n, pinia], directives: { - shortcut: () => vi.fn(), + shortcut: keyShortcut, }, }, - stubs: ['router-link'], mocks: { t: (tKey) => tKey, $t: (tKey) => tKey, $route: () => vi.fn(), }, - directives: { - shortcut: () => vi.fn(), - }, }; const mountOptions = Object.assign({}, defaultOptions);