Toast notifications: ignore undefined options (#126120)

* Toast notifications: ignore undefined options

* just use omitBy
This commit is contained in:
Pierre Gayvallet 2022-02-22 11:31:56 +01:00 committed by GitHub
parent 1b2f9a43be
commit 2cabe1a68f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 3 deletions

View file

@ -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', () => {

View file

@ -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);
};
/**