From 926251bac750164b586e4ff3b18a889682d94531 Mon Sep 17 00:00:00 2001 From: "C.T. Lin" Date: Mon, 21 Sep 2015 16:29:22 +0800 Subject: [PATCH 1/2] add getRoutes with onEnter test case --- src/__tests__/reduxReactRouter-test.js | 33 ++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/__tests__/reduxReactRouter-test.js b/src/__tests__/reduxReactRouter-test.js index 1c4e3f3..0af1f10 100644 --- a/src/__tests__/reduxReactRouter-test.js +++ b/src/__tests__/reduxReactRouter-test.js @@ -162,6 +162,39 @@ describe('reduxRouter()', () => { expect(store.getState().router.location.pathname) .to.equal('/parent/child/123'); }); + + it('works with onEnter', () => { + const reducer = combineReducers({ + router: routerStateReducer + }); + + let store; + const history = createHistory(); + + reduxReactRouter({ + history, + getRoutes: s => { + store = s; + function requireAuth(nextState, redirectTo) { + if (!s.getState().user) { + redirectTo(null, '/login'); + } + } + return ( + + + + + + + ); + } + })(createStore)(reducer); + + store.dispatch(pushState(null, '/parent/child/123', { key: 'value' })); + expect(store.getState().router.location.pathname) + .to.equal('/login'); + }); }); describe('onEnter hook', () => { From bdf2e0725485d7ece7fd98d0c4284c7653618627 Mon Sep 17 00:00:00 2001 From: "C.T. Lin" Date: Mon, 21 Sep 2015 16:47:25 +0800 Subject: [PATCH 2/2] add a failed test with getState --- src/__tests__/reduxReactRouter-test.js | 35 +++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/__tests__/reduxReactRouter-test.js b/src/__tests__/reduxReactRouter-test.js index 0af1f10..92a1444 100644 --- a/src/__tests__/reduxReactRouter-test.js +++ b/src/__tests__/reduxReactRouter-test.js @@ -8,7 +8,7 @@ import { import { createStore, combineReducers } from 'redux'; import React from 'react'; -import { Route } from 'react-router'; +import { Route, Redirect } from 'react-router'; import createHistory from 'history/lib/createMemoryHistory'; import sinon from 'sinon'; @@ -195,6 +195,39 @@ describe('reduxRouter()', () => { expect(store.getState().router.location.pathname) .to.equal('/login'); }); + + it('works with onEnter and Redirect', () => { + const reducer = combineReducers({ + router: routerStateReducer + }); + + let store; + const history = createHistory(); + + reduxReactRouter({ + history, + getRoutes: s => { + store = s; + function notNeedAuth(nextState, redirectTo) { + if (s.getState().user) { + redirectTo(null, '/parent/child/123'); + } + } + return ( + + + + + + + + ); + } + })(createStore)(reducer, { user: 'test_user' }); + + expect(store.getState().router.location.pathname) + .to.equal('/login'); + }); }); describe('onEnter hook', () => {