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 VnNotes from 'src/components/ui/VnNotes.vue';
import vnDate from 'src/boot/vnDate';
@ -11,36 +20,43 @@ describe('VnNotes', () => {
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 }) {
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: {
url,
saveUrl,
body,
selectType,
justInput
},
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 }};
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');
patchMock = vi.spyOn(axios, 'patch');
});
afterEach(() => {
@ -65,7 +81,7 @@ describe('VnNotes', () => {
});
it('should not call axios.post and vnPaginateRef.fetch when newNote.text is empty', async () => {
generateWrapper({ text: "" });
generateWrapper(null, '');
createSpyFetch();
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 () => {
generateWrapper({ text: "Test Note", selectType: true });
generateWrapper({ selectType: true }, 'Test Note');
createSpyFetch();
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 () => {
generateWrapper({ text: "Test Note" });
generateWrapper(null, 'Test Note');
createSpyFetch();
generateExpectedBody();
await vm.insert();
expect(postMock).toHaveBeenCalledWith( vm.$props.url, expectedInsertBody );
expect(postMock).toHaveBeenCalledWith(vm.$props.url, expectedInsertBody);
expect(spyFetch).toHaveBeenCalled();
});
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();
generateExpectedBody();
await vm.insert();
expect(postMock).toHaveBeenCalledWith(vm.$props.url, expectedInsertBody);
@ -109,7 +125,11 @@ describe('VnNotes', () => {
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' });
generateWrapper({
url: '/business',
justInput: true,
saveUrl: '/saveUrlTest',
});
generateExpectedBody();
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 () => {
generateWrapper({ url: '/business', body: { workerFk: 1110 }, justInput: true });
generateWrapper({
url: '/business',
body: { workerFk: 1110 },
justInput: true,
});
generateExpectedBody();
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,
);
});
});
});
});