salix-front/test/vitest/__tests__/components/common/CrudModel.spec.js

121 lines
3.2 KiB
JavaScript
Raw Normal View History

import { createWrapper } from 'app/test/vitest/helper';
2023-08-17 13:32:56 +00:00
import CrudModel from 'components/CrudModel.vue';
import { vi, afterEach, beforeEach, beforeAll, describe, expect, it } from 'vitest';
2023-08-17 13:32:56 +00:00
2023-08-25 11:47:56 +00:00
describe('CrudModel', () => {
2023-08-17 13:32:56 +00:00
let vm;
beforeAll(() => {
vm = createWrapper(CrudModel, {
global: {
stubs: [
'vnPaginate',
'useState',
'arrayData',
'useStateStore',
'vue-i18n',
],
mocks: {
validate: vi.fn(),
},
2023-08-17 13:32:56 +00:00
},
propsData: {
dataRequired: {
fk: 1,
2023-08-17 13:32:56 +00:00
},
dataKey: 'crudModelKey',
model: 'crudModel',
url: 'crudModelUrl',
},
2023-08-17 13:32:56 +00:00
}).vm;
});
beforeEach(() => {
vm.fetch([]);
});
2023-08-17 13:32:56 +00:00
afterEach(() => {
vi.clearAllMocks();
});
describe('insert()', () => {
it('should new element in list with index 0 if formData not has data', () => {
2023-08-17 13:32:56 +00:00
vm.insert();
expect(vm.formData.length).toEqual(1);
expect(vm.formData[0].fk).toEqual(1);
expect(vm.formData[0].$index).toEqual(0);
2023-08-17 13:32:56 +00:00
});
});
describe('getChanges()', () => {
it('should return correct updates and creates', async () => {
vm.fetch([
{ id: 1, name: 'New name one' },
{ id: 2, name: 'New name two' },
{ id: 3, name: 'Bruce Wayne' },
]);
vm.originalData = [
{ id: 1, name: 'Tony Starks' },
{ id: 2, name: 'Jessica Jones' },
{ id: 3, name: 'Bruce Wayne' },
];
vm.insert();
const result = vm.getChanges();
const expected = {
creates: [
{
$index: 3,
fk: 1,
},
],
updates: [
{
data: {
name: 'New name one',
},
where: {
id: 1,
},
},
{
data: {
name: 'New name two',
},
where: {
id: 2,
},
},
],
};
expect(result).toEqual(expected);
});
});
describe('getDifferences()', () => {
it('should return the differences between two objects', async () => {
const obj1 = {
a: 1,
b: 2,
c: 3,
};
const obj2 = {
a: null,
b: 4,
d: 5,
};
const result = vm.getDifferences(obj1, obj2);
expect(result).toEqual({
a: null,
b: 4,
d: 5,
});
});
});
2023-08-17 13:32:56 +00:00
});