From c164c39a2d70f802c1b5440baf5bb61ef9475604 Mon Sep 17 00:00:00 2001 From: Jtubau Date: Thu, 12 Dec 2024 15:52:36 +0100 Subject: [PATCH 1/6] feat: refs #7050 7050 add test to isEmpty() --- .../components/common/CrudModel.spec.js | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/test/vitest/__tests__/components/common/CrudModel.spec.js b/test/vitest/__tests__/components/common/CrudModel.spec.js index 6ce93e59c..e8cf43473 100644 --- a/test/vitest/__tests__/components/common/CrudModel.spec.js +++ b/test/vitest/__tests__/components/common/CrudModel.spec.js @@ -1,5 +1,6 @@ import { createWrapper } from 'app/test/vitest/helper'; import CrudModel from 'components/CrudModel.vue'; +import c from 'croppie'; import { vi, afterEach, beforeEach, beforeAll, describe, expect, it } from 'vitest'; describe('CrudModel', () => { @@ -117,4 +118,47 @@ describe('CrudModel', () => { }); }); }); + + describe('isEmpty()', () => { + let dummyObj; + let dummyArray; + let result; + it('should return true if object si null', async () => { + dummyObj = null; + result = vm.isEmpty(dummyObj); + + expect(result).toBe(true); + }); + + it('should return true if object si undefined', async () => { + dummyObj = undefined; + result = vm.isEmpty(dummyObj); + + expect(result).toBe(true); + }); + + it('should return true if object is empty', async () => { + dummyObj ={}; + 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 () => { + dummyObj = {a:1, b:2, c:3}; + result = vm.isEmpty(dummyObj); + + expect(result).toBe(false); + + dummyArray = [1,2,3]; + result = vm.isEmpty(dummyArray); + + expect(result).toBe(false); + }) + }); }); From 77f4949ba6297c4a585bd60e7ac43f51f6852682 Mon Sep 17 00:00:00 2001 From: Jtubau Date: Thu, 12 Dec 2024 15:53:56 +0100 Subject: [PATCH 2/6] feat: refs #7050 7050 add object check --- src/components/CrudModel.vue | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/components/CrudModel.vue b/src/components/CrudModel.vue index 7fdb54bc4..cff18f2de 100644 --- a/src/components/CrudModel.vue +++ b/src/components/CrudModel.vue @@ -270,10 +270,8 @@ function getChanges() { function isEmpty(obj) { if (obj == null) return true; - if (obj === undefined) return true; - if (Object.keys(obj).length === 0) return true; - - if (obj.length > 0) return false; + if (Array.isArray(obj)) return !obj.length; + return Object.keys(obj).length === 0 ; } async function reload(params) { From 0706c9d58ed0c28ed38f58f20f83a0b91b0252ca Mon Sep 17 00:00:00 2001 From: Jtubau Date: Thu, 12 Dec 2024 16:04:57 +0100 Subject: [PATCH 3/6] fix: refs #7050 delete import added by mistake --- test/vitest/__tests__/components/common/CrudModel.spec.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/vitest/__tests__/components/common/CrudModel.spec.js b/test/vitest/__tests__/components/common/CrudModel.spec.js index e8cf43473..2d7493cca 100644 --- a/test/vitest/__tests__/components/common/CrudModel.spec.js +++ b/test/vitest/__tests__/components/common/CrudModel.spec.js @@ -1,6 +1,5 @@ import { createWrapper } from 'app/test/vitest/helper'; import CrudModel from 'components/CrudModel.vue'; -import c from 'croppie'; import { vi, afterEach, beforeEach, beforeAll, describe, expect, it } from 'vitest'; describe('CrudModel', () => { From 9d955f45bbdb0947781106c734ef0bbe8841fc9b Mon Sep 17 00:00:00 2001 From: Jtubau Date: Wed, 18 Dec 2024 07:06:34 +0100 Subject: [PATCH 4/6] test: refs #7050 add tests to fns resetData() and saveChanges() --- .../components/common/CrudModel.spec.js | 101 +++++++++++++++++- 1 file changed, 100 insertions(+), 1 deletion(-) diff --git a/test/vitest/__tests__/components/common/CrudModel.spec.js b/test/vitest/__tests__/components/common/CrudModel.spec.js index 2d7493cca..b3cdbede7 100644 --- a/test/vitest/__tests__/components/common/CrudModel.spec.js +++ b/test/vitest/__tests__/components/common/CrudModel.spec.js @@ -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 () => { + 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 () => { + 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 () => { + 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))); + }); + }); }); From 1cf817be173892a8b3a9e8780aef00c8943f8b13 Mon Sep 17 00:00:00 2001 From: jtubau Date: Mon, 23 Dec 2024 15:09:14 +0100 Subject: [PATCH 5/6] refactor: refs #7050 refactorize --- src/components/CrudModel.vue | 4 +- .../components/common/CrudModel.spec.js | 65 ++++++++----------- 2 files changed, 29 insertions(+), 40 deletions(-) diff --git a/src/components/CrudModel.vue b/src/components/CrudModel.vue index cff18f2de..940b72ff0 100644 --- a/src/components/CrudModel.vue +++ b/src/components/CrudModel.vue @@ -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) { diff --git a/test/vitest/__tests__/components/common/CrudModel.spec.js b/test/vitest/__tests__/components/common/CrudModel.spec.js index b3cdbede7..e28b6003e 100644 --- a/test/vitest/__tests__/components/common/CrudModel.spec.js +++ b/test/vitest/__tests__/components/common/CrudModel.spec.js @@ -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); @@ -193,7 +203,7 @@ describe('CrudModel', () => { lastName: 'Stark', age: 42, }; - + vm.resetData(data); expect(vm.originalData).toEqual(data); @@ -210,39 +220,18 @@ describe('CrudModel', () => { }]; it('should call saveFn if exists', async () => { - 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, - }, - }); + await wrapper.setProps({ saveFn: vi.fn() }); - localVm.vm.saveChanges(data); - expect(saveFnMock).toHaveBeenCalledOnce(); - expect(localVm.vm.isLoading).toBe(false); - expect(localVm.vm.hasChanges).toBe(false); + vm.saveChanges(data); + + expect(vm.saveFn).toHaveBeenCalledOnce(); + expect(vm.isLoading).toBe(false); + expect(vm.hasChanges).toBe(false); + + await wrapper.setProps({ saveFn: '' }); }); - 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 = [{ From 638bd1dc260b1c2de2e3517eed18bb5227aed926 Mon Sep 17 00:00:00 2001 From: jtubau Date: Tue, 24 Dec 2024 12:59:40 +0100 Subject: [PATCH 6/6] refactor: refs #7050 removed blank spaces --- test/vitest/__tests__/components/common/CrudModel.spec.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/vitest/__tests__/components/common/CrudModel.spec.js b/test/vitest/__tests__/components/common/CrudModel.spec.js index e28b6003e..e0afd30ad 100644 --- a/test/vitest/__tests__/components/common/CrudModel.spec.js +++ b/test/vitest/__tests__/components/common/CrudModel.spec.js @@ -157,7 +157,6 @@ describe('CrudModel', () => { }); it('should return true if array is empty', async () => { - dummyArray = []; result = vm.isEmpty(dummyArray); @@ -165,7 +164,6 @@ describe('CrudModel', () => { }); it('should return false if array is not empty', async () => { - dummyArray = [1,2,3]; result = vm.isEmpty(dummyArray); @@ -174,7 +172,6 @@ describe('CrudModel', () => { }); describe('resetData()', () => { - it('should add $index to elements in data[] and sets originalData and formData with data', async () => { data = [{ name: 'Tony',