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" } diff --git a/test/index.js b/test/index.js index 2aa051c..1981f2f 100644 --- a/test/index.js +++ b/test/index.js @@ -184,4 +184,53 @@ 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/); + }); + + 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'); + }); + + 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(); + }); });