From c9d73f69701dfc9bff0930ec97bc4b2d55f2b345 Mon Sep 17 00:00:00 2001 From: Rostislav Wolny Date: Mon, 2 Dec 2019 17:16:04 +0100 Subject: [PATCH] Add mapper for mapping blocks to form body data [MAILPOET-2451] --- .../form_editor/store/blocks_to_form_body.jsx | 35 ++++++++ .../store/blocks_to_form_body.spec.js | 85 +++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 assets/js/src/form_editor/store/blocks_to_form_body.jsx create mode 100644 tests/javascript/form_editor/store/blocks_to_form_body.spec.js diff --git a/assets/js/src/form_editor/store/blocks_to_form_body.jsx b/assets/js/src/form_editor/store/blocks_to_form_body.jsx new file mode 100644 index 0000000000..a4da675d66 --- /dev/null +++ b/assets/js/src/form_editor/store/blocks_to_form_body.jsx @@ -0,0 +1,35 @@ +export default (blocks) => { + let position = 1; + return blocks.map((block) => { + const mapped = { + params: {}, + }; + switch (block.name) { + case 'mailpoet-form/email-input': + mapped.id = 'email'; + mapped.type = 'text'; + mapped.unique = '0'; + mapped.static = '1'; + mapped.name = 'Email'; + mapped.params.label = block.attributes.label; + mapped.params.required = '1'; + if (block.attributes.labelWithinInput) { + mapped.params.label_within = '1'; + } + break; + case 'mailpoet-form/submit-button': + mapped.id = 'submit'; + mapped.type = 'submit'; + mapped.name = 'Submit'; + mapped.unique = '0'; + mapped.static = '1'; + mapped.params.label = block.attributes.label; + break; + default: + return null; + } + mapped.position = position.toString(); + position += 1; + return mapped; + }).filter(Boolean); +}; diff --git a/tests/javascript/form_editor/store/blocks_to_form_body.spec.js b/tests/javascript/form_editor/store/blocks_to_form_body.spec.js new file mode 100644 index 0000000000..e454058c50 --- /dev/null +++ b/tests/javascript/form_editor/store/blocks_to_form_body.spec.js @@ -0,0 +1,85 @@ +import { expect } from 'chai'; +import formBlocksToBody from '../../../../assets/js/src/form_editor/store/blocks_to_form_body.jsx'; + +const emailBlock = { + clientId: 'email', + isValid: true, + innerBlocks: [], + name: 'mailpoet-form/email-input', + attributes: { + id: 'email', + label: 'Email Address', + labelWithinInput: false, + }, +}; +const submitBlock = { + clientId: 'submit', + isValid: true, + innerBlocks: [], + name: 'mailpoet-form/submit-button', + attributes: { + id: 'submit', + label: 'Subscribe!', + }, +}; + +const checkBodyInputBasics = (input) => { + expect(input.id).to.be.a('string'); + expect(parseInt(input.position, 10)).to.be.a('number'); + expect(input.type).to.be.a('string'); + expect(input.type).to.be.not.empty; + expect(input.params).to.be.a('Object'); +}; + +describe('Form Body To Blocks', () => { + it('Should map email block to input data', () => { + const [input] = formBlocksToBody([emailBlock]); + checkBodyInputBasics(input); + expect(input.id).to.be.equal('email'); + expect(input.name).to.be.equal('Email'); + expect(input.type).to.be.equal('text'); + expect(input.position).to.be.equal('1'); + expect(input.unique).to.be.equal('0'); + expect(input.static).to.be.equal('1'); + expect(input.params.label).to.be.equal('Email Address'); + expect(input.params.required).to.be.equal('1'); + expect(input.params.label_within).to.be.undefined; + }); + + it('Should map email block with label within', () => { + const block = { ...emailBlock }; + block.attributes.labelWithinInput = true; + const [input] = formBlocksToBody([block]); + checkBodyInputBasics(input); + expect(input.params.label_within).to.be.equal('1'); + }); + + it('Should map submit block to input data', () => { + const [input] = formBlocksToBody([submitBlock]); + checkBodyInputBasics(input); + expect(input.id).to.be.equal('submit'); + expect(input.name).to.be.equal('Submit'); + expect(input.type).to.be.equal('submit'); + expect(input.position).to.be.equal('1'); + expect(input.unique).to.be.equal('0'); + expect(input.static).to.be.equal('1'); + expect(input.params.label).to.be.equal('Subscribe!'); + }); + + it('Should map multiple blocks at once', () => { + const unknownBlock = { + name: 'unknown', + clientId: '1234', + attributes: { + id: 'unknowns', + }, + }; + const inputs = formBlocksToBody([unknownBlock, submitBlock, emailBlock]); + inputs.map(checkBodyInputBasics); + expect(inputs.length).to.be.equal(2); + expect(inputs[0].id).to.be.equal('submit'); + expect(inputs[0].position).to.be.equal('1'); + expect(inputs[1].id).to.be.equal('email'); + expect(inputs[1].position).to.be.equal('2'); + }); +});