Skip to content

Commit

Permalink
add test for collapseToast
Browse files Browse the repository at this point in the history
  • Loading branch information
fkhadra committed May 9, 2020
1 parent 35e079e commit a6b2e9c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
10 changes: 6 additions & 4 deletions test/__mocks__/react-transition-group.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { isFn } from '../../src/utils';

jest.mock('react-transition-group', () => {
const FakeTransition = jest.fn(
({ children, onEnter, onEntered, onExit, nodeRef, in: isIn }) => {
if (onEnter) isFn(onEnter) && isIn && onEnter(nodeRef);
if (onEntered) isFn(onEntered) && isIn && onEntered(nodeRef);
if (onExit) isFn(onExit) && !isIn && onExit(nodeRef);
({ children, onEnter, onEntered, onExit, in: isIn }) => {
if (onEnter) isFn(onEnter) && isIn && onEnter();
if (onEntered) isFn(onEntered) && isIn && onEntered();
if (onExit) {
isFn(onExit) && !isIn && onExit();
}

return isIn ? <div>{children}</div> : null;
}
Expand Down
33 changes: 32 additions & 1 deletion test/core/toast.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import '../__mocks__/react-transition-group';
import { cssClasses } from '../helpers';
import { eventManager, toast, Event } from '../../src/core';
import { ContainerInstance } from '../../src/hooks';
import { DEFAULT } from '../../src/utils';
import { DEFAULT, cssTransition } from '../../src/utils';
import { Id } from '../../src/types';
import { ToastContainer } from '../../src/components';

Expand Down Expand Up @@ -306,6 +306,10 @@ describe('toastify', () => {
expect(queryByText('hello')).toBe(null);
});

/**
* This test trigger a warning but it's a false positif
* Warning: Encountered two children with the same key, `.$toast-1`. ...
*/
it('Should be able to update the toastId', () => {
const { queryByText } = render(<ToastContainer />);
const toastId = 'bar';
Expand Down Expand Up @@ -433,13 +437,15 @@ describe('toastify', () => {
toast.warning('warning');
toast.info('info');
toast.warn('warn');
toast.dark("dark");
jest.runAllTimers();
});
expect(queryByText('default')).not.toBe(null);
expect(queryByText('success')).not.toBe(null);
expect(queryByText('error')).not.toBe(null);
expect(queryByText('warning')).not.toBe(null);
expect(queryByText('info')).not.toBe(null);
expect(queryByText('dark')).not.toBe(null);
expect(queryByText('warn')).not.toBe(null);
});

Expand Down Expand Up @@ -477,4 +483,29 @@ describe('toastify', () => {
expect(container.innerHTML).toMatch(/transform:(\s)?scaleX\(1\)/);
});
});

it("should remove toast even when not collapsing on exit", () => {
const Transition = cssTransition({
enter: 'foo',
exit: 'bar',
duration: [300, 500],
collapse: false
});

const { queryByText } = render(<ToastContainer transition={Transition}/>);
let id: Id;
act(() => {
id = toast('hello');
jest.runAllTimers();
});

expect(queryByText('hello')).not.toBe(null);

act(() => {
toast.dismiss(id);
jest.runAllTimers();
});

expect(queryByText('hello')).toBe(null);
})
});
31 changes: 31 additions & 0 deletions test/utils/collapseToast.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { collapseToast } from '../../src/utils';

beforeEach(() => {
// @ts-ignore
jest.spyOn(window, 'requestAnimationFrame').mockImplementation(cb => cb());
});

afterEach(() => {
// @ts-ignore
window.requestAnimationFrame.mockRestore();
});

describe('collapseToast function', () => {
it('Should handle collapse function', () => {
const done = jest.fn();
const node = document.createElement('div');
const addEvent = jest.fn((_, fn) => {
fn();
});
const removeEvent = jest.fn();

node.addEventListener = addEvent;
node.removeEventListener = removeEvent;

collapseToast(node, done);

expect(addEvent).toHaveBeenCalled();
expect(removeEvent).toHaveBeenCalled();
expect(done).toHaveBeenCalled();
});
});

0 comments on commit a6b2e9c

Please sign in to comment.