test: refs #7052 add unit tests for EditTableCellValueForm component
gitea/salix-front/pipeline/pr-dev There was a failure building this commit Details

This commit is contained in:
Javi Gallego 2024-12-18 09:04:37 +01:00
parent 35de47f102
commit 1193eaa073
1 changed files with 55 additions and 0 deletions

View File

@ -0,0 +1,55 @@
import { createWrapper, axios } from 'app/test/vitest/helper';
import EditForm from 'components/EditTableCellValueForm.vue';
import { vi, afterEach, beforeAll, describe, expect, it } from 'vitest';
describe('EditForm', () => {
let vm;
const mockRows = [
{ id: 1, itemFk: 101 },
{ id: 2, itemFk: 102 },
];
const mockFieldsOptions = [
{ label: 'Field A', field: 'fieldA', component: 'input', attrs: {} },
{ label: 'Field B', field: 'fieldB', component: 'date', attrs: {} },
];
const editUrl = '/api/edit';
beforeAll(() => {
vi.spyOn(axios, 'post').mockResolvedValue({ status: 200 });
vm = createWrapper(EditForm, {
props: {
rows: mockRows,
fieldsOptions: mockFieldsOptions,
editUrl,
},
}).vm;
});
afterEach(() => {
vi.clearAllMocks();
});
describe('onSubmit()', () => {
it('should call axios.post with the correct parameters in the payload', async () => {
const selectedField = { field: 'fieldA', component: 'input', attrs: {} };
const newValue = 'Test Value';
vm.selectedField = selectedField;
vm.newValue = newValue;
await vm.onSubmit();
const payload = axios.post.mock.calls[0][1];
expect(axios.post).toHaveBeenCalledWith(editUrl, expect.any(Object));
expect(payload.field).toEqual('fieldA');
expect(payload.newValue).toEqual('Test Value');
expect(payload.lines).toContainEqual(
expect.objectContaining({ id: 1, itemFk: 101 })
);
expect(vm.isLoading).toEqual(false);
});
});
});