From 69451862bf79616f5ebe40408f6b7b387dc98c14 Mon Sep 17 00:00:00 2001 From: jtubau Date: Fri, 20 Dec 2024 13:20:11 +0100 Subject: [PATCH] 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();