162 lines
4.9 KiB
JavaScript
162 lines
4.9 KiB
JavaScript
import { describe, it, expect, vi, afterEach, beforeEach, afterAll } from 'vitest';
|
|
import { createWrapper } from 'app/test/vitest/helper';
|
|
import { default as axios } from 'axios';
|
|
import VnNotes from 'src/components/ui/VnNotes.vue';
|
|
|
|
describe('VnNotes', () => {
|
|
let vm;
|
|
let wrapper;
|
|
let spyFetch;
|
|
let postMock;
|
|
let patchMock;
|
|
let deleteMock;
|
|
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: options,
|
|
});
|
|
wrapper = wrapper.wrapper;
|
|
vm = wrapper.vm;
|
|
vm.newNote.text = text;
|
|
vm.newNote.observationTypeFk = observationType;
|
|
}
|
|
|
|
function createSpyFetch() {
|
|
spyFetch = vi.spyOn(vm.$refs.vnPaginateRef, 'fetch');
|
|
}
|
|
|
|
function generateExpectedBody() {
|
|
expectedInsertBody = {
|
|
...vm.$props.body,
|
|
...{ text: vm.newNote.text, observationTypeFk: vm.newNote.observationTypeFk },
|
|
};
|
|
expectedUpdateBody = { ...vm.$props.body, ...{ notes: vm.newNote.text } };
|
|
}
|
|
|
|
beforeEach(() => {
|
|
postMock = vi.spyOn(axios, 'post');
|
|
patchMock = vi.spyOn(axios, 'patch');
|
|
deleteMock = vi.spyOn(axios, 'delete');
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.clearAllMocks();
|
|
expectedInsertBody = {};
|
|
expectedUpdateBody = {};
|
|
});
|
|
|
|
afterAll(() => {
|
|
vi.restoreAllMocks();
|
|
});
|
|
|
|
describe('insert', () => {
|
|
it('should not call axios.post and vnPaginateRef.fetch when newNote.text is null', async () => {
|
|
generateWrapper({ selectType: true });
|
|
createSpyFetch();
|
|
|
|
await vm.insert();
|
|
|
|
expect(postMock).not.toHaveBeenCalled();
|
|
expect(spyFetch).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('should not call axios.post and vnPaginateRef.fetch when newNote.text is empty', async () => {
|
|
generateWrapper(null, '');
|
|
createSpyFetch();
|
|
|
|
await vm.insert();
|
|
|
|
expect(postMock).not.toHaveBeenCalled();
|
|
expect(spyFetch).not.toHaveBeenCalled();
|
|
});
|
|
|
|
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();
|
|
|
|
expect(postMock).not.toHaveBeenCalled();
|
|
expect(spyFetch).not.toHaveBeenCalled();
|
|
});
|
|
|
|
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, expectedInsertBody);
|
|
expect(spyFetch).toHaveBeenCalled();
|
|
});
|
|
|
|
it('should call axios.post and vnPaginateRef.fetch when newNote is valid', async () => {
|
|
generateWrapper({ selectType: true }, 'Test Note', 1);
|
|
createSpyFetch();
|
|
generateExpectedBody();
|
|
|
|
await vm.insert();
|
|
|
|
expect(postMock).toHaveBeenCalledWith(vm.$props.url, expectedInsertBody);
|
|
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,
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('delete', () => {
|
|
it('Should call axios.delete with url and vnPaginateRef.fetch', async () => {
|
|
generateWrapper();
|
|
createSpyFetch();
|
|
|
|
await vm.deleteNote({ id: 1 });
|
|
|
|
expect(deleteMock).toHaveBeenCalledWith(`${vm.$props.url}/1`);
|
|
expect(spyFetch).toHaveBeenCalled();
|
|
});
|
|
});
|
|
});
|