Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Popover] Fix a callback leak #5158

Merged
merged 1 commit into from
Sep 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/Popover/Popover.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ class Popover extends Component {
}

componentWillUnmount() {
this.handleResize.cancel();
this.handleScroll.cancel();

if (this.timeout) {
clearTimeout(this.timeout);
this.timeout = null;
Expand Down Expand Up @@ -206,10 +209,6 @@ class Popover extends Component {
this.requestClose('clickAway');
};

_resizeAutoPosition() {
this.setPlacement();
}

getAnchorPosition(el) {
if (!el) {
el = ReactDOM.findDOMNode(this);
Expand Down Expand Up @@ -247,8 +246,6 @@ class Popover extends Component {
return;
}

const anchorEl = this.props.anchorEl || this.anchorEl;

if (!this.refs.layer.getLayer()) {
return;
}
Expand All @@ -259,6 +256,7 @@ class Popover extends Component {
}

const {targetOrigin, anchorOrigin} = this.props;
const anchorEl = this.props.anchorEl || this.anchorEl;

const anchor = this.getAnchorPosition(anchorEl);
let target = this.getTargetPosition(targetEl);
Expand Down
18 changes: 17 additions & 1 deletion src/Popover/Popover.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import React from 'react';
import {assert} from 'chai';
import {shallow} from 'enzyme';
import {shallow, mount} from 'enzyme';
import Popover from './Popover';
import getMuiTheme from '../styles/getMuiTheme';

describe('<Popover />', () => {
const muiTheme = getMuiTheme();
const shallowWithContext = (node) => shallow(node, {context: {muiTheme}});
const mountWithContext = (node) => mount(node, {context: {muiTheme}});

describe('state: closing', () => {
it('should not create new timeout when popover is already closing', () => {
Expand All @@ -23,4 +24,19 @@ describe('<Popover />', () => {
assert.strictEqual(timeout, nextTimeout);
});
});

describe('unmounting', () => {
it('should stop listening correctly', (done) => {
const wrapper = mountWithContext(<Popover open={true} />);

wrapper.instance().handleScroll();
wrapper.instance().handleScroll();
wrapper.unmount();

setTimeout(() => {
// Wait for the end of the throttle. Makes sure we don't crash.
done();
}, 100);
});
});
});