import type { Propagation } from '$conditions-core'; import { createAppFormAction } from…
- User
import type { Propagation } from 'ufs-common';
- Assistant
import type { Propagation } from 'ufs-common';
import type { ConditionDto, ConditionMessage } from '$conditions-constructor-v2/entities/condition';
import { getErrorsWithIndexes } from '../helpers'; import { setErrors } from '../slices';
export interface ApplyConditionFormProps { errors: ConditionMessage[]; propagations: Propagation[]; }
export const [useApplyConditionForm, applyConditionFormErrors] = createAppFormAction< ApplyConditionFormProps, ConditionDto, Promise
(async ({ errors, propagations }, { setError }, { dispatch }) => { const errorsWithIndexes = getErrorsWithIndexes({ propagations, errors });
console.log(errorsWithIndexes);
dispatch(setErrors(errorsWithIndexes));
errorsWithIndexes.forEach((err) => {
if (err.severity === 'ERROR') {
setError(err.path, { types: { required: err.message } });
}
});
}); write jest tests
To write Jest tests for the applyConditionForm action that you defined using the createAppFormAction function, you'll want to ensure that you verify a few key aspects:
setError with the correct parameters for errors of severity 'ERROR'.Here’s an example of how you might structure these tests:
import { createAppFormAction } from '$ufs-common';
import { setErrors } from '../slices';
import { applyConditionForm } from './yourModule'; // Adjust the import based on your file structure
import { getErrorsWithIndexes } from '../helpers';
jest.mock('../helpers', () => ({
getErrorsWithIndexes: jest.fn(),
}));
jest.mock('../slices', () => ({
setErrors: jest.fn(),
}));
describe('applyConditionForm', () => {
const dispatch = jest.fn();
const setError = jest.fn();
afterEach(() => {
jest.clearAllMocks();
});
it('should dispatch errors with indexes and call setError for ERROR severity', async () => {
const errors = [
{ path: 'field1', message: 'Field 1 is required', severity: 'ERROR' },
{ path: 'field2', message: 'Field 2 is required', severity: 'WARNING' },
];
const propagations = [{ /* Mock propagations data */ }];
const errorsWithIndexes = [
{ path: 'field1', message: 'Field 1 is required', severity: 'ERROR' },
{ path: 'field2', message: 'Field 2 is required', severity: 'WARNING' },
];
(getErrorsWithIndexes as jest.Mock).mockReturnValue(errorsWithIndexes);
await applyConditionForm({ errors, propagations }, { setError }, { dispatch });
expect(getErrorsWithIndexes).toHaveBeenCalledWith({ propagations, errors });
// Check if dispatching setErrors
expect(dispatch).toHaveBeenCalledWith(setErrors(errorsWithIndexes));
// Check if setError is called correctly for errors
expect(setError).toHaveBeenCalledWith('field1', { types: { required: 'Field 1 is required' } });
expect(setError).not.toHaveBeenCalledWith('field2', expect.any(Object)); // Should not be called for WARNING severity
});
it('should not call setError for non-ERROR severity', async () => {
const errors = [
{ path: 'field1', message: 'This is an info', severity: 'INFO' },
{ path: 'field2', message: 'Field 2 is required', severity: 'WARNING' },
];
const propagations = [{ /* Mock propagations data */ }];
const errorsWithIndexes = [
{ path: 'field1', message: 'This is an info', severity: 'INFO' },
{ path: 'field2', message: 'Field 2 is required', severity: 'WARNING' },
];
(getErrorsWithIndexes as jest.Mock).mockReturnValue(errorsWithIndexes);
await applyConditionForm({ errors, propagations }, { setError }, { dispatch });
// Ensure that setError is never called
expect(setError).not.toHaveBeenCalled();
});
});
getErrorsWithIndexes to control test scenarios easily.applyConditionForm should correspond to your application's file structure.ConditionMessage and its severity are structured in your application, you may need to adjust the attributes or expectations accordingly.Not yet included in a published release.