test: vnNotes restore
gitea/salix-front/pipeline/pr-dev There was a failure building this commit Details

This commit is contained in:
Javier Segarra 2025-03-01 02:38:05 +01:00
parent bdda691ca9
commit 644339bd13
1 changed files with 74 additions and 47 deletions

View File

@ -1,7 +1,14 @@
import { describe, it, expect, vi, beforeAll, afterEach, beforeEach } from 'vitest'; import {
import { createWrapper } from 'app/test/vitest/helper'; describe,
import { default as axios } from 'axios'; 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 VnNotes from 'src/components/ui/VnNotes.vue';
import vnDate from 'src/boot/vnDate'; import vnDate from 'src/boot/vnDate';
@ -10,27 +17,24 @@ describe('VnNotes', () => {
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() { const defaultOptions = {
expectedBody = {...vm.$props.body, ...{ text: vm.newNote.text, observationTypeFk: vm.newNote.observationTypeFk }}; url: '/test',
} body: { name: 'Tony', lastName: 'Stark' },
selectType: false,
async function setTestParams(text, observationType, type){ saveUrl: null,
vm.newNote.text = text; justInput: false,
vm.newNote.observationTypeFk = observationType; };
wrapper.setProps({ selectType: type }); function generateWrapper(
} options = defaultOptions,
text = null,
beforeAll(async () => { observationType = null,
vi.spyOn(axios, 'get').mockReturnValue({ data: [] }); ) {
vi.spyOn(axios, 'get').mockResolvedValue({ data: [] });
wrapper = createWrapper(VnNotes, { wrapper = createWrapper(VnNotes, {
propsData: { propsData: options,
url: '/test',
body: { name: 'Tony', lastName: 'Stark' },
}
}); });
wrapper = wrapper.wrapper; wrapper = wrapper.wrapper;
vm = wrapper.vm; vm = wrapper.vm;
@ -66,8 +70,9 @@ describe('VnNotes', () => {
}); });
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();
@ -75,8 +80,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(null, '');
createSpyFetch();
await vm.insert(); await vm.insert();
@ -84,8 +90,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({ selectType: true }, 'Test Note');
createSpyFetch();
await vm.insert(); await vm.insert();
@ -93,31 +100,20 @@ 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(null, '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({ selectType: true }, 'Test Note', 1);
createSpyFetch();
generateExpectedBody(); generateExpectedBody();
await vm.insert(); await vm.insert();
@ -126,4 +122,35 @@ describe('VnNotes', () => {
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,
);
});
});
}); });