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

Commit 6c02fd7

Browse files
committed
Unsubscribe after each test to silence warnings
1 parent 43b7d41 commit 6c02fd7

File tree

3 files changed

+26
-454
lines changed

3 files changed

+26
-454
lines changed

package.json

Lines changed: 1 addition & 1 deletion
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

Lines changed: 25 additions & 12 deletions
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 = () => {};
@@ -115,39 +115,57 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
115115
});
116116

117117
describe('syncReduxAndRouter', () => {
118+
let history, store, unsubscribe;
119+
120+
beforeEach(() => {
121+
let synced = createSyncedHistoryAndStore(createHistory);
122+
history = synced.history;
123+
store = synced.store;
124+
unsubscribe = synced.unsubscribe;
125+
});
126+
127+
afterEach(() => {
128+
unsubscribe();
129+
});
130+
118131
it('syncs router -> redux', () => {
119-
const { history, store } = createSyncedHistoryAndStore(createHistory);
120132
expect(store.getState().routing.path).toEqual('/');
121133

122134
history.pushState(null, '/foo');
123135
expect(store.getState().routing.path).toEqual('/foo');
124136
expect(store.getState().routing.state).toBe(null);
137+
expect(store.getState().routing.replace).toBe(false);
125138

126139
history.pushState({ bar: 'baz' }, '/foo');
127140
expect(store.getState().routing.path).toEqual('/foo');
128141
expect(store.getState().routing.state).toEqual({ bar: 'baz' });
142+
expect(store.getState().routing.replace).toBe(true);
129143

130144
history.replaceState(null, '/bar');
131145
expect(store.getState().routing.path).toEqual('/bar');
132146
expect(store.getState().routing.state).toBe(null);
147+
expect(store.getState().routing.replace).toBe(true);
133148

134149
history.pushState(null, '/bar');
135150
expect(store.getState().routing.path).toEqual('/bar');
136151
expect(store.getState().routing.state).toBe(null);
152+
expect(store.getState().routing.replace).toBe(true);
137153

138154
history.pushState(null, '/bar?query=1');
139155
expect(store.getState().routing.path).toEqual('/bar?query=1');
156+
expect(store.getState().routing.replace).toBe(false);
140157

141158
history.replaceState({ bar: 'baz' }, '/bar?query=1');
142159
expect(store.getState().routing.path).toEqual('/bar?query=1');
143160
expect(store.getState().routing.state).toEqual({ bar: 'baz' });
161+
expect(store.getState().routing.replace).toBe(true);
144162

145-
history.pushState(null, '/bar?query=1#hash=2');
163+
history.pushState({ bar: 'baz' }, '/bar?query=1#hash=2');
146164
expect(store.getState().routing.path).toEqual('/bar?query=1#hash=2');
165+
expect(store.getState().routing.replace).toBe(true);
147166
});
148167

149168
it('syncs redux -> router', () => {
150-
const { history, store } = createSyncedHistoryAndStore(createHistory);
151169
expect(store.getState().routing).toEqual({
152170
path: '/',
153171
changeId: 1,
@@ -205,7 +223,6 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
205223
});
206224

207225
it('updates the router even if path is the same', () => {
208-
const { history, store } = createSyncedHistoryAndStore(createHistory);
209226
expect(store.getState().routing).toEqual({
210227
path: '/',
211228
changeId: 1,
@@ -239,7 +256,6 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
239256
});
240257

241258
it('does not update the router for other state changes', () => {
242-
const { history, store } = createSyncedHistoryAndStore(createHistory);
243259
store.dispatch({
244260
type: 'RANDOM_ACTION',
245261
value: 5
@@ -254,7 +270,6 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
254270
});
255271

256272
it('only updates the router once when dispatching from `listenBefore`', () => {
257-
const { history, store } = createSyncedHistoryAndStore(createHistory);
258273
expect(store.getState().routing).toEqual({
259274
path: '/',
260275
changeId: 1,
@@ -280,10 +295,9 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
280295
});
281296

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

286-
const unsubscribe = store.subscribe(() => {
300+
const unsubscribeFromStore = store.subscribe(() => {
287301
updates.push(store.getState())
288302
});
289303

@@ -293,7 +307,7 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
293307
store.dispatch(replacePath('/bar'));
294308
store.dispatch(replacePath('/bar', { bar: 'foo' }));
295309

296-
unsubscribe();
310+
unsubscribeFromStore();
297311

298312
expect(updates.length).toBe(5);
299313
expect(updates).toEqual([
@@ -341,7 +355,6 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
341355
});
342356

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

0 commit comments

Comments
 (0)