From 0bd48d476b5c517201efdae94b5257eb449f8114 Mon Sep 17 00:00:00 2001 From: jtubau Date: Fri, 20 Dec 2024 10:35:31 +0100 Subject: [PATCH 1/5] test: refs #7100 added test to vnNotes component --- .../components/common/VnNotes.spec.js | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 test/vitest/__tests__/components/common/VnNotes.spec.js diff --git a/test/vitest/__tests__/components/common/VnNotes.spec.js b/test/vitest/__tests__/components/common/VnNotes.spec.js new file mode 100644 index 000000000..cdd97a924 --- /dev/null +++ b/test/vitest/__tests__/components/common/VnNotes.spec.js @@ -0,0 +1,66 @@ +import { describe, it, expect, vi, beforeAll, afterEach, beforeEach } from 'vitest'; +import { createWrapper, axios } from 'app/test/vitest/helper'; +import VnNotes from 'src/components/ui/VnNotes.vue'; + +describe('VnNotes', () => { + let vm; + let spyFetch; + let postMock; + + beforeAll(async () => { + vi.spyOn(axios, 'get').mockReturnValue({ data: [] }); + + vm = createWrapper(VnNotes, { + propsData: { + url: '/test', + filter: { order: 'created DESC' }, + body: { name: 'Tony', lastName: 'Stark' }, + addNote: false, + selectType: true, + } + }).vm; + }); + + beforeEach(() => { + postMock = vi.spyOn(axios, 'post').mockResolvedValue({ data: {name: 'Tony', lastName: 'Stark', text: 'Test Note', observationTypeFk: 1} }); + spyFetch = vi.spyOn(vm.vnPaginateRef, 'fetch').mockImplementation(() => vi.fn()); + }); + + afterEach(() => { + vi.clearAllMocks(); + }); + + describe('insert', () => { + it('should not call axios.post if newNote.text is empty', async () => { + vm.newNote.text = ''; + vm.newNote.observationTypeFk = 1; + + await vm.insert(); + + expect(postMock).not.toHaveBeenCalled(); + expect(spyFetch).not.toHaveBeenCalled(); + }); + + it('should not call axios.post if observationTypeFk is missing and selectType is set', async () => { + vm.newNote.text = 'Test Note'; + vm.newNote.observationTypeFk = null; + + await vm.insert(); + + expect(postMock).not.toHaveBeenCalled(); + expect(spyFetch).not.toHaveBeenCalled(); + }); + + it('should call axios.post and vnPaginateRef.fetch when newNote is valid', async () => { + vm.newNote.text = 'Test Note'; + vm.newNote.observationTypeFk = 1; + + const expectedBody = {...vm.$props.body, ...{ text: vm.newNote.text, observationTypeFk: vm.newNote.observationTypeFk }}; + + await vm.insert(); + + expect(postMock).toHaveBeenCalledWith(vm.$props.url, expectedBody); + expect(spyFetch).toHaveBeenCalled(); + }); + }); +}); \ No newline at end of file From 69451862bf79616f5ebe40408f6b7b387dc98c14 Mon Sep 17 00:00:00 2001 From: jtubau Date: Fri, 20 Dec 2024 13:20:11 +0100 Subject: [PATCH 2/5] test: refs #7100 modified test and added more cases --- .../components/common/VnNotes.spec.js | 64 +++++++++++++++---- 1 file changed, 53 insertions(+), 11 deletions(-) diff --git a/test/vitest/__tests__/components/common/VnNotes.spec.js b/test/vitest/__tests__/components/common/VnNotes.spec.js index cdd97a924..598ea4526 100644 --- a/test/vitest/__tests__/components/common/VnNotes.spec.js +++ b/test/vitest/__tests__/components/common/VnNotes.spec.js @@ -4,21 +4,23 @@ import VnNotes from 'src/components/ui/VnNotes.vue'; describe('VnNotes', () => { let vm; + let wrapper; let spyFetch; let postMock; + let expectedBody; beforeAll(async () => { vi.spyOn(axios, 'get').mockReturnValue({ data: [] }); - vm = createWrapper(VnNotes, { + wrapper = createWrapper(VnNotes, { propsData: { url: '/test', - filter: { order: 'created DESC' }, body: { name: 'Tony', lastName: 'Stark' }, - addNote: false, - selectType: true, + selectType: false, } - }).vm; + }); + wrapper = wrapper.wrapper; + vm = wrapper.vm; }); beforeEach(() => { @@ -31,9 +33,10 @@ describe('VnNotes', () => { }); describe('insert', () => { - it('should not call axios.post if newNote.text is empty', async () => { - vm.newNote.text = ''; - vm.newNote.observationTypeFk = 1; + it('should not call axios.post and vnPaginateRef.fetch if newNote.text is null', async () => { + vm.newNote.text = null; + vm.newNote.observationTypeFk = null; + await wrapper.setProps({ selectType: true }); await vm.insert(); @@ -41,9 +44,10 @@ describe('VnNotes', () => { expect(spyFetch).not.toHaveBeenCalled(); }); - it('should not call axios.post if observationTypeFk is missing and selectType is set', async () => { - vm.newNote.text = 'Test Note'; + it('should not call axios.post and vnPaginateRef.fetch if newNote.text is empty', async () => { + vm.newNote.text = ""; vm.newNote.observationTypeFk = null; + await wrapper.setProps({ selectType: false }); await vm.insert(); @@ -51,11 +55,49 @@ describe('VnNotes', () => { expect(spyFetch).not.toHaveBeenCalled(); }); + it('should not call axios.post and vnPaginateRef.fetch if observationTypeFk is missing and selectType is true', async () => { + vm.newNote.text = 'Test Note'; + vm.newNote.observationTypeFk = null; + await wrapper.setProps({ selectType: true }); + + await vm.insert(); + + expect(postMock).not.toHaveBeenCalled(); + expect(spyFetch).not.toHaveBeenCalled(); + }); + + it('should call axios.post and vnPaginateRef.fetch if observationTypeFk is missing and selectType is false', async () => { + vm.newNote.text = "Test Note"; + vm.newNote.observationTypeFk = null; + await wrapper.setProps({ selectType: false }); + + expectedBody = {...vm.$props.body, ...{ text: vm.newNote.text, observationTypeFk: vm.newNote.observationTypeFk }}; + + await vm.insert(); + + expect(postMock).toHaveBeenCalledWith(vm.$props.url, expectedBody); + expect(spyFetch).toHaveBeenCalled(); + }); + + it('should call axios.post and vnPaginateRef.fetch if observationTypeFk is setted and selectType is false', async () => { + vm.newNote.text = "Test Note"; + vm.newNote.observationTypeFk = 1; + await wrapper.setProps({ selectType: false }); + + expectedBody = {...vm.$props.body, ...{ text: vm.newNote.text, observationTypeFk: vm.newNote.observationTypeFk }}; + + await vm.insert(); + + expect(postMock).toHaveBeenCalledWith(vm.$props.url, expectedBody); + expect(spyFetch).toHaveBeenCalled(); + }); + it('should call axios.post and vnPaginateRef.fetch when newNote is valid', async () => { vm.newNote.text = 'Test Note'; vm.newNote.observationTypeFk = 1; + wrapper.setProps({ selectType: false }); - const expectedBody = {...vm.$props.body, ...{ text: vm.newNote.text, observationTypeFk: vm.newNote.observationTypeFk }}; + expectedBody = {...vm.$props.body, ...{ text: vm.newNote.text, observationTypeFk: vm.newNote.observationTypeFk }}; await vm.insert(); From 07afbc82efe2038278545a90311fe52c08f404e3 Mon Sep 17 00:00:00 2001 From: jtubau Date: Mon, 23 Dec 2024 09:29:50 +0100 Subject: [PATCH 3/5] refactor: refs #7100 delete unnecesary set prop --- test/vitest/__tests__/components/common/VnNotes.spec.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/vitest/__tests__/components/common/VnNotes.spec.js b/test/vitest/__tests__/components/common/VnNotes.spec.js index 598ea4526..c9b40007b 100644 --- a/test/vitest/__tests__/components/common/VnNotes.spec.js +++ b/test/vitest/__tests__/components/common/VnNotes.spec.js @@ -16,7 +16,6 @@ describe('VnNotes', () => { propsData: { url: '/test', body: { name: 'Tony', lastName: 'Stark' }, - selectType: false, } }); wrapper = wrapper.wrapper; From 771ecf1cc699e0ddbd8b10d4d38dd2770007be0c Mon Sep 17 00:00:00 2001 From: jtubau Date: Mon, 30 Dec 2024 07:40:25 +0100 Subject: [PATCH 4/5] refactor: refs #7100 refactorized with methods --- .../common/__tests__/VnNotes.spec.js | 43 +++++++++---------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/src/components/common/__tests__/VnNotes.spec.js b/src/components/common/__tests__/VnNotes.spec.js index c9b40007b..249e801d7 100644 --- a/src/components/common/__tests__/VnNotes.spec.js +++ b/src/components/common/__tests__/VnNotes.spec.js @@ -9,6 +9,16 @@ describe('VnNotes', () => { let postMock; let expectedBody; + function generateExpectedBody() { + expectedBody = {...vm.$props.body, ...{ text: vm.newNote.text, observationTypeFk: vm.newNote.observationTypeFk }}; + } + + async function setTestParams(text, observationType, type){ + vm.newNote.text = text; + vm.newNote.observationTypeFk = observationType; + wrapper.setProps({ selectType: type }); + } + beforeAll(async () => { vi.spyOn(axios, 'get').mockReturnValue({ data: [] }); @@ -29,13 +39,12 @@ describe('VnNotes', () => { afterEach(() => { vi.clearAllMocks(); + expectedBody = {}; }); describe('insert', () => { it('should not call axios.post and vnPaginateRef.fetch if newNote.text is null', async () => { - vm.newNote.text = null; - vm.newNote.observationTypeFk = null; - await wrapper.setProps({ selectType: true }); + await setTestParams( null, null, true ); await vm.insert(); @@ -44,9 +53,7 @@ describe('VnNotes', () => { }); it('should not call axios.post and vnPaginateRef.fetch if newNote.text is empty', async () => { - vm.newNote.text = ""; - vm.newNote.observationTypeFk = null; - await wrapper.setProps({ selectType: false }); + await setTestParams( "", null, false ); await vm.insert(); @@ -55,9 +62,7 @@ describe('VnNotes', () => { }); it('should not call axios.post and vnPaginateRef.fetch if observationTypeFk is missing and selectType is true', async () => { - vm.newNote.text = 'Test Note'; - vm.newNote.observationTypeFk = null; - await wrapper.setProps({ selectType: true }); + await setTestParams( "Test Note", null, true ); await vm.insert(); @@ -66,11 +71,9 @@ describe('VnNotes', () => { }); it('should call axios.post and vnPaginateRef.fetch if observationTypeFk is missing and selectType is false', async () => { - vm.newNote.text = "Test Note"; - vm.newNote.observationTypeFk = null; - await wrapper.setProps({ selectType: false }); + await setTestParams( "Test Note", null, false ); - expectedBody = {...vm.$props.body, ...{ text: vm.newNote.text, observationTypeFk: vm.newNote.observationTypeFk }}; + generateExpectedBody(); await vm.insert(); @@ -78,12 +81,10 @@ describe('VnNotes', () => { expect(spyFetch).toHaveBeenCalled(); }); - it('should call axios.post and vnPaginateRef.fetch if observationTypeFk is setted and selectType is false', async () => { - vm.newNote.text = "Test Note"; - vm.newNote.observationTypeFk = 1; - await wrapper.setProps({ selectType: false }); + it('should call axios.post and vnPaginateRef.fetch if observationTypeFk is setted and selectType is false', async () => { + await setTestParams( "Test Note", 1, false ); - expectedBody = {...vm.$props.body, ...{ text: vm.newNote.text, observationTypeFk: vm.newNote.observationTypeFk }}; + generateExpectedBody(); await vm.insert(); @@ -92,11 +93,9 @@ describe('VnNotes', () => { }); it('should call axios.post and vnPaginateRef.fetch when newNote is valid', async () => { - vm.newNote.text = 'Test Note'; - vm.newNote.observationTypeFk = 1; - wrapper.setProps({ selectType: false }); + await setTestParams( "Test Note", 1, true ); - expectedBody = {...vm.$props.body, ...{ text: vm.newNote.text, observationTypeFk: vm.newNote.observationTypeFk }}; + generateExpectedBody(); await vm.insert(); From 23afe3276ce5689b7ee428a383a43c759f7c8131 Mon Sep 17 00:00:00 2001 From: jtubau Date: Thu, 2 Jan 2025 15:13:36 +0100 Subject: [PATCH 5/5] refactor: refs #7100 added const mockData --- src/components/common/__tests__/VnNotes.spec.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/components/common/__tests__/VnNotes.spec.js b/src/components/common/__tests__/VnNotes.spec.js index 249e801d7..8f24a7f14 100644 --- a/src/components/common/__tests__/VnNotes.spec.js +++ b/src/components/common/__tests__/VnNotes.spec.js @@ -8,6 +8,7 @@ describe('VnNotes', () => { let spyFetch; let postMock; let expectedBody; + const mockData= {name: 'Tony', lastName: 'Stark', text: 'Test Note', observationTypeFk: 1}; function generateExpectedBody() { expectedBody = {...vm.$props.body, ...{ text: vm.newNote.text, observationTypeFk: vm.newNote.observationTypeFk }}; @@ -33,7 +34,7 @@ describe('VnNotes', () => { }); beforeEach(() => { - postMock = vi.spyOn(axios, 'post').mockResolvedValue({ data: {name: 'Tony', lastName: 'Stark', text: 'Test Note', observationTypeFk: 1} }); + postMock = vi.spyOn(axios, 'post').mockResolvedValue(mockData); spyFetch = vi.spyOn(vm.vnPaginateRef, 'fetch').mockImplementation(() => vi.fn()); });