test: refs #7050 add tests to fns resetData() and saveChanges()
gitea/salix-front/pipeline/pr-dev This commit looks good
Details
gitea/salix-front/pipeline/pr-dev This commit looks good
Details
This commit is contained in:
parent
48805f977b
commit
9d955f45bb
|
@ -1,9 +1,10 @@
|
|||
import { createWrapper } from 'app/test/vitest/helper';
|
||||
import { createWrapper, axios } from 'app/test/vitest/helper';
|
||||
import CrudModel from 'components/CrudModel.vue';
|
||||
import { vi, afterEach, beforeEach, beforeAll, describe, expect, it } from 'vitest';
|
||||
|
||||
describe('CrudModel', () => {
|
||||
let vm;
|
||||
let data;
|
||||
beforeAll(() => {
|
||||
vm = createWrapper(CrudModel, {
|
||||
global: {
|
||||
|
@ -31,6 +32,7 @@ describe('CrudModel', () => {
|
|||
|
||||
beforeEach(() => {
|
||||
vm.fetch([]);
|
||||
vm.watchChanges = null;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
|
@ -160,4 +162,101 @@ describe('CrudModel', () => {
|
|||
expect(result).toBe(false);
|
||||
})
|
||||
});
|
||||
|
||||
describe('resetData()', () => {
|
||||
|
||||
it('should add $index to elements in data[] and sets originalData and formData with data', async () => {
|
||||
data = [{
|
||||
name: 'Tony',
|
||||
lastName: 'Stark',
|
||||
age: 42,
|
||||
}];
|
||||
|
||||
vm.resetData(data);
|
||||
|
||||
expect(vm.originalData).toEqual(data);
|
||||
expect(vm.originalData[0].$index).toEqual(0);
|
||||
expect(vm.formData).toEqual(data);
|
||||
expect(vm.formData[0].$index).toEqual(0);
|
||||
expect(vm.watchChanges).not.toBeNull();
|
||||
});
|
||||
|
||||
it('should dont do nothing if data is null', async () => {
|
||||
vm.resetData(null);
|
||||
|
||||
expect(vm.watchChanges).toBeNull();
|
||||
});
|
||||
|
||||
it('should set originalData and formatData with data and generate watchChanges', async () => {
|
||||
data = {
|
||||
name: 'Tony',
|
||||
lastName: 'Stark',
|
||||
age: 42,
|
||||
};
|
||||
|
||||
vm.resetData(data);
|
||||
|
||||
expect(vm.originalData).toEqual(data);
|
||||
expect(vm.formData).toEqual(data);
|
||||
expect(vm.watchChanges).not.toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('saveChanges()', () => {
|
||||
data = [{
|
||||
name: 'Tony',
|
||||
lastName: 'Stark',
|
||||
age: 42,
|
||||
}];
|
||||
|
||||
it('should call saveFn if exists', async () => {
|
||||
const saveFnMock = vi.fn();
|
||||
|
||||
const localVm = createWrapper(CrudModel, {
|
||||
global: {
|
||||
stubs: [
|
||||
'vnPaginate',
|
||||
'useState',
|
||||
'arrayData',
|
||||
'useStateStore',
|
||||
'vue-i18n',
|
||||
],
|
||||
mocks: {
|
||||
validate: vi.fn(),
|
||||
},
|
||||
},
|
||||
propsData: {
|
||||
dataRequired: {
|
||||
fk: 1,
|
||||
},
|
||||
dataKey: 'crudModelKey',
|
||||
model: 'crudModel',
|
||||
url: 'crudModelUrl',
|
||||
saveFn: saveFnMock,
|
||||
},
|
||||
});
|
||||
|
||||
localVm.vm.saveChanges(data);
|
||||
expect(saveFnMock).toHaveBeenCalledOnce();
|
||||
expect(localVm.vm.isLoading).toBe(false);
|
||||
expect(localVm.vm.hasChanges).toBe(false);
|
||||
});
|
||||
|
||||
it('should not call saveFn if not exists', async () => {
|
||||
const postMock =vi.spyOn(axios, 'post');
|
||||
|
||||
vm.formData = [{
|
||||
name: 'Bruce',
|
||||
lastName: 'Wayne',
|
||||
age: 45,
|
||||
}]
|
||||
|
||||
await vm.saveChanges(data);
|
||||
|
||||
expect(postMock).toHaveBeenCalledWith(vm.url + '/crud', data);
|
||||
expect(vm.isLoading).toBe(false);
|
||||
expect(vm.hasChanges).toBe(false);
|
||||
expect(vm.originalData).toEqual(JSON.parse(JSON.stringify(vm.formData)));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue