fix: refs #8647 warnings common

This commit is contained in:
Javier Segarra 2025-02-27 20:41:32 +01:00
parent cf397a38ce
commit b08778aed7
8 changed files with 82 additions and 45 deletions

View File

@ -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: {

View File

@ -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() {

View File

@ -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);

View File

@ -21,6 +21,12 @@ describe('CardSummary', () => {
beforeEach(() => {
wrapper = createWrapper(CardSummary, {
global: {
stubs: ['RouterLink'],
mocks: {
validate: vi.fn(),
},
},
propsData: {
dataKey: 'cardSummaryKey',
url: 'cardSummaryUrl',

View File

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

View File

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

View File

@ -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(() => {

View File

@ -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);