Add test and fixes form form validator
[MAILPOET-2451]
This commit is contained in:
committed by
Jack Kitterhing
parent
f284812037
commit
fe204ec53f
@@ -1,10 +1,16 @@
|
|||||||
export default (formData, formBlocks) => {
|
export default (formData, formBlocks) => {
|
||||||
|
if (!formData || !formData.settings || !Array.isArray(formData.settings.segments)) {
|
||||||
|
throw new Error('formData.settings.segments are expected to be an array.');
|
||||||
|
}
|
||||||
|
if (!Array.isArray(formBlocks)) {
|
||||||
|
throw new Error('formBlocks are expected to be an array.');
|
||||||
|
}
|
||||||
const errors = [];
|
const errors = [];
|
||||||
if (!formData.settings.segments || formData.settings.segments.length === 0) {
|
if (!formData.settings.segments || formData.settings.segments.length === 0) {
|
||||||
errors.push('missing-lists');
|
errors.push('missing-lists');
|
||||||
}
|
}
|
||||||
const emailInput = formBlocks.filter((block) => (block.attributes.id === 'email'));
|
const emailInput = formBlocks.find((block) => (block.name === 'mailpoet-form/email-input'));
|
||||||
const submit = formBlocks.filter((block) => (block.attributes.id === 'submit'));
|
const submit = formBlocks.find((block) => (block.name === 'mailpoet-form/submit-button'));
|
||||||
if (!emailInput) {
|
if (!emailInput) {
|
||||||
errors.push('missing-email-input');
|
errors.push('missing-email-input');
|
||||||
}
|
}
|
||||||
|
@@ -7,7 +7,6 @@ const emailBlock = {
|
|||||||
innerBlocks: [],
|
innerBlocks: [],
|
||||||
name: 'mailpoet-form/email-input',
|
name: 'mailpoet-form/email-input',
|
||||||
attributes: {
|
attributes: {
|
||||||
id: 'email',
|
|
||||||
label: 'Email Address',
|
label: 'Email Address',
|
||||||
labelWithinInput: false,
|
labelWithinInput: false,
|
||||||
},
|
},
|
||||||
@@ -18,7 +17,6 @@ const submitBlock = {
|
|||||||
innerBlocks: [],
|
innerBlocks: [],
|
||||||
name: 'mailpoet-form/submit-button',
|
name: 'mailpoet-form/submit-button',
|
||||||
attributes: {
|
attributes: {
|
||||||
id: 'submit',
|
|
||||||
label: 'Subscribe!',
|
label: 'Subscribe!',
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
96
tests/javascript/form_editor/store/form_validator.spec.js
Normal file
96
tests/javascript/form_editor/store/form_validator.spec.js
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
import { expect } from 'chai';
|
||||||
|
import validate from '../../../../assets/js/src/form_editor/store/form_validator.jsx';
|
||||||
|
|
||||||
|
const emailBlock = {
|
||||||
|
clientId: 'email',
|
||||||
|
isValid: true,
|
||||||
|
innerBlocks: [],
|
||||||
|
name: 'mailpoet-form/email-input',
|
||||||
|
attributes: {
|
||||||
|
label: 'Email Address',
|
||||||
|
labelWithinInput: false,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const submitBlock = {
|
||||||
|
clientId: 'submit',
|
||||||
|
isValid: true,
|
||||||
|
innerBlocks: [],
|
||||||
|
name: 'mailpoet-form/submit-button',
|
||||||
|
attributes: {
|
||||||
|
label: 'Subscribe!',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
describe('Form validator', () => {
|
||||||
|
it('Should return no errors for valid data', () => {
|
||||||
|
const formData = {
|
||||||
|
settings: {
|
||||||
|
segments: [1],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const blocks = [emailBlock, submitBlock];
|
||||||
|
const result = validate(formData, blocks);
|
||||||
|
expect(result).to.be.empty;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Should return error for missing lists', () => {
|
||||||
|
const formData = {
|
||||||
|
settings: {
|
||||||
|
segments: [],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const blocks = [emailBlock, submitBlock];
|
||||||
|
const result = validate(formData, blocks);
|
||||||
|
expect(result).to.contain('missing-lists');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Should return error for missing email', () => {
|
||||||
|
const formData = {
|
||||||
|
settings: {
|
||||||
|
segments: [1],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const blocks = [submitBlock];
|
||||||
|
const result = validate(formData, blocks);
|
||||||
|
expect(result).to.contain('missing-email-input');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Should return error for missing submit', () => {
|
||||||
|
const formData = {
|
||||||
|
settings: {
|
||||||
|
segments: [1],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const blocks = [emailBlock];
|
||||||
|
const result = validate(formData, blocks);
|
||||||
|
expect(result).to.contain('missing-submit');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Should return multiple errors', () => {
|
||||||
|
const formData = {
|
||||||
|
settings: {
|
||||||
|
segments: [],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const blocks = [];
|
||||||
|
const result = validate(formData, blocks);
|
||||||
|
expect(result).to.contain('missing-submit');
|
||||||
|
expect(result).to.contain('missing-email-input');
|
||||||
|
expect(result).to.contain('missing-lists');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('Should throw errors for invalid inputs', () => {
|
||||||
|
const formData = {
|
||||||
|
settings: {
|
||||||
|
segments: [1],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const blocks = [emailBlock, submitBlock];
|
||||||
|
const formDataError = 'formData.settings.segments are expected to be an array.';
|
||||||
|
expect(() => validate(null, blocks)).to.throw(formDataError);
|
||||||
|
expect(() => validate({ settings: {} }, blocks)).to.throw(formDataError);
|
||||||
|
const blocksError = 'formBlocks are expected to be an array.';
|
||||||
|
expect(() => validate(formData, null)).to.throw(blocksError);
|
||||||
|
expect(() => validate(formData, 'string')).to.throw(blocksError);
|
||||||
|
});
|
||||||
|
});
|
Reference in New Issue
Block a user