Skip to content

Commit

Permalink
test(notifications): add more test cases to deduplicate_toasts tests
Browse files Browse the repository at this point in the history
  • Loading branch information
delanni committed Jul 19, 2023
1 parent 1d87243 commit 15dd131
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import { deduplicateToasts } from './deduplicate_toasts';
import { Toast } from '@kbn/core-notifications-browser';
import { render } from 'enzyme';
import { ReactElement, ReactNode } from 'react';

const mockDismissToast = jest.fn();

Expand All @@ -28,21 +30,44 @@ describe('deduplicate toasts', () => {
expect(deduplicatedToastList).toHaveLength(0);
});

it("doesn't affect singular notifications", () => {
const toasts: Toast[] = [
toast('A', 'B'), // single toast
toast('X', 'Y'), // single toast
];

const deduplicatedToastList = deduplicateToasts(toasts, mockDismissToast);

expect(deduplicatedToastList).toHaveLength(toasts.length);
verifyTextAndTitle(deduplicatedToastList[0], 'A', 'B');
verifyTextAndTitle(deduplicatedToastList[1], 'X', 'Y');
});

it('groups toasts based on title + name', () => {
const toasts: Toast[] = [
toast('A', 'B'), // 2 of these
toast('X', 'Y'), // 3 of these
toast('A', 'B'),
toast('X', 'Y'),
toast('A', 'B'),
toast('A', 'C'),
toast('A', 'C'),
toast('A', 'C'), // 1 of these
toast('X', 'Y'),
];

const deduplicatedToastList = deduplicateToasts(toasts, mockDismissToast);

expect(deduplicatedToastList).toHaveLength(3);
expect(deduplicatedToastList[0].text).toBe('B');
expect(deduplicatedToastList[1].text).toBe('Y');
expect(deduplicatedToastList[2].text).toBe('C');
verifyTextAndTitle(deduplicatedToastList[0], 'A 2', 'B');
verifyTextAndTitle(deduplicatedToastList[1], 'X 3', 'Y');
verifyTextAndTitle(deduplicatedToastList[2], 'A', 'C');
});
});

function verifyTextAndTitle({ text, title }: Toast, expectedTitle: string, expectedText: string) {
expect(getNodeText(title)).toBe(expectedTitle);
expect(text).toBe(expectedText);
}

function getNodeText(node: ReactNode) {
const rendered = render(node as ReactElement);
return rendered.text();
}

0 comments on commit 15dd131

Please sign in to comment.