From 2ac375b3fe751607a5ae69aa5c84b24f14b58e88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Andr=C3=A9s=20V=C3=A9lez=20Vidal?= Date: Wed, 31 May 2023 10:30:42 +0200 Subject: [PATCH] MM-52365 - fix JS error banner (#23501) * MM-52365 - fix js error banner * add null type to bindings as an optional type --- .../mattermost-redux/src/utils/apps.test.ts | 62 ++++++++++++++++++- .../mattermost-redux/src/utils/apps.ts | 25 +++++--- 2 files changed, 77 insertions(+), 10 deletions(-) diff --git a/webapp/channels/src/packages/mattermost-redux/src/utils/apps.test.ts b/webapp/channels/src/packages/mattermost-redux/src/utils/apps.test.ts index 122a67e8c2..900a151593 100644 --- a/webapp/channels/src/packages/mattermost-redux/src/utils/apps.test.ts +++ b/webapp/channels/src/packages/mattermost-redux/src/utils/apps.test.ts @@ -5,7 +5,7 @@ import {AppBinding, AppCall, AppField, AppForm, AppSelectOption} from '@mattermo import {AppBindingLocations, AppFieldTypes} from 'mattermost-redux/constants/apps'; -import {cleanForm, cleanBinding} from './apps'; +import {cleanForm, cleanBinding, validateBindings} from './apps'; describe('Apps Utils', () => { const basicCall: AppCall = { @@ -1315,4 +1315,64 @@ describe('Apps Utils', () => { expect(inBinding).toEqual(outBinding); }); }); + + describe('validateBindings', () => { + test('return validated binding when bindings are NOT empty', () => { + const outBinding: AppBinding = { + location: '/command', + bindings: [ + { + app_id: 'app', + location: '/command/loc1', + label: 'loc1', + bindings: [ + { + app_id: 'app', + location: '/command/loc1/loc11', + label: 'same', + form: { + submit: { + path: '/path', + }, + }, + }, + ], + }, + { + app_id: 'app', + location: '/command/loc2', + label: 'loc2', + form: { + submit: { + path: '/path', + }, + }, + }, + ], + } as AppBinding; + + const bindings = validateBindings([outBinding]); + expect([outBinding]).toEqual(bindings); + }); + + test('return empty array when bindings are empty', () => { + const inBinding: AppBinding = { + location: '/command', + bindings: [] as AppBinding[], + } as AppBinding; + + const bindings = validateBindings([inBinding]); + expect([]).toEqual(bindings); + }); + + test('return empty array when bindings is NULL', () => { + const inBinding: AppBinding = { + location: '/command', + bindings: null, + } as unknown as AppBinding; + + const bindings = validateBindings([inBinding]); + expect([]).toEqual(bindings); + }); + }); }); diff --git a/webapp/channels/src/packages/mattermost-redux/src/utils/apps.ts b/webapp/channels/src/packages/mattermost-redux/src/utils/apps.ts index 5128fb6d20..93c10641fd 100644 --- a/webapp/channels/src/packages/mattermost-redux/src/utils/apps.ts +++ b/webapp/channels/src/packages/mattermost-redux/src/utils/apps.ts @@ -101,17 +101,24 @@ function cleanBindingRec(binding: AppBinding, topLocation: string, depth: number }); } -export function validateBindings(bindings: AppBinding[] = []): AppBinding[] { - const channelHeaderBindings = bindings?.filter((b) => b.location === AppBindingLocations.CHANNEL_HEADER_ICON); - const postMenuBindings = bindings?.filter((b) => b.location === AppBindingLocations.POST_MENU_ITEM); - const commandBindings = bindings?.filter((b) => b.location === AppBindingLocations.COMMAND); +export function validateBindings(bindings: AppBinding[] | null = []): AppBinding[] { + if (!bindings || (bindings.length && bindings.length === 0)) { + return []; + } + const filterAndCleanBindings = (location: string): AppBinding[] => { + const filteredBindings = bindings.filter((b) => b.location === location); + if (filteredBindings?.length === 0) { + return []; + } + filteredBindings.forEach((b) => cleanBinding(b, location)); + return filteredBindings.filter((b) => b.bindings?.length); + }; - channelHeaderBindings.forEach((b) => cleanBinding(b, AppBindingLocations.CHANNEL_HEADER_ICON)); - postMenuBindings.forEach((b) => cleanBinding(b, AppBindingLocations.POST_MENU_ITEM)); - commandBindings.forEach((b) => cleanBinding(b, AppBindingLocations.COMMAND)); + const channelHeaderBindings = filterAndCleanBindings(AppBindingLocations.CHANNEL_HEADER_ICON); + const postMenuBindings = filterAndCleanBindings(AppBindingLocations.POST_MENU_ITEM); + const commandBindings = filterAndCleanBindings(AppBindingLocations.COMMAND); - const hasBindings = (b: AppBinding) => b.bindings?.length; - return postMenuBindings.filter(hasBindings).concat(channelHeaderBindings.filter(hasBindings), commandBindings.filter(hasBindings)); + return postMenuBindings.concat(channelHeaderBindings, commandBindings); } export function cleanForm(form?: AppForm) {