0
0
Fork 0

test: refs #8304 enhance VnNotes.spec.js with additional test cases and refactor setup functions

This commit is contained in:
Jose Antonio Tubau 2025-01-23 13:38:19 +01:00
parent adc9d1630b
commit 3a2fca110c
1 changed files with 69 additions and 47 deletions

View File

@ -1,51 +1,62 @@
import { describe, it, expect, vi, beforeAll, afterEach, beforeEach } from 'vitest'; import { describe, it, expect, vi, beforeAll, afterEach, beforeEach, afterAll } from 'vitest';
import { createWrapper, axios } from 'app/test/vitest/helper'; import { createWrapper, axios } from 'app/test/vitest/helper';
import VnNotes from 'src/components/ui/VnNotes.vue'; import VnNotes from 'src/components/ui/VnNotes.vue';
import vnDate from 'src/boot/vnDate';
describe('VnNotes', () => { describe('VnNotes', () => {
let vm; let vm;
let wrapper; let wrapper;
let spyFetch; let spyFetch;
let postMock; let postMock;
let expectedBody; let patchMock;
const mockData= {name: 'Tony', lastName: 'Stark', text: 'Test Note', observationTypeFk: 1}; let expectedInsertBody;
let expectedUpdateBody;
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: [] });
function generateWrapper({url = '/test', body = { name: 'Tony', lastName: 'Stark' }, text = null, observationType = null, selectType = false, saveUrl = null, justInput = false }) {
vi.spyOn(axios, 'get').mockResolvedValue({ data: [] });
wrapper = createWrapper(VnNotes, { wrapper = createWrapper(VnNotes, {
propsData: { propsData: {
url: '/test', url,
body: { name: 'Tony', lastName: 'Stark' }, saveUrl,
} body,
selectType,
justInput
},
}); });
wrapper = wrapper.wrapper; wrapper = wrapper.wrapper;
vm = wrapper.vm; 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(() => { beforeEach(() => {
postMock = vi.spyOn(axios, 'post').mockResolvedValue(mockData); postMock = vi.spyOn(axios, 'post');
spyFetch = vi.spyOn(vm.vnPaginateRef, 'fetch').mockImplementation(() => vi.fn()); patchMock = vi.spyOn(axios, 'patch');
}); });
afterEach(() => { afterEach(() => {
vi.clearAllMocks(); vi.clearAllMocks();
expectedBody = {}; expectedInsertBody = {};
expectedUpdateBody = {};
});
afterAll(() => {
vi.restoreAllMocks();
}); });
describe('insert', () => { describe('insert', () => {
it('should not call axios.post and vnPaginateRef.fetch if newNote.text is null', async () => { it('should not call axios.post and vnPaginateRef.fetch when newNote.text is null', async () => {
await setTestParams( null, null, true ); generateWrapper({ selectType: true });
createSpyFetch();
await vm.insert(); await vm.insert();
@ -53,8 +64,9 @@ describe('VnNotes', () => {
expect(spyFetch).not.toHaveBeenCalled(); expect(spyFetch).not.toHaveBeenCalled();
}); });
it('should not call axios.post and vnPaginateRef.fetch if newNote.text is empty', async () => { it('should not call axios.post and vnPaginateRef.fetch when newNote.text is empty', async () => {
await setTestParams( "", null, false ); generateWrapper({ text: "" });
createSpyFetch();
await vm.insert(); await vm.insert();
@ -62,8 +74,9 @@ describe('VnNotes', () => {
expect(spyFetch).not.toHaveBeenCalled(); expect(spyFetch).not.toHaveBeenCalled();
}); });
it('should not call axios.post and vnPaginateRef.fetch if observationTypeFk is missing and selectType is true', async () => { it('should not call axios.post and vnPaginateRef.fetch when observationTypeFk is null and selectType is true', async () => {
await setTestParams( "Test Note", null, true ); generateWrapper({ text: "Test Note", selectType: true });
createSpyFetch();
await vm.insert(); await vm.insert();
@ -71,37 +84,46 @@ describe('VnNotes', () => {
expect(spyFetch).not.toHaveBeenCalled(); expect(spyFetch).not.toHaveBeenCalled();
}); });
it('should call axios.post and vnPaginateRef.fetch if observationTypeFk is missing and selectType is false', async () => { it('should call axios.post and vnPaginateRef.fetch when observationTypeFk is missing and selectType is false', async () => {
await setTestParams( "Test Note", null, false ); generateWrapper({ text: "Test Note" });
createSpyFetch();
generateExpectedBody(); generateExpectedBody();
await vm.insert(); 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 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(); expect(spyFetch).toHaveBeenCalled();
}); });
it('should call axios.post and vnPaginateRef.fetch when newNote is valid', async () => { it('should call axios.post and vnPaginateRef.fetch when newNote is valid', async () => {
await setTestParams( "Test Note", 1, true ); generateWrapper({ text: "Test Note", observationType: 1, selectType: true });
createSpyFetch();
generateExpectedBody(); generateExpectedBody();
await vm.insert(); await vm.insert();
expect(postMock).toHaveBeenCalledWith(vm.$props.url, expectedBody); expect(postMock).toHaveBeenCalledWith(vm.$props.url, expectedInsertBody);
expect(spyFetch).toHaveBeenCalled(); 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);
});
});
}); });