test: refs #8304 enhance VnNotes.spec.js with additional test cases and refactor setup functions
gitea/salix-front/pipeline/pr-dev There was a failure building this commit Details

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