From 8e27ea59d58a3d7a3f05139ee61d6e54ce3a7266 Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Tue, 22 Feb 2022 07:07:54 -0500 Subject: [PATCH] Toast notifications: ignore undefined options (#126120) (#126134) * Toast notifications: ignore undefined options * just use omitBy (cherry picked from commit 2cabe1a68f01834db914d68cd6afc05501776468) Co-authored-by: Pierre Gayvallet --- .../notifications/toasts/toasts_api.test.ts | 34 +++++++++++++++++-- .../notifications/toasts/toasts_api.tsx | 3 +- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/core/public/notifications/toasts/toasts_api.test.ts b/src/core/public/notifications/toasts/toasts_api.test.ts index 87d703d5b6814..35f141a995d06 100644 --- a/src/core/public/notifications/toasts/toasts_api.test.ts +++ b/src/core/public/notifications/toasts/toasts_api.test.ts @@ -19,13 +19,13 @@ async function getCurrentToasts(toasts: ToastsApi) { function uiSettingsMock() { const mock = uiSettingsServiceMock.createSetupContract(); - mock.get.mockImplementation(() => (config: string) => { + mock.get.mockImplementation((config: string) => { switch (config) { case 'notifications:lifetime:info': return 5000; case 'notifications:lifetime:warning': return 10000; - case 'notification:lifetime:error': + case 'notifications:lifetime:error': return 30000; default: throw new Error(`Accessing ${config} is not supported in the mock.`); @@ -113,6 +113,12 @@ describe('#add()', () => { const toasts = new ToastsApi(toastDeps()); expect(toasts.add('foo')).toHaveProperty('title', 'foo'); }); + + it('fallbacks to default values for undefined properties', async () => { + const toasts = new ToastsApi(toastDeps()); + const toast = toasts.add({ title: 'foo', toastLifeTimeMs: undefined }); + expect(toast.toastLifeTimeMs).toEqual(5000); + }); }); describe('#remove()', () => { @@ -145,6 +151,12 @@ describe('#addInfo()', () => { expect(currentToasts[0].toastLifeTimeMs).toBe(1); expect(currentToasts[0]).toBe(toast); }); + + it('fallbacks to default values for undefined properties', async () => { + const toasts = new ToastsApi(toastDeps()); + const toast = toasts.addInfo({ title: 'foo', toastLifeTimeMs: undefined }); + expect(toast.toastLifeTimeMs).toEqual(5000); + }); }); describe('#addSuccess()', () => { @@ -159,6 +171,12 @@ describe('#addSuccess()', () => { const currentToasts = await getCurrentToasts(toasts); expect(currentToasts[0]).toBe(toast); }); + + it('fallbacks to default values for undefined properties', async () => { + const toasts = new ToastsApi(toastDeps()); + const toast = toasts.addSuccess({ title: 'foo', toastLifeTimeMs: undefined }); + expect(toast.toastLifeTimeMs).toEqual(5000); + }); }); describe('#addWarning()', () => { @@ -173,6 +191,12 @@ describe('#addWarning()', () => { const currentToasts = await getCurrentToasts(toasts); expect(currentToasts[0]).toBe(toast); }); + + it('fallbacks to default values for undefined properties', async () => { + const toasts = new ToastsApi(toastDeps()); + const toast = toasts.addWarning({ title: 'foo', toastLifeTimeMs: undefined }); + expect(toast.toastLifeTimeMs).toEqual(10000); + }); }); describe('#addDanger()', () => { @@ -187,6 +211,12 @@ describe('#addDanger()', () => { const currentToasts = await getCurrentToasts(toasts); expect(currentToasts[0]).toBe(toast); }); + + it('fallbacks to default values for undefined properties', async () => { + const toasts = new ToastsApi(toastDeps()); + const toast = toasts.addDanger({ title: 'foo', toastLifeTimeMs: undefined }); + expect(toast.toastLifeTimeMs).toEqual(10000); + }); }); describe('#addError', () => { diff --git a/src/core/public/notifications/toasts/toasts_api.tsx b/src/core/public/notifications/toasts/toasts_api.tsx index 5e5d9d5615fd4..5aaea1ca90a56 100644 --- a/src/core/public/notifications/toasts/toasts_api.tsx +++ b/src/core/public/notifications/toasts/toasts_api.tsx @@ -9,6 +9,7 @@ import { EuiGlobalToastListToast as EuiToast } from '@elastic/eui'; import React from 'react'; import * as Rx from 'rxjs'; +import { omitBy, isUndefined } from 'lodash'; import { ErrorToast } from './error_toast'; import { MountPoint } from '../../types'; @@ -75,7 +76,7 @@ const normalizeToast = (toastOrTitle: ToastInput): ToastInputFields => { title: toastOrTitle, }; } - return toastOrTitle; + return omitBy(toastOrTitle, isUndefined); }; /**