From 21b851209cbe8833654d8da26ba38c8a1463e567 Mon Sep 17 00:00:00 2001 From: Lee Bannard Date: Tue, 24 Nov 2015 22:13:12 +0000 Subject: [PATCH 1/4] Add test coverage --- .gitignore | 3 ++- package.json | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index a9f4ed5..7c4bc80 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ lib -node_modules \ No newline at end of file +node_modules +coverage diff --git a/package.json b/package.json index 23876f7..6f99485 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "scripts": { "build": "mkdir -p lib && babel ./src/index.js --plugins transform-object-assign --out-file ./lib/index.js", "test": "mocha --compilers js:babel-core/register --recursive", + "test:cov": "babel-node $(npm bin)/isparta cover $(npm bin)/_mocha -- --recursive", "prepublish": "npm run build" }, "tags": [ @@ -32,6 +33,7 @@ "babel-preset-es2015": "^6.1.2", "expect": "^1.13.0", "history": "^1.13.1", + "isparta": "^4.0.0", "mocha": "^2.3.4", "redux": "^3.0.4" } From 5f01ab93385525499bc31b7dc383499b0c4406d8 Mon Sep 17 00:00:00 2001 From: Lee Bannard Date: Tue, 24 Nov 2015 22:19:51 +0000 Subject: [PATCH 2/4] Add test for misnamed routing store key --- test/index.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/index.js b/test/index.js index 2aa051c..b295570 100644 --- a/test/index.js +++ b/test/index.js @@ -184,4 +184,14 @@ describe('syncReduxAndRouter', () => { changeId: 3 }); }) + + it('throws if "routing" key is missing with default selectRouteState', () => { + const store = createStore(combineReducers({ + notRouting: routeReducer + })); + const history = createHistory(); + expect( + () => syncReduxAndRouter(history, store) + ).toThrow(/Cannot sync router: route state does not exist/); + }); }); From b585d4e81b29585a180126fe8c94c82e3015406d Mon Sep 17 00:00:00 2001 From: Lee Bannard Date: Tue, 24 Nov 2015 22:20:55 +0000 Subject: [PATCH 3/4] Add test for custom selectRouterState --- test/index.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/index.js b/test/index.js index b295570..b53792e 100644 --- a/test/index.js +++ b/test/index.js @@ -194,4 +194,14 @@ describe('syncReduxAndRouter', () => { () => syncReduxAndRouter(history, store) ).toThrow(/Cannot sync router: route state does not exist/); }); + + it('accepts custom selectRouterState', () => { + const store = createStore(combineReducers({ + notRouting: routeReducer + })); + const history = createHistory(); + syncReduxAndRouter(history, store, state => state.notRouting) + history.pushState(null, '/bar'); + expect(store.getState().notRouting.path).toEqual('/bar'); + }); }); From 0b5bbae3f1f25ef753c7137ae7e4c484439fd72d Mon Sep 17 00:00:00 2001 From: Lee Bannard Date: Tue, 24 Nov 2015 22:22:36 +0000 Subject: [PATCH 4/4] Add test for unsubscribe function --- test/index.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/test/index.js b/test/index.js index b53792e..1981f2f 100644 --- a/test/index.js +++ b/test/index.js @@ -204,4 +204,33 @@ describe('syncReduxAndRouter', () => { history.pushState(null, '/bar'); expect(store.getState().notRouting.path).toEqual('/bar'); }); + + it('returns unsubscribe to stop listening to history and store', () => { + const store = createStore(combineReducers({ + routing: routeReducer + })); + const history = createHistory(); + const unsubscribe = syncReduxAndRouter(history, store) + + history.pushState(null, '/foo'); + expect(store.getState().routing.path).toEqual('/foo'); + + store.dispatch(updatePath('/bar')); + expect(store.getState().routing).toEqual({ + path: '/bar', + changeId: 2 + }); + + unsubscribe(); + + history.pushState(null, '/foo'); + expect(store.getState().routing.path).toEqual('/bar'); + + history.listenBefore(location => { + throw new Error() + }); + expect( + () => store.dispatch(updatePath('/foo')) + ).toNotThrow(); + }); });