Skip to content
This repository was archived by the owner on Oct 26, 2018. It is now read-only.

Commit 21adf55

Browse files
committed
Unsubscribe after each test to silence warnings
1 parent 10ba2e9 commit 21adf55

File tree

3 files changed

+80
-500
lines changed

3 files changed

+80
-500
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"test:node": "mocha --compilers js:babel-core/register --recursive ./test/node",
1818
"test:browser": "karma start",
1919
"test:cov": "npm run test:cov:browser && npm run test:cov:node && npm run test:cov:report",
20-
"test:cov:node": "babel-node $(npm bin)/isparta cover $(npm bin)/_mocha report --dir ./coverage/node-coverage -- --recursive",
20+
"test:cov:node": "babel-node $(npm bin)/isparta cover $(npm bin)/_mocha report --dir ./coverage/node-coverage -- --recursive ./test/node",
2121
"test:cov:browser": "COVERAGE=true karma start",
2222
"test:cov:report": "$(npm bin)/istanbul report --dir ./coverage --include **/*coverage.json html text",
2323
"prepublish": "npm run build"

test/createTests.js

+79-42
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ function createSyncedHistoryAndStore(createHistory) {
77
routing: routeReducer
88
}));
99
const history = createHistory();
10-
syncReduxAndRouter(history, store);
11-
return { history, store };
10+
const unsubscribe = syncReduxAndRouter(history, store);
11+
return { history, store, unsubscribe };
1212
}
1313

1414
const defaultReset = () => {};
@@ -22,18 +22,22 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
2222
it('creates actions', () => {
2323
expect(pushPath('/foo', { bar: 'baz' })).toEqual({
2424
type: UPDATE_PATH,
25-
path: '/foo',
26-
replace: false,
27-
state: { bar: 'baz' },
28-
avoidRouterUpdate: false
25+
payload: {
26+
path: '/foo',
27+
replace: false,
28+
state: { bar: 'baz' },
29+
avoidRouterUpdate: false
30+
}
2931
});
3032

3133
expect(pushPath('/foo', undefined, { avoidRouterUpdate: true })).toEqual({
3234
type: UPDATE_PATH,
33-
path: '/foo',
34-
state: undefined,
35-
replace: false,
36-
avoidRouterUpdate: true
35+
payload: {
36+
path: '/foo',
37+
state: undefined,
38+
replace: false,
39+
avoidRouterUpdate: true
40+
}
3741
});
3842
});
3943
});
@@ -42,26 +46,32 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
4246
it('creates actions', () => {
4347
expect(replacePath('/foo', { bar: 'baz' })).toEqual({
4448
type: UPDATE_PATH,
45-
path: '/foo',
46-
replace: true,
47-
state: { bar: 'baz' },
48-
avoidRouterUpdate: false
49+
payload: {
50+
path: '/foo',
51+
replace: true,
52+
state: { bar: 'baz' },
53+
avoidRouterUpdate: false
54+
}
4955
});
5056

5157
expect(replacePath('/foo', undefined, { avoidRouterUpdate: true })).toEqual({
5258
type: UPDATE_PATH,
53-
path: '/foo',
54-
state: undefined,
55-
replace: true,
56-
avoidRouterUpdate: true
59+
payload: {
60+
path: '/foo',
61+
state: undefined,
62+
replace: true,
63+
avoidRouterUpdate: true
64+
}
5765
});
5866

5967
expect(replacePath('/foo', undefined, { avoidRouterUpdate: false })).toEqual({
6068
type: UPDATE_PATH,
61-
path: '/foo',
62-
state: undefined,
63-
replace: true,
64-
avoidRouterUpdate: false
69+
payload: {
70+
path: '/foo',
71+
state: undefined,
72+
replace: true,
73+
avoidRouterUpdate: false
74+
}
6575
});
6676
});
6777
});
@@ -75,8 +85,10 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
7585
it('updates the path', () => {
7686
expect(routeReducer(state, {
7787
type: UPDATE_PATH,
78-
path: '/bar',
79-
replace: false
88+
payload: {
89+
path: '/bar',
90+
replace: false
91+
}
8092
})).toEqual({
8193
path: '/bar',
8294
replace: false,
@@ -88,9 +100,11 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
88100
it('respects replace', () => {
89101
expect(routeReducer(state, {
90102
type: UPDATE_PATH,
91-
path: '/bar',
92-
replace: true,
93-
avoidRouterUpdate: false
103+
payload: {
104+
path: '/bar',
105+
replace: true,
106+
avoidRouterUpdate: false
107+
}
94108
})).toEqual({
95109
path: '/bar',
96110
replace: true,
@@ -102,9 +116,11 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
102116
it('respects `avoidRouterUpdate` flag', () => {
103117
expect(routeReducer(state, {
104118
type: UPDATE_PATH,
105-
path: '/bar',
106-
replace: false,
107-
avoidRouterUpdate: true
119+
payload: {
120+
path: '/bar',
121+
replace: false,
122+
avoidRouterUpdate: true
123+
}
108124
})).toEqual({
109125
path: '/bar',
110126
replace: false,
@@ -115,39 +131,57 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
115131
});
116132

117133
describe('syncReduxAndRouter', () => {
134+
let history, store, unsubscribe;
135+
136+
beforeEach(() => {
137+
let synced = createSyncedHistoryAndStore(createHistory);
138+
history = synced.history;
139+
store = synced.store;
140+
unsubscribe = synced.unsubscribe;
141+
});
142+
143+
afterEach(() => {
144+
unsubscribe();
145+
});
146+
118147
it('syncs router -> redux', () => {
119-
const { history, store } = createSyncedHistoryAndStore(createHistory);
120148
expect(store.getState().routing.path).toEqual('/');
121149

122150
history.pushState(null, '/foo');
123151
expect(store.getState().routing.path).toEqual('/foo');
124152
expect(store.getState().routing.state).toBe(null);
153+
expect(store.getState().routing.replace).toBe(false);
125154

126155
history.pushState({ bar: 'baz' }, '/foo');
127156
expect(store.getState().routing.path).toEqual('/foo');
128157
expect(store.getState().routing.state).toEqual({ bar: 'baz' });
158+
expect(store.getState().routing.replace).toBe(true);
129159

130160
history.replaceState(null, '/bar');
131161
expect(store.getState().routing.path).toEqual('/bar');
132162
expect(store.getState().routing.state).toBe(null);
163+
expect(store.getState().routing.replace).toBe(true);
133164

134165
history.pushState(null, '/bar');
135166
expect(store.getState().routing.path).toEqual('/bar');
136167
expect(store.getState().routing.state).toBe(null);
168+
expect(store.getState().routing.replace).toBe(true);
137169

138170
history.pushState(null, '/bar?query=1');
139171
expect(store.getState().routing.path).toEqual('/bar?query=1');
172+
expect(store.getState().routing.replace).toBe(false);
140173

141174
history.replaceState({ bar: 'baz' }, '/bar?query=1');
142175
expect(store.getState().routing.path).toEqual('/bar?query=1');
143176
expect(store.getState().routing.state).toEqual({ bar: 'baz' });
177+
expect(store.getState().routing.replace).toBe(true);
144178

145-
history.pushState(null, '/bar?query=1#hash=2');
179+
history.pushState({ bar: 'baz' }, '/bar?query=1#hash=2');
146180
expect(store.getState().routing.path).toEqual('/bar?query=1#hash=2');
181+
expect(store.getState().routing.replace).toBe(true);
147182
});
148183

149184
it('syncs redux -> router', () => {
150-
const { history, store } = createSyncedHistoryAndStore(createHistory);
151185
expect(store.getState().routing).toEqual({
152186
path: '/',
153187
changeId: 1,
@@ -205,7 +239,6 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
205239
});
206240

207241
it('updates the router even if path is the same', () => {
208-
const { history, store } = createSyncedHistoryAndStore(createHistory);
209242
expect(store.getState().routing).toEqual({
210243
path: '/',
211244
changeId: 1,
@@ -239,10 +272,13 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
239272
});
240273

241274
it('does not update the router for other state changes', () => {
242-
const { history, store } = createSyncedHistoryAndStore(createHistory);
243275
store.dispatch({
244276
type: 'RANDOM_ACTION',
245-
value: 5
277+
payload: {
278+
payload: {
279+
value: 5
280+
}
281+
}
246282
});
247283

248284
expect(store.getState().routing).toEqual({
@@ -254,7 +290,6 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
254290
});
255291

256292
it('only updates the router once when dispatching from `listenBefore`', () => {
257-
const { history, store } = createSyncedHistoryAndStore(createHistory);
258293
expect(store.getState().routing).toEqual({
259294
path: '/',
260295
changeId: 1,
@@ -266,7 +301,11 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
266301
expect(location.pathname).toEqual('/foo');
267302
store.dispatch({
268303
type: 'RANDOM_ACTION',
269-
value: 5
304+
payload: {
305+
payload: {
306+
value: 5
307+
}
308+
}
270309
});
271310
});
272311

@@ -280,10 +319,9 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
280319
});
281320

282321
it('does not unnecessarily update the store', () => {
283-
const { history, store } = createSyncedHistoryAndStore(createHistory);
284322
const updates = [];
285323

286-
const unsubscribe = store.subscribe(() => {
324+
const unsubscribeFromStore = store.subscribe(() => {
287325
updates.push(store.getState())
288326
});
289327

@@ -293,7 +331,7 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
293331
store.dispatch(replacePath('/bar'));
294332
store.dispatch(replacePath('/bar', { bar: 'foo' }));
295333

296-
unsubscribe();
334+
unsubscribeFromStore();
297335

298336
expect(updates.length).toBe(5);
299337
expect(updates).toEqual([
@@ -341,7 +379,6 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
341379
});
342380

343381
it('allows updating the route from within `listenBefore`', () => {
344-
const { history, store } = createSyncedHistoryAndStore(createHistory);
345382
expect(store.getState().routing).toEqual({
346383
path: '/',
347384
changeId: 1,

0 commit comments

Comments
 (0)