Skip to content

Commit e4ef9c6

Browse files
committedJun 28, 2017
Remove a Switcher's listeners when using SwitcherProvider
1 parent e3f1c43 commit e4ef9c6

File tree

3 files changed

+78
-4
lines changed

3 files changed

+78
-4
lines changed
 

‎.travis.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ cache:
55
directories:
66
- node_modules
77
node_js:
8-
- "5.7"
98
- "6.2"
9+
- "7.8"
10+
- "8.0"
1011
env:
1112
matrix:
1213
- TEST_SUITE=lint

‎src/Switcher.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,33 @@ export default class Switcher extends Component {
105105
}
106106

107107
componentWillUnmount() {
108+
const usingProvider = this.context.switcherProvider;
108109
if (this.props.load) {
109-
window.removeEventListener('load', this.handleRouteChange);
110+
if (usingProvider) {
111+
this.context.switcherProvider.loadListeners = this.context.switcherProvider.loadListeners.filter(
112+
({ id }) => id !== this._id
113+
);
114+
} else {
115+
window.removeEventListener('load', this.handleRouteChange);
116+
}
110117
}
111118
if (this.props.pushState) {
112-
window.removeEventListener('popstate', this.handleRouteChange);
119+
if (usingProvider) {
120+
this.context.switcherProvider.popStateListeners = this.context.switcherProvider.popStateListeners.filter(
121+
({ id }) => id !== this._id
122+
);
123+
} else {
124+
window.removeEventListener('popstate', this.handleRouteChange);
125+
}
113126
}
114127
if (this.props.hashChange) {
115-
window.removeEventListener('hashchange', this.handleRouteChange);
128+
if (usingProvider) {
129+
this.context.switcherProvider.hashChangeListeners = this.context.switcherProvider.hashChangeListeners.filter(
130+
({ id }) => id !== this._id
131+
);
132+
} else {
133+
window.removeEventListener('hashchange', this.handleRouteChange);
134+
}
116135
}
117136
}
118137

‎test/SwitcherProvider_test.js

+54
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,62 @@ describe('SwitcherProvider', () => {
2424
expect(component.text()).toEqual('Another ChildHomeHome');
2525
helpers.currentPath.restore();
2626
});
27+
28+
it('removes event listeners when a child Switcher is unmounted', () => {
29+
sinon.stub(helpers, 'currentPath').returns('/');
30+
const component = renderNested();
31+
const innerSwitcher = component.find('Switcher').last();
32+
const listenerId = innerSwitcher.node._id;
33+
const instance = component.instance();
34+
35+
expect(instance.switcherProvider.loadListeners.length).toEqual(2);
36+
expect(instance.switcherProvider.hashChangeListeners.length).toEqual(2);
37+
expect(
38+
instance.switcherProvider.loadListeners
39+
.map(({ id }) => id)
40+
.indexOf(listenerId)
41+
).not.toEqual(-1);
42+
expect(
43+
instance.switcherProvider.hashChangeListeners
44+
.map(({ id }) => id)
45+
.indexOf(listenerId)
46+
).not.toEqual(-1);
47+
helpers.currentPath.restore();
48+
49+
sinon.stub(helpers, 'currentPath').returns('/hello');
50+
component.update();
51+
expect(instance.switcherProvider.loadListeners.length).toEqual(1);
52+
expect(instance.switcherProvider.hashChangeListeners.length).toEqual(1);
53+
expect(
54+
instance.switcherProvider.loadListeners
55+
.map(({ id }) => id)
56+
.indexOf(listenerId)
57+
).toEqual(-1);
58+
expect(
59+
instance.switcherProvider.hashChangeListeners
60+
.map(({ id }) => id)
61+
.indexOf(listenerId)
62+
).toEqual(-1);
63+
helpers.currentPath.restore();
64+
});
2765
});
2866

67+
function renderNested() {
68+
return mount(
69+
<SwitcherProvider>
70+
<div>
71+
<Switcher>
72+
<Switcher path="/">
73+
<div path={['/', '/other']}>Home</div>
74+
<div path="/second">Second</div>
75+
</Switcher>
76+
<div path="/hello">Hello</div>
77+
</Switcher>
78+
</div>
79+
</SwitcherProvider>
80+
);
81+
}
82+
2983
function renderComponent() {
3084
return mount(
3185
<SwitcherProvider>

0 commit comments

Comments
 (0)
Please sign in to comment.