2023-08-21 13:14:19 +00:00
|
|
|
import { createWrapper, axios } from 'app/test/vitest/helper';
|
2023-08-17 13:32:56 +00:00
|
|
|
import CrudModel from 'components/CrudModel.vue';
|
2023-08-25 10:33:38 +00:00
|
|
|
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: {
|
2023-08-24 13:05:31 +00:00
|
|
|
stubs: [
|
|
|
|
'vnPaginate',
|
|
|
|
'useState',
|
|
|
|
'arrayData',
|
|
|
|
'useStateStore',
|
2023-08-25 08:56:03 +00:00
|
|
|
'vue-i18n',
|
2023-08-24 13:05:31 +00:00
|
|
|
],
|
2023-08-21 13:14:19 +00:00
|
|
|
mocks: {
|
|
|
|
fetch: vi.fn(),
|
2023-08-24 13:05:31 +00:00
|
|
|
validate: vi.fn(),
|
2023-08-21 13:14:19 +00:00
|
|
|
},
|
2023-08-17 13:32:56 +00:00
|
|
|
},
|
|
|
|
propsData: {
|
|
|
|
dataRequired: {
|
2023-08-25 10:33:38 +00:00
|
|
|
fk: 1,
|
2023-08-17 13:32:56 +00:00
|
|
|
},
|
2023-08-21 13:14:19 +00:00
|
|
|
dataKey: 'crudModelKey',
|
2023-08-25 08:56:03 +00:00
|
|
|
model: 'crudModel',
|
2023-08-21 13:14:19 +00:00
|
|
|
url: 'crudModelUrl',
|
|
|
|
},
|
2023-08-17 13:32:56 +00:00
|
|
|
}).vm;
|
|
|
|
});
|
|
|
|
|
2023-08-25 10:33:38 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
vm.state.set('crudModel', []);
|
|
|
|
});
|
|
|
|
|
2023-08-17 13:32:56 +00:00
|
|
|
afterEach(() => {
|
|
|
|
vi.clearAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('insert()', () => {
|
2023-08-21 13:14:19 +00:00
|
|
|
it('should new element in list with index 0 if formData not has data', () => {
|
2023-08-17 13:32:56 +00:00
|
|
|
vm.insert();
|
|
|
|
|
2023-08-21 13:14:19 +00:00
|
|
|
expect(vm.formData.length).toEqual(1);
|
2023-08-25 10:33:38 +00:00
|
|
|
expect(vm.formData[0].fk).toEqual(1);
|
2023-08-21 13:14:19 +00:00
|
|
|
expect(vm.formData[0].$index).toEqual(0);
|
2023-08-17 13:32:56 +00:00
|
|
|
});
|
|
|
|
});
|
2023-08-25 10:33:38 +00:00
|
|
|
|
|
|
|
describe('getChanges()', () => {
|
|
|
|
it('should return correct updates and creates', async () => {
|
|
|
|
vm.originalData = [
|
|
|
|
{ id: 1, name: 'Tony Starks', $index: 1 },
|
|
|
|
{ id: 2, name: 'Jessica Jones', $index: 2 },
|
|
|
|
{ id: 3, name: 'Bruce Wayne', $index: 3 },
|
|
|
|
];
|
|
|
|
vm.state.set('crudModel', [
|
|
|
|
{ id: 1, name: 'New name one', $index: 1 },
|
|
|
|
{ id: 2, name: 'New name two', $index: 2 },
|
|
|
|
{ id: 3, name: 'Bruce Wayne', $index: 3 },
|
|
|
|
]);
|
|
|
|
|
|
|
|
vm.insert();
|
|
|
|
const result = vm.getChanges();
|
|
|
|
|
|
|
|
const expected = {
|
|
|
|
creates: [
|
|
|
|
{
|
|
|
|
$index: 4,
|
|
|
|
fk: 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
updates: [
|
|
|
|
{
|
|
|
|
data: {
|
|
|
|
name: 'New name one',
|
|
|
|
},
|
|
|
|
where: {
|
|
|
|
id: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
data: {
|
|
|
|
name: 'New name two',
|
|
|
|
},
|
|
|
|
where: {
|
|
|
|
id: 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(result).toEqual(expected);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO: remove test
|
2023-08-25 10:56:00 +00:00
|
|
|
describe.skip('remove()', () => {
|
|
|
|
it('should remove', async () => {
|
|
|
|
vi.spyOn(vm.quasar, 'dialog');
|
|
|
|
|
|
|
|
vm.originalData = [
|
|
|
|
{ id: 1, name: 'Tony Starks', $index: 1 },
|
|
|
|
{ id: 2, name: 'Jessica Jones', $index: 2 },
|
|
|
|
{ id: 3, name: 'Bruce Wayne', $index: 3 },
|
|
|
|
];
|
|
|
|
vm.state.set('crudModel', [
|
|
|
|
{ id: 1, name: 'New name one', $index: 1 },
|
|
|
|
{ id: 2, name: 'New name two', $index: 2 },
|
|
|
|
{ id: 3, name: 'Bruce Wayne', $index: 3 },
|
|
|
|
]);
|
|
|
|
|
|
|
|
vm.remove([{ id: 1 }]);
|
|
|
|
|
|
|
|
expect(vm.quasar.dialog).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
2023-08-25 10:33:38 +00:00
|
|
|
|
|
|
|
describe('getDifferences()', () => {
|
|
|
|
it('should return the differences between two objects', async () => {
|
|
|
|
const obj1 = {
|
|
|
|
a: 1,
|
|
|
|
b: 2,
|
|
|
|
c: 3,
|
|
|
|
};
|
|
|
|
const obj2 = {
|
|
|
|
a: 1,
|
|
|
|
b: 4,
|
|
|
|
d: 5,
|
|
|
|
};
|
|
|
|
|
|
|
|
const result = vm.getDifferences(obj1, obj2);
|
|
|
|
|
|
|
|
expect(result).toEqual({
|
|
|
|
b: 4,
|
|
|
|
d: 5,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2023-08-17 13:32:56 +00:00
|
|
|
});
|