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 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: [] }); wrapper = createWrapper(VnNotes, { propsData: { url: '/test', body: { name: 'Tony', lastName: 'Stark' }, } }); wrapper = wrapper.wrapper; vm = wrapper.vm; }); beforeEach(() => { postMock = vi.spyOn(axios, 'post').mockResolvedValue(mockData); spyFetch = vi.spyOn(vm.vnPaginateRef, 'fetch').mockImplementation(() => vi.fn()); }); afterEach(() => { vi.clearAllMocks(); expectedBody = {}; }); describe('insert', () => { it('should not call axios.post and vnPaginateRef.fetch if newNote.text is null', async () => { await setTestParams( null, null, true ); await vm.insert(); expect(postMock).not.toHaveBeenCalled(); expect(spyFetch).not.toHaveBeenCalled(); }); it('should not call axios.post and vnPaginateRef.fetch if newNote.text is empty', async () => { await setTestParams( "", null, false ); await vm.insert(); expect(postMock).not.toHaveBeenCalled(); 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 ); 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 () => { await setTestParams( "Test Note", null, false ); 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(spyFetch).toHaveBeenCalled(); }); it('should call axios.post and vnPaginateRef.fetch when newNote is valid', async () => { await setTestParams( "Test Note", 1, true ); generateExpectedBody(); await vm.insert(); expect(postMock).toHaveBeenCalledWith(vm.$props.url, expectedBody); expect(spyFetch).toHaveBeenCalled(); }); }); });