Skip to content

Commit

Permalink
fix onClose being called two times
Browse files Browse the repository at this point in the history
  • Loading branch information
Sinewyk committed Jan 21, 2016
1 parent 8bb0ca1 commit b118d12
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/portal.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ export default class Portal extends React.Component {
document.removeEventListener('touchstart', this.handleOutsideMouseClick);
}

this.closePortal();
if (this.state.active) {
this.closePortal();
}
}

shouldComponentUpdate(nextProps, nextState) {
Expand Down
20 changes: 19 additions & 1 deletion test/portal_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import jsdom from 'jsdom';
import Portal from '../lib/portal';
import assert from 'assert';
import {spy} from 'sinon';
import {render, unmountComponentAtNode} from 'react-dom';
import {
mount,
spyLifecycle
Expand Down Expand Up @@ -110,7 +111,24 @@ describe('react-portal', () => {
const props = {isOpened: true, onClose: spy()};
const wrapper = mount(<Portal {...props}><p>Hi</p></Portal>);
wrapper.instance().closePortal();
assert(props.onClose.called);
assert(props.onClose.calledOnce);
});

it('should call props.onClose() only once when portal closes and then is unmounted', () => {
const div = document.createElement('div');
const props = {isOpened: true, onClose: spy()};
const component = render(<Portal {...props}><p>Hi</p></Portal>, div);
component.closePortal();
unmountComponentAtNode(div);
assert(props.onClose.calledOnce);
});

it('should call props.onClose() only once when directly unmounting', () => {
const div = document.createElement('div');
const props = {isOpened: true, onClose: spy()};
render(<Portal {...props}><p>Hi</p></Portal>, div);
unmountComponentAtNode(div);
assert(props.onClose.calledOnce);
});
});

Expand Down

0 comments on commit b118d12

Please sign in to comment.