From bb36b55d7c3a06dea1a448fb6b588b12547141b4 Mon Sep 17 00:00:00 2001 From: Gervwyk Date: Wed, 16 Feb 2022 09:55:12 +0200 Subject: [PATCH] fix(build): Events with try defined should add default for catch. --- .../buildPages/buildBlock/buildEvents.js | 5 ++- .../buildPages/buildBlock/buildEvents.test.js | 36 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/packages/build/src/build/buildPages/buildBlock/buildEvents.js b/packages/build/src/build/buildPages/buildBlock/buildEvents.js index 5a66c607b..25ab200b8 100644 --- a/packages/build/src/build/buildPages/buildBlock/buildEvents.js +++ b/packages/build/src/build/buildPages/buildBlock/buildEvents.js @@ -64,7 +64,10 @@ function buildEvents(block, pageContext) { }". Received ${JSON.stringify(block.events[key].try)}` ); } - if (!type.isArray(block.events[key].catch) && !type.isNone(block.events[key].catch)) { + if (type.isNone(block.events[key].catch)) { + block.events[key].catch = []; + } + if (!type.isArray(block.events[key].catch)) { throw new Error( `Catch events must be an array of actions at "${ block.blockId diff --git a/packages/build/src/build/buildPages/buildBlock/buildEvents.test.js b/packages/build/src/build/buildPages/buildBlock/buildEvents.test.js index dbdd7ce0a..12a47fcb9 100644 --- a/packages/build/src/build/buildPages/buildBlock/buildEvents.test.js +++ b/packages/build/src/build/buildPages/buildBlock/buildEvents.test.js @@ -120,6 +120,42 @@ test('block events actions as try catch arrays', async () => { ]); }); +test('block events actions as try array and catch not defined.', async () => { + const components = { + pages: [ + { + id: 'page_1', + type: 'Container', + auth, + blocks: [ + { + id: 'block_1', + type: 'Input', + events: { + onClick: { + try: [ + { + id: 'action_1', + type: 'Reset', + }, + ], + }, + }, + }, + ], + }, + ], + }; + const res = await buildPages({ components, context }); + expect(get(res, 'pages.0.areas.content.blocks.0.events.onClick.try')).toEqual([ + { + id: 'action_1', + type: 'Reset', + }, + ]); + expect(get(res, 'pages.0.areas.content.blocks.0.events.onClick.catch')).toEqual([]); +}); + test('block events actions try not an array', async () => { const components = { pages: [