This commit is contained in:
parent
5cbff5035b
commit
1cf817be17
|
@ -127,7 +127,7 @@ function resetData(data) {
|
|||
originalData.value = JSON.parse(JSON.stringify(data));
|
||||
formData.value = JSON.parse(JSON.stringify(data));
|
||||
|
||||
if (watchChanges.value) watchChanges.value(); //destoy watcher
|
||||
if (watchChanges.value) watchChanges.value(); //destroy watcher
|
||||
watchChanges.value = watch(formData, () => (hasChanges.value = true), { deep: true });
|
||||
}
|
||||
|
||||
|
@ -271,7 +271,7 @@ function getChanges() {
|
|||
function isEmpty(obj) {
|
||||
if (obj == null) return true;
|
||||
if (Array.isArray(obj)) return !obj.length;
|
||||
return Object.keys(obj).length === 0 ;
|
||||
return !Object.keys(obj).length;
|
||||
}
|
||||
|
||||
async function reload(params) {
|
||||
|
|
|
@ -3,10 +3,11 @@ import CrudModel from 'components/CrudModel.vue';
|
|||
import { vi, afterEach, beforeEach, beforeAll, describe, expect, it } from 'vitest';
|
||||
|
||||
describe('CrudModel', () => {
|
||||
let wrapper;
|
||||
let vm;
|
||||
let data;
|
||||
beforeAll(() => {
|
||||
vm = createWrapper(CrudModel, {
|
||||
wrapper = createWrapper(CrudModel, {
|
||||
global: {
|
||||
stubs: [
|
||||
'vnPaginate',
|
||||
|
@ -26,8 +27,11 @@ describe('CrudModel', () => {
|
|||
dataKey: 'crudModelKey',
|
||||
model: 'crudModel',
|
||||
url: 'crudModelUrl',
|
||||
saveFn: '',
|
||||
},
|
||||
}).vm;
|
||||
});
|
||||
wrapper=wrapper.wrapper;
|
||||
vm=wrapper.vm;
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
|
@ -143,11 +147,6 @@ describe('CrudModel', () => {
|
|||
result = vm.isEmpty(dummyObj);
|
||||
|
||||
expect(result).toBe(true);
|
||||
|
||||
dummyArray = [];
|
||||
result = vm.isEmpty(dummyArray);
|
||||
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false if object is not empty', async () => {
|
||||
|
@ -155,6 +154,17 @@ describe('CrudModel', () => {
|
|||
result = vm.isEmpty(dummyObj);
|
||||
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it('should return true if array is empty', async () => {
|
||||
|
||||
dummyArray = [];
|
||||
result = vm.isEmpty(dummyArray);
|
||||
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false if array is not empty', async () => {
|
||||
|
||||
dummyArray = [1,2,3];
|
||||
result = vm.isEmpty(dummyArray);
|
||||
|
@ -210,39 +220,18 @@ describe('CrudModel', () => {
|
|||
}];
|
||||
|
||||
it('should call saveFn if exists', async () => {
|
||||
const saveFnMock = vi.fn();
|
||||
await wrapper.setProps({ saveFn: 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,
|
||||
},
|
||||
vm.saveChanges(data);
|
||||
|
||||
expect(vm.saveFn).toHaveBeenCalledOnce();
|
||||
expect(vm.isLoading).toBe(false);
|
||||
expect(vm.hasChanges).toBe(false);
|
||||
|
||||
await wrapper.setProps({ saveFn: '' });
|
||||
});
|
||||
|
||||
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 () => {
|
||||
it("should use default url if there's not saveFn", async () => {
|
||||
const postMock =vi.spyOn(axios, 'post');
|
||||
|
||||
vm.formData = [{
|
||||
|
|
Loading…
Reference in New Issue