From 644339bd13418af71c96ff980dcf2149851b0f7b Mon Sep 17 00:00:00 2001 From: Javier Segarra <jsegarra@verdnatura.es> Date: Sat, 1 Mar 2025 02:38:05 +0100 Subject: [PATCH] test: vnNotes restore --- .../common/__tests__/VnNotes.spec.js | 121 +++++++++++------- 1 file changed, 74 insertions(+), 47 deletions(-) diff --git a/src/components/common/__tests__/VnNotes.spec.js b/src/components/common/__tests__/VnNotes.spec.js index c6551554f..2603bf03c 100644 --- a/src/components/common/__tests__/VnNotes.spec.js +++ b/src/components/common/__tests__/VnNotes.spec.js @@ -1,7 +1,14 @@ -import { describe, it, expect, vi, beforeAll, afterEach, beforeEach } from 'vitest'; -import { createWrapper } from 'app/test/vitest/helper'; -import { default as axios } from 'axios'; - +import { + describe, + it, + expect, + vi, + beforeAll, + afterEach, + beforeEach, + afterAll, +} from 'vitest'; +import { createWrapper, axios } from 'app/test/vitest/helper'; import VnNotes from 'src/components/ui/VnNotes.vue'; import vnDate from 'src/boot/vnDate'; @@ -10,27 +17,24 @@ describe('VnNotes', () => { let wrapper; 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 }}; - } - - 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: [] }); - + let patchMock; + let expectedInsertBody; + let expectedUpdateBody; + const defaultOptions = { + url: '/test', + body: { name: 'Tony', lastName: 'Stark' }, + selectType: false, + saveUrl: null, + justInput: false, + }; + function generateWrapper( + options = defaultOptions, + text = null, + observationType = null, + ) { + vi.spyOn(axios, 'get').mockResolvedValue({ data: [] }); wrapper = createWrapper(VnNotes, { - propsData: { - url: '/test', - body: { name: 'Tony', lastName: 'Stark' }, - } + propsData: options, }); wrapper = wrapper.wrapper; vm = wrapper.vm; @@ -66,8 +70,9 @@ describe('VnNotes', () => { }); describe('insert', () => { - it('should not call axios.post and vnPaginateRef.fetch if newNote.text is null', async () => { - await setTestParams( null, null, true ); + it('should not call axios.post and vnPaginateRef.fetch when newNote.text is null', async () => { + generateWrapper({ selectType: true }); + createSpyFetch(); await vm.insert(); @@ -75,8 +80,9 @@ describe('VnNotes', () => { expect(spyFetch).not.toHaveBeenCalled(); }); - it('should not call axios.post and vnPaginateRef.fetch if newNote.text is empty', async () => { - await setTestParams( "", null, false ); + it('should not call axios.post and vnPaginateRef.fetch when newNote.text is empty', async () => { + generateWrapper(null, ''); + createSpyFetch(); await vm.insert(); @@ -84,8 +90,9 @@ describe('VnNotes', () => { expect(spyFetch).not.toHaveBeenCalled(); }); - it('should not call axios.post and vnPaginateRef.fetch if observationTypeFk is missing and selectType is true', async () => { - await setTestParams( "Test Note", null, true ); + it('should not call axios.post and vnPaginateRef.fetch when observationTypeFk is null and selectType is true', async () => { + generateWrapper({ selectType: true }, 'Test Note'); + createSpyFetch(); await vm.insert(); @@ -93,31 +100,20 @@ describe('VnNotes', () => { expect(spyFetch).not.toHaveBeenCalled(); }); - it('should call axios.post and vnPaginateRef.fetch if observationTypeFk is missing and selectType is false', async () => { - await setTestParams( "Test Note", null, false ); - + it('should call axios.post and vnPaginateRef.fetch when observationTypeFk is missing and selectType is false', async () => { + generateWrapper(null, 'Test Note'); + createSpyFetch(); generateExpectedBody(); 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 () => { - await setTestParams( "Test Note", 1, false ); - - generateExpectedBody(); - - await vm.insert(); - - expect(postMock).toHaveBeenCalledWith(vm.$props.url, expectedBody); + expect(postMock).toHaveBeenCalledWith(vm.$props.url, expectedInsertBody); expect(spyFetch).toHaveBeenCalled(); }); it('should call axios.post and vnPaginateRef.fetch when newNote is valid', async () => { - await setTestParams( "Test Note", 1, true ); - + generateWrapper({ selectType: true }, 'Test Note', 1); + createSpyFetch(); generateExpectedBody(); await vm.insert(); @@ -126,4 +122,35 @@ describe('VnNotes', () => { expect(spyFetch).toHaveBeenCalled(); }); }); + + describe('update', () => { + it('should call axios.patch with saveUrl when saveUrl is set and justInput is true', async () => { + generateWrapper({ + url: '/business', + justInput: true, + saveUrl: '/saveUrlTest', + }); + generateExpectedBody(); + + await vm.update(); + + expect(patchMock).toHaveBeenCalledWith(vm.$props.saveUrl, expectedUpdateBody); + }); + + it('should call axios.patch with url when saveUrl is not set and justInput is true', async () => { + generateWrapper({ + url: '/business', + body: { workerFk: 1110 }, + justInput: true, + }); + generateExpectedBody(); + + await vm.update(); + + expect(patchMock).toHaveBeenCalledWith( + `${vm.$props.url}/${vm.$props.body.workerFk}`, + expectedUpdateBody, + ); + }); + }); });