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

62 lines
1.9 KiB
JavaScript

import { createWrapper, axios } from 'app/test/vitest/helper';
import CrudModel from 'components/CrudModel.vue';
import { vi, afterEach, beforeAll, beforeEach, describe, expect, it } from 'vitest';
describe.only('CrudModel', () => {
let vm;
beforeAll(() => {
vm = createWrapper(CrudModel, {
global: {
stubs: ['vnPaginate', 'useState', 'arrayData', 'useStateStore'],
mocks: {
fetch: vi.fn(),
},
},
propsData: {
dataRequired: {
id: 1,
name: 'name',
autoLoad: true,
},
dataKey: 'crudModelKey',
model: 'crudModel',
url: 'crudModelUrl',
},
attrs: {
url: 'crudModelUrl',
dataKey: 'CustomerList',
order: 'id DESC',
limit: 3,
},
}).vm;
});
afterEach(() => {
vi.clearAllMocks();
});
describe('insert()', () => {
it('should new element in list with index 0 if formData not has data', () => {
vi.mock('src/composables/useValidator', () => ({
default: () => {},
fetch: () => {
vi.fn();
},
}));
vi.spyOn(axios, 'get').mockResolvedValue({
data: [
{ id: 1, name: 'Tony Stark' },
{ id: 2, name: 'Jessica Jones' },
{ id: 3, name: 'Bruce Wayne' },
],
});
vm.state.set('crudModel', []);
vm.insert();
expect(vm.formData.length).toEqual(1);
expect(vm.formData[0].id).toEqual(1);
expect(vm.formData[0].$index).toEqual(0);
});
});
});