8627-devToTest #1421

Merged
alexm merged 768 commits from 8627-devToTest into test 2025-02-18 12:37:37 +00:00
1 changed files with 52 additions and 25 deletions
Showing only changes of commit 71967591d3 - Show all commits

View File

@ -1,4 +1,13 @@
import { describe, it, expect, vi, beforeAll, afterEach, beforeEach, afterAll } 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'; import vnDate from 'src/boot/vnDate';
@ -11,36 +20,43 @@ describe('VnNotes', () => {
let patchMock; let patchMock;
let expectedInsertBody; let expectedInsertBody;
let expectedUpdateBody; let expectedUpdateBody;
const defaultOptions = {
function generateWrapper({url = '/test', body = { name: 'Tony', lastName: 'Stark' }, text = null, observationType = null, selectType = false, saveUrl = null, justInput = false }) { 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: [] }); vi.spyOn(axios, 'get').mockResolvedValue({ data: [] });
wrapper = createWrapper(VnNotes, { wrapper = createWrapper(VnNotes, {
propsData: { propsData: options,
url,
saveUrl,
body,
selectType,
justInput
},
}); });
wrapper = wrapper.wrapper; wrapper = wrapper.wrapper;
vm = wrapper.vm; vm = wrapper.vm;
vm.newNote.text = text; vm.newNote.text = text;
vm.newNote.observationTypeFk = observationType; vm.newNote.observationTypeFk = observationType;
}; }
function createSpyFetch() { function createSpyFetch() {
spyFetch = vi.spyOn(vm.$refs.vnPaginateRef, 'fetch'); spyFetch = vi.spyOn(vm.$refs.vnPaginateRef, 'fetch');
}; }
function generateExpectedBody() { function generateExpectedBody() {
expectedInsertBody = {...vm.$props.body, ...{ text: vm.newNote.text, observationTypeFk: vm.newNote.observationTypeFk }}; expectedInsertBody = {
expectedUpdateBody = {...vm.$props.body, ...{ notes: vm.newNote.text }}; ...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'); postMock = vi.spyOn(axios, 'post');
patchMock = vi.spyOn(axios, 'patch'); patchMock = vi.spyOn(axios, 'patch');
}); });
afterEach(() => { afterEach(() => {
@ -65,7 +81,7 @@ describe('VnNotes', () => {
}); });
it('should not call axios.post and vnPaginateRef.fetch when newNote.text is empty', async () => { it('should not call axios.post and vnPaginateRef.fetch when newNote.text is empty', async () => {
generateWrapper({ text: "" }); generateWrapper(null, '');
createSpyFetch(); createSpyFetch();
await vm.insert(); await vm.insert();
@ -75,7 +91,7 @@ describe('VnNotes', () => {
}); });
it('should not call axios.post and vnPaginateRef.fetch when observationTypeFk is null and selectType is true', async () => { it('should not call axios.post and vnPaginateRef.fetch when observationTypeFk is null and selectType is true', async () => {
generateWrapper({ text: "Test Note", selectType: true }); generateWrapper({ selectType: true }, 'Test Note');
createSpyFetch(); createSpyFetch();
await vm.insert(); await vm.insert();
@ -85,21 +101,21 @@ describe('VnNotes', () => {
}); });
it('should call axios.post and vnPaginateRef.fetch when 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 () => {
generateWrapper({ text: "Test Note" }); generateWrapper(null, 'Test Note');
createSpyFetch(); createSpyFetch();
generateExpectedBody(); generateExpectedBody();
await vm.insert(); await vm.insert();
expect(postMock).toHaveBeenCalledWith( vm.$props.url, expectedInsertBody ); expect(postMock).toHaveBeenCalledWith(vm.$props.url, expectedInsertBody);
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 () => {
generateWrapper({ text: "Test Note", observationType: 1, selectType: true }); generateWrapper({ selectType: true }, 'Test Note', 1);
createSpyFetch(); createSpyFetch();
generateExpectedBody(); generateExpectedBody();
await vm.insert(); await vm.insert();
expect(postMock).toHaveBeenCalledWith(vm.$props.url, expectedInsertBody); expect(postMock).toHaveBeenCalledWith(vm.$props.url, expectedInsertBody);
@ -109,7 +125,11 @@ describe('VnNotes', () => {
describe('update', () => { describe('update', () => {
it('should call axios.patch with saveUrl when saveUrl is set and justInput is true', async () => { it('should call axios.patch with saveUrl when saveUrl is set and justInput is true', async () => {
generateWrapper({ url: '/business', justInput: true, saveUrl: '/saveUrlTest' }); generateWrapper({
url: '/business',
justInput: true,
saveUrl: '/saveUrlTest',
});
generateExpectedBody(); generateExpectedBody();
await vm.update(); await vm.update();
@ -118,12 +138,19 @@ describe('VnNotes', () => {
}); });
it('should call axios.patch with url when saveUrl is not set and justInput is true', async () => { 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 }); generateWrapper({
url: '/business',
body: { workerFk: 1110 },
justInput: true,
});
generateExpectedBody(); generateExpectedBody();
await vm.update(); await vm.update();
expect(patchMock).toHaveBeenCalledWith(`${vm.$props.url}/${vm.$props.body.workerFk}`, expectedUpdateBody); expect(patchMock).toHaveBeenCalledWith(
`${vm.$props.url}/${vm.$props.body.workerFk}`,
expectedUpdateBody,
);
}); });
}); });
}); });