Skip to content

Commit

Permalink
Toast notifications: ignore undefined options (#126120)
Browse files Browse the repository at this point in the history
* Toast notifications: ignore undefined options

* just use omitBy
  • Loading branch information
pgayvallet authored Feb 22, 2022
1 parent 1b2f9a4 commit 2cabe1a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
34 changes: 32 additions & 2 deletions src/core/public/notifications/toasts/toasts_api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`);
Expand Down Expand Up @@ -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()', () => {
Expand Down Expand Up @@ -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()', () => {
Expand All @@ -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()', () => {
Expand All @@ -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()', () => {
Expand All @@ -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', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/core/public/notifications/toasts/toasts_api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -75,7 +76,7 @@ const normalizeToast = (toastOrTitle: ToastInput): ToastInputFields => {
title: toastOrTitle,
};
}
return toastOrTitle;
return omitBy(toastOrTitle, isUndefined);
};

/**
Expand Down

0 comments on commit 2cabe1a

Please sign in to comment.