#7050 testCrudModel #1077
|
@ -1,9 +1,10 @@
|
|||
import { createWrapper } from 'app/test/vitest/helper';
|
||||
import { createWrapper, axios } from 'app/test/vitest/helper';
|
||||
import CrudModel from 'components/CrudModel.vue';
|
||||
import { vi, afterEach, beforeEach, beforeAll, describe, expect, it } from 'vitest';
|
||||
|
||||
describe('CrudModel', () => {
|
||||
let vm;
|
||||
let data;
|
||||
beforeAll(() => {
|
||||
vm = createWrapper(CrudModel, {
|
||||
global: {
|
||||
|
@ -31,6 +32,7 @@ describe('CrudModel', () => {
|
|||
|
||||
beforeEach(() => {
|
||||
vm.fetch([]);
|
||||
vm.watchChanges = null;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
|
@ -160,4 +162,101 @@ describe('CrudModel', () => {
|
|||
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,
|
||||
}];
|
||||
|
||||
vm.resetData(data);
|
||||
|
||||
expect(vm.originalData).toEqual(data);
|
||||
expect(vm.originalData[0].$index).toEqual(0);
|
||||
expect(vm.formData).toEqual(data);
|
||||
expect(vm.formData[0].$index).toEqual(0);
|
||||
expect(vm.watchChanges).not.toBeNull();
|
||||
});
|
||||
|
||||
it('should dont do nothing if data is null', async () => {
|
||||
vm.resetData(null);
|
||||
|
||||
expect(vm.watchChanges).toBeNull();
|
||||
});
|
||||
|
||||
it('should set originalData and formatData with data and generate watchChanges', async () => {
|
||||
jtubau marked this conversation as resolved
Outdated
|
||||
data = {
|
||||
name: 'Tony',
|
||||
lastName: 'Stark',
|
||||
age: 42,
|
||||
};
|
||||
|
||||
vm.resetData(data);
|
||||
|
||||
expect(vm.originalData).toEqual(data);
|
||||
expect(vm.formData).toEqual(data);
|
||||
expect(vm.watchChanges).not.toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('saveChanges()', () => {
|
||||
data = [{
|
||||
name: 'Tony',
|
||||
lastName: 'Stark',
|
||||
age: 42,
|
||||
}];
|
||||
|
||||
it('should call saveFn if exists', async () => {
|
||||
jtubau marked this conversation as resolved
Outdated
jorgep
commented
Porque vuelves a crear una instancia de crudmodel? No puedes usar la que ya tenías? Algo tipo vm.$props.saveFn = ....? o pasarlo mockeado desde el principio... Porque vuelves a crear una instancia de crudmodel? No puedes usar la que ya tenías? Algo tipo vm.$props.saveFn = ....? o pasarlo mockeado desde el principio...
|
||||
const saveFnMock = vi.fn();
|
||||
|
||||
const localVm = createWrapper(CrudModel, {
|
||||
global: {
|
||||
stubs: [
|
||||
'vnPaginate',
|
||||
'useState',
|
||||
'arrayData',
|
||||
'useStateStore',
|
||||
'vue-i18n',
|
||||
],
|
||||
mocks: {
|
||||
validate: vi.fn(),
|
||||
},
|
||||
},
|
||||
propsData: {
|
||||
dataRequired: {
|
||||
fk: 1,
|
||||
},
|
||||
dataKey: 'crudModelKey',
|
||||
model: 'crudModel',
|
||||
url: 'crudModelUrl',
|
||||
saveFn: saveFnMock,
|
||||
},
|
||||
});
|
||||
|
||||
localVm.vm.saveChanges(data);
|
||||
expect(saveFnMock).toHaveBeenCalledOnce();
|
||||
expect(localVm.vm.isLoading).toBe(false);
|
||||
expect(localVm.vm.hasChanges).toBe(false);
|
||||
});
|
||||
|
||||
it('should not call saveFn if not exists', async () => {
|
||||
jtubau marked this conversation as resolved
Outdated
jorgep
commented
El nombre del test no me acaba de convencer... el test lo veo bien. Algo tipo should use the default url if there's not saveFn. Algo así. El nombre del test no me acaba de convencer... el test lo veo bien. Algo tipo should use the default url if there's not saveFn. Algo así.
|
||||
const postMock =vi.spyOn(axios, 'post');
|
||||
|
||||
vm.formData = [{
|
||||
name: 'Bruce',
|
||||
lastName: 'Wayne',
|
||||
age: 45,
|
||||
}]
|
||||
|
||||
await vm.saveChanges(data);
|
||||
|
||||
expect(postMock).toHaveBeenCalledWith(vm.url + '/crud', data);
|
||||
expect(vm.isLoading).toBe(false);
|
||||
expect(vm.hasChanges).toBe(false);
|
||||
expect(vm.originalData).toEqual(JSON.parse(JSON.stringify(vm.formData)));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue
Aquí deberias cambiar un dato o varios de formatData y ver al llamar a la fn reset esta vuelve a tener el valor original.
formData su valor al inicio del test es un array vacío, por eso al comprobarlo, lo hago contra el argumento que se le pasa a la función, ya que ese es el valor que deben de tener tanto originalData como formData.