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