Skip to content

Commit

Permalink
Resolve issue #4.
Browse files Browse the repository at this point in the history
If there is no container, toast will be enqueue and dispatched as soon as an
container is available
  • Loading branch information
fkhadra committed May 28, 2017
1 parent ee218df commit 18c1338
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 6 deletions.
2 changes: 1 addition & 1 deletion dist/ReactToastify.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/ReactToastify.min.js.map

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions src/ToastContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ class ToastContainer extends Component {
}

componentDidMount() {
EventManager.on(config.ACTION.SHOW,
EventManager
.on(config.ACTION.SHOW,
(content, options) => this.show(content, options))
.on(config.ACTION.CLEAR, () => this.clear());
.on(config.ACTION.CLEAR, () => this.clear())
.emit(config.ACTION.MOUNTED);
}

componentWillUnmount() {
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/ToastContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ describe('ToastContainer', () => {
// Create a toast
toastify('coucou');
const props = component.children().find(Toast).props();

[
'autoClose',
'closeButton',
Expand Down
21 changes: 21 additions & 0 deletions src/__tests__/toastify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* eslint-env jest */

import React from 'react';
import { mount } from 'enzyme';

import ToastContainer from './../ToastContainer';
import toastify from './../toastify';
import EventManager from './../util/EventManager';
import config from './../config';

describe('toastify', () => {
it("Should emit notification only if a container is mounted", () => {
const spy = jest.fn();
EventManager.on(config.ACTION.SHOW, spy);
toastify('hello');
expect(spy).not.toHaveBeenCalled();

mount(<ToastContainer />);
expect(spy).toHaveBeenCalled();
});
});
3 changes: 2 additions & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default {
},
ACTION: {
SHOW: 'SHOW_TOAST',
CLEAR: 'CLEAR_TOAST'
CLEAR: 'CLEAR_TOAST',
MOUNTED: 'CONTAINER_MOUNTED'
}
};
19 changes: 18 additions & 1 deletion src/toastify.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,24 @@ function mergeOptions(options) {
return Object.assign({}, defaultOptions, options);
}

const emitEvent = (content, options) => EventManager.emit(ACTION.SHOW, content, options);
let isContainerMounted = false;
let queue = [];

EventManager.on(ACTION.MOUNTED, () => {
isContainerMounted = true;
queue.forEach(item => {
EventManager.emit(item.action, item.content, item.options);
});
queue = [];
});

const emitEvent = (content, options) => {
if (isContainerMounted) {
EventManager.emit(ACTION.SHOW, content, options);
} else {
queue.push({ action: ACTION.SHOW, content, options });
}
};

export default Object.assign(
(content, options) => emitEvent(content, mergeOptions(options)),
Expand Down

0 comments on commit 18c1338

Please sign in to comment.