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

Commit

Permalink
Unsubscribe after each test to silence warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
ellbee committed Nov 30, 2015
1 parent 43b7d41 commit 6c02fd7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 454 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"test:node": "mocha --compilers js:babel-core/register --recursive ./test/node",
"test:browser": "karma start",
"test:cov": "npm run test:cov:browser && npm run test:cov:node && npm run test:cov:report",
"test:cov:node": "babel-node $(npm bin)/isparta cover $(npm bin)/_mocha report --dir ./coverage/node-coverage -- --recursive",
"test:cov:node": "babel-node $(npm bin)/isparta cover $(npm bin)/_mocha report --dir ./coverage/node-coverage -- --recursive ./test/node",
"test:cov:browser": "COVERAGE=true karma start",
"test:cov:report": "$(npm bin)/istanbul report --dir ./coverage --include **/*coverage.json html text",
"prepublish": "npm run build"
Expand Down
37 changes: 25 additions & 12 deletions test/createTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ function createSyncedHistoryAndStore(createHistory) {
routing: routeReducer
}));
const history = createHistory();
syncReduxAndRouter(history, store);
return { history, store };
const unsubscribe = syncReduxAndRouter(history, store);
return { history, store, unsubscribe };
}

const defaultReset = () => {};
Expand Down Expand Up @@ -115,39 +115,57 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
});

describe('syncReduxAndRouter', () => {
let history, store, unsubscribe;

beforeEach(() => {
let synced = createSyncedHistoryAndStore(createHistory);
history = synced.history;
store = synced.store;
unsubscribe = synced.unsubscribe;
});

afterEach(() => {
unsubscribe();
});

it('syncs router -> redux', () => {
const { history, store } = createSyncedHistoryAndStore(createHistory);
expect(store.getState().routing.path).toEqual('/');

history.pushState(null, '/foo');
expect(store.getState().routing.path).toEqual('/foo');
expect(store.getState().routing.state).toBe(null);
expect(store.getState().routing.replace).toBe(false);

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

history.replaceState(null, '/bar');
expect(store.getState().routing.path).toEqual('/bar');
expect(store.getState().routing.state).toBe(null);
expect(store.getState().routing.replace).toBe(true);

history.pushState(null, '/bar');
expect(store.getState().routing.path).toEqual('/bar');
expect(store.getState().routing.state).toBe(null);
expect(store.getState().routing.replace).toBe(true);

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

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

history.pushState(null, '/bar?query=1#hash=2');
history.pushState({ bar: 'baz' }, '/bar?query=1#hash=2');
expect(store.getState().routing.path).toEqual('/bar?query=1#hash=2');
expect(store.getState().routing.replace).toBe(true);
});

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

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

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

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

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

const unsubscribe = store.subscribe(() => {
const unsubscribeFromStore = store.subscribe(() => {
updates.push(store.getState())
});

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

unsubscribe();
unsubscribeFromStore();

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

it('allows updating the route from within `listenBefore`', () => {
const { history, store } = createSyncedHistoryAndStore(createHistory);
expect(store.getState().routing).toEqual({
path: '/',
changeId: 1,
Expand Down
Loading

0 comments on commit 6c02fd7

Please sign in to comment.