107 lines
3.9 KiB
JavaScript
107 lines
3.9 KiB
JavaScript
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;
|
|
|
|
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({ 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 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();
|
|
|
|
expect(postMock).not.toHaveBeenCalled();
|
|
expect(spyFetch).not.toHaveBeenCalled();
|
|
});
|
|
|
|
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();
|
|
|
|
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 () => {
|
|
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 });
|
|
|
|
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();
|
|
});
|
|
});
|
|
}); |