Skip to content

Commit

Permalink
feat(UIForms): add errors in triggers, expose error getter utils (#1030)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsomsanith-tlnd authored Feb 2, 2018
1 parent 70d6614 commit 2238561
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
7 changes: 6 additions & 1 deletion packages/forms/src/UIForm/UIForm.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,12 @@ export default class UIForm extends React.Component {
propertyName = schema.key[schema.key.length - 1];
this.onTrigger(event, { formData, formId: this.props.id, propertyName, value });
} else {
this.onTrigger(event, { trigger: schema.triggers[0], schema, properties: formData });
this.onTrigger(event, {
trigger: schema.triggers[0],
schema,
properties: formData,
errors,
});
}
}
}
Expand Down
33 changes: 33 additions & 0 deletions packages/forms/src/UIForm/UIForm.component.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,39 @@ describe('UIForm component', () => {
});

describe('#onFinish', () => {
it('should perform trigger', () => {
// given
const validData = {
...data,
properties: { firstname: 'toto' },
};
const wrapper = mount(<UIForm {...validData} {...props} />);
props.onTrigger.mockReturnValueOnce(Promise.resolve({}));

// when
wrapper
.find('input')
.at(1)
.simulate('blur');

// then
expect(props.onTrigger).toBeCalledWith(expect.anything(), {
trigger: 'after',
schema: {
key: ['firstname'],
title: 'First Name (with placeholder)',
placeholder: 'Enter your firstname here',
triggers: ['after'],
required: true,
schema: { type: 'string' },
ngModelOptions: {},
type: 'text',
},
properties: validData.properties,
errors: validData.errors,
});
});

it('should NOT perform trigger when field has errors', () => {
// given: required firstname is empty
const wrapper = mount(<UIForm {...data} {...props} />);
Expand Down
3 changes: 2 additions & 1 deletion packages/forms/src/UIForm/Widget/Widget.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import includes from 'lodash/includes';
import { sfPath } from 'talend-json-schema-form-core';

import defaultWidgets from '../utils/widgets';
import { getError } from '../utils/errors';
import { getValue } from '../utils/properties';

function shouldRender(conditions, properties) {
Expand All @@ -26,7 +27,7 @@ export default function Widget(props) {
}

const id = sfPath.name(key, '_', props.id);
const error = props.errors[key];
const error = getError(props.errors, props.schema);
const errorMessage = validationMessage || error;
return (
<WidgetImpl
Expand Down
4 changes: 4 additions & 0 deletions packages/forms/src/UIForm/utils/errors.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import omit from 'lodash/omit';

export function getError(errors, schema) {
return errors[schema.key];
}

export function removeError(errors, schema) {
return omit(errors, schema.key.toString());
}
Expand Down
16 changes: 15 additions & 1 deletion packages/forms/src/UIForm/utils/errors.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { removeError, addError } from './errors';
import { removeError, addError, getError } from './errors';

describe('Errors utils', () => {
describe('#removeError', () => {
Expand Down Expand Up @@ -34,4 +34,18 @@ describe('Errors utils', () => {
});
});
});

describe('#getError', () => {
it('should add error based on schema key', () => {
// given
const schema = { key: ['hakuna', 'matata'] };
const errors = { [schema.key]: 'this is it' };

// when
const errorMessage = getError(errors, schema);

// then
expect(errorMessage).toEqual('this is it');
});
});
});

0 comments on commit 2238561

Please sign in to comment.