unit test fixes

This commit is contained in:
Virginie Ky 2019-08-02 10:54:33 +02:00
parent 020ebac000
commit 5741b200cf
4 changed files with 45 additions and 16 deletions

View File

@ -74,6 +74,13 @@ class AttributeForm extends React.Component {
const { custom, validations } = formValidations[current];
const value = modifiedData[current];
if (
current === 'name' &&
!new RegExp('^[A-Za-z][_0-9A-Za-z]*$').test(value)
) {
acc[current] = [{ id: `${pluginId}.error.validation.regex.name` }];
}
if (!value && validations.required === true && custom !== true) {
acc[current] = [{ id: `${pluginId}.error.validation.required` }];
}
@ -82,13 +89,6 @@ class AttributeForm extends React.Component {
acc[current] = [{ id: `${pluginId}.error.validation.required` }];
}
if (
current === 'name' &&
!new RegExp('^[A-Za-z][_0-9A-Za-z]*$').test(value)
) {
acc[current] = [{ id: `${pluginId}.error.validation.regex.name` }];
}
if (current === 'enum' && !!value) {
const split = value.split('\n');

View File

@ -79,7 +79,7 @@ describe('<AttributeForm />', () => {
wrapper
.find(FormattedMessage)
.at(0)
.prop('id'),
.prop('id')
).toContain('edit');
});
@ -101,7 +101,7 @@ describe('<AttributeForm />', () => {
describe('instances', () => {
describe('GetFormErrors', () => {
it('should return an empty object if there is not field that contain the created field\'s name', () => {
it("should return an empty object if there is not field that contain the created field's name", () => {
props.modifiedData = { name: 'test' };
wrapper = renderComponent(props);
@ -111,7 +111,7 @@ describe('<AttributeForm />', () => {
expect(getFormErrors()).toEqual({});
});
it('should return an object with the input\'s name and an array of error if the name is empty', () => {
it("should return an object with the input's name and an array of error if the name is empty", () => {
wrapper = renderComponent(props);
const { getFormErrors } = wrapper.instance();
@ -121,6 +121,18 @@ describe('<AttributeForm />', () => {
});
});
it('should return a unique error if the name begins with a special character', () => {
props.modifiedData = { name: '_test' };
wrapper = renderComponent(props);
const { getFormErrors } = wrapper.instance();
expect(getFormErrors()).toEqual({
name: [{ id: `${pluginId}.error.validation.regex.name` }],
});
});
it('should return a unique error if the name of the field is already taken', () => {
props.alreadyTakenAttributes = ['test'];
props.modifiedData = { name: 'test' };
@ -200,7 +212,7 @@ describe('<AttributeForm />', () => {
'modalType=attributeForm&attributeType=string&settingType=advanced&actionType=create',
});
expect(context.emitEvent).toHaveBeenCalledWith(
'didSelectContentTypeFieldSettings',
'didSelectContentTypeFieldSettings'
);
});
});
@ -292,7 +304,7 @@ describe('<AttributeForm />', () => {
expect(props.onSubmitEdit).toHaveBeenCalledWith(true);
expect(context.emitEvent).toHaveBeenCalledWith(
'willAddMoreFieldToContentType',
'willAddMoreFieldToContentType'
);
});

View File

@ -100,16 +100,16 @@ class ModelForm extends React.Component {
};
}
if (modifiedData.name === '') {
formErrors = { name: [{ id: `${pluginId}.error.validation.required` }] };
}
if (!new RegExp('^[A-Za-z][_0-9A-Za-z]*$').test(modifiedData.name)) {
formErrors = {
name: [{ id: `${pluginId}.error.validation.regex.name` }],
};
}
if (modifiedData.name === '') {
formErrors = { name: [{ id: `${pluginId}.error.validation.required` }] };
}
this.setState(prevState => ({
formErrors,
didCheckErrors: !prevState.didCheckErrors,

View File

@ -254,6 +254,8 @@ describe('<ModelForm />', () => {
describe('HandleSubmit', () => {
it('should not submit if the form is empty', () => {
props.modifiedData.name = '';
wrapper = renderComponent(props);
const { handleSubmit } = wrapper.instance();
@ -266,6 +268,21 @@ describe('<ModelForm />', () => {
expect(wrapper.prop('push')).not.toHaveBeenCalled();
});
it('should not submit if the name of the CT begins with a special character', () => {
props.modifiedData.name = '_test';
wrapper = renderComponent(props);
const { handleSubmit } = wrapper.instance();
handleSubmit({ preventDefault: jest.fn() });
expect(wrapper.state('formErrors')).toEqual({
name: [{ id: `${pluginId}.error.validation.regex.name` }],
});
expect(wrapper.prop('push')).not.toHaveBeenCalled();
});
it('should not submit if the name of the CT is already taken', () => {
props.allTakenNames = ['test'];
props.modifiedData.name = 'test';