From 771ecf1cc699e0ddbd8b10d4d38dd2770007be0c Mon Sep 17 00:00:00 2001 From: jtubau Date: Mon, 30 Dec 2024 07:40:25 +0100 Subject: [PATCH] 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();