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 Dec 2, 2015
1 parent 10ba2e9 commit 21adf55
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 500 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
121 changes: 79 additions & 42 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 All @@ -22,18 +22,22 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
it('creates actions', () => {
expect(pushPath('/foo', { bar: 'baz' })).toEqual({
type: UPDATE_PATH,
path: '/foo',
replace: false,
state: { bar: 'baz' },
avoidRouterUpdate: false
payload: {
path: '/foo',
replace: false,
state: { bar: 'baz' },
avoidRouterUpdate: false
}
});

expect(pushPath('/foo', undefined, { avoidRouterUpdate: true })).toEqual({
type: UPDATE_PATH,
path: '/foo',
state: undefined,
replace: false,
avoidRouterUpdate: true
payload: {
path: '/foo',
state: undefined,
replace: false,
avoidRouterUpdate: true
}
});
});
});
Expand All @@ -42,26 +46,32 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
it('creates actions', () => {
expect(replacePath('/foo', { bar: 'baz' })).toEqual({
type: UPDATE_PATH,
path: '/foo',
replace: true,
state: { bar: 'baz' },
avoidRouterUpdate: false
payload: {
path: '/foo',
replace: true,
state: { bar: 'baz' },
avoidRouterUpdate: false
}
});

expect(replacePath('/foo', undefined, { avoidRouterUpdate: true })).toEqual({
type: UPDATE_PATH,
path: '/foo',
state: undefined,
replace: true,
avoidRouterUpdate: true
payload: {
path: '/foo',
state: undefined,
replace: true,
avoidRouterUpdate: true
}
});

expect(replacePath('/foo', undefined, { avoidRouterUpdate: false })).toEqual({
type: UPDATE_PATH,
path: '/foo',
state: undefined,
replace: true,
avoidRouterUpdate: false
payload: {
path: '/foo',
state: undefined,
replace: true,
avoidRouterUpdate: false
}
});
});
});
Expand All @@ -75,8 +85,10 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
it('updates the path', () => {
expect(routeReducer(state, {
type: UPDATE_PATH,
path: '/bar',
replace: false
payload: {
path: '/bar',
replace: false
}
})).toEqual({
path: '/bar',
replace: false,
Expand All @@ -88,9 +100,11 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
it('respects replace', () => {
expect(routeReducer(state, {
type: UPDATE_PATH,
path: '/bar',
replace: true,
avoidRouterUpdate: false
payload: {
path: '/bar',
replace: true,
avoidRouterUpdate: false
}
})).toEqual({
path: '/bar',
replace: true,
Expand All @@ -102,9 +116,11 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
it('respects `avoidRouterUpdate` flag', () => {
expect(routeReducer(state, {
type: UPDATE_PATH,
path: '/bar',
replace: false,
avoidRouterUpdate: true
payload: {
path: '/bar',
replace: false,
avoidRouterUpdate: true
}
})).toEqual({
path: '/bar',
replace: false,
Expand All @@ -115,39 +131,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 +239,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,10 +272,13 @@ 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
payload: {
payload: {
value: 5
}
}
});

expect(store.getState().routing).toEqual({
Expand All @@ -254,7 +290,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 @@ -266,7 +301,11 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
expect(location.pathname).toEqual('/foo');
store.dispatch({
type: 'RANDOM_ACTION',
value: 5
payload: {
payload: {
value: 5
}
}
});
});

Expand All @@ -280,10 +319,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 +331,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 +379,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 21adf55

Please sign in to comment.