File tree Expand file tree Collapse file tree 5 files changed +70
-7
lines changed Expand file tree Collapse file tree 5 files changed +70
-7
lines changed Original file line number Diff line number Diff line change @@ -140,6 +140,21 @@ describe('<ReduxRouter>', () => {
140
140
} ) ) ;
141
141
} ) ;
142
142
143
+ it ( 'throws if routes are not passed to store enhancer' , ( ) => {
144
+ const reducer = combineReducers ( {
145
+ router : routerStateReducer
146
+ } ) ;
147
+
148
+ expect ( ( ) => server . reduxReactRouter ( ) ( createStore ) ( reducer ) )
149
+ . to . throw (
150
+ 'When rendering on the server, routes must be passed to the '
151
+ + 'reduxReactRouter() store enhancer; routes as a prop or as children '
152
+ + 'of <ReduxRouter> is not supported. To deal with circular '
153
+ + 'dependencies between routes and the store, use the '
154
+ + 'option getRoutes(store).'
155
+ ) ;
156
+ } ) ;
157
+
143
158
it ( 'handles redirects' , ( ) => {
144
159
const reducer = combineReducers ( {
145
160
router : routerStateReducer
Original file line number Diff line number Diff line change @@ -140,6 +140,29 @@ describe('reduxRouter()', () => {
140
140
expect ( store . getState ( ) . string ) . to . equal ( 'Unidirectional' ) ;
141
141
} ) ;
142
142
143
+ describe ( 'getRoutes()' , ( ) => {
144
+ it ( 'is passed dispatch and getState' , ( ) => {
145
+ const reducer = combineReducers ( {
146
+ router : routerStateReducer
147
+ } ) ;
148
+
149
+ let store ;
150
+ const history = createHistory ( ) ;
151
+
152
+ reduxReactRouter ( {
153
+ history,
154
+ getRoutes : s => {
155
+ store = s ;
156
+ return routes ;
157
+ }
158
+ } ) ( createStore ) ( reducer ) ;
159
+
160
+ store . dispatch ( pushState ( null , '/parent/child/123' , { key : 'value' } ) ) ;
161
+ expect ( store . getState ( ) . router . location . pathname )
162
+ . to . equal ( '/parent/child/123' ) ;
163
+ } ) ;
164
+ } ) ;
165
+
143
166
describe ( 'onEnter hook' , ( ) => {
144
167
it ( 'can perform redirects' , ( ) => {
145
168
const reducer = combineReducers ( {
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ import reduxReactRouter from './reduxReactRouter';
5
5
import useDefaults from './useDefaults' ;
6
6
import routeReplacement from './routeReplacement' ;
7
7
8
- function client ( next ) {
8
+ function historySynchronization ( next ) {
9
9
return options => createStore => ( reducer , initialState ) => {
10
10
const { onError, routerStateSelector } = options ;
11
11
const store = next ( options ) ( createStore ) ( reducer , initialState ) ;
@@ -47,5 +47,5 @@ function client(next) {
47
47
export default compose (
48
48
useDefaults ,
49
49
routeReplacement ,
50
- client
50
+ historySynchronization
51
51
) ( reduxReactRouter ) ;
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ export default function routeReplacement(next) {
6
6
return options => createStore => ( reducer , initialState ) => {
7
7
const {
8
8
routes : baseRoutes ,
9
+ getRoutes,
9
10
routerStateSelector
10
11
} = options ;
11
12
@@ -30,9 +31,16 @@ export default function routeReplacement(next) {
30
31
}
31
32
}
32
33
33
- const routes = baseRoutes
34
- ? baseRoutes
35
- : [ {
34
+ let routes ;
35
+ if ( baseRoutes ) {
36
+ routes = baseRoutes ;
37
+ } else if ( getRoutes ) {
38
+ routes = getRoutes ( {
39
+ dispatch : action => store . dispatch ( action ) ,
40
+ getState : ( ) => store . getState ( )
41
+ } ) ;
42
+ } else {
43
+ routes = [ {
36
44
getChildRoutes : ( location , cb ) => {
37
45
if ( ! areChildRoutesResolved ) {
38
46
childRoutesCallbacks . push ( cb ) ;
@@ -42,6 +50,7 @@ export default function routeReplacement(next) {
42
50
cb ( null , childRoutes ) ;
43
51
}
44
52
} ] ;
53
+ }
45
54
46
55
store = compose (
47
56
applyMiddleware (
Original file line number Diff line number Diff line change @@ -6,7 +6,22 @@ import routeReplacement from './routeReplacement';
6
6
import matchMiddleware from './matchMiddleware' ;
7
7
import { MATCH } from './constants' ;
8
8
9
- function server ( next ) {
9
+ function serverInvariants ( next ) {
10
+ return options => createStore => {
11
+ if ( ! options || ! ( options . routes || options . getRoutes ) ) {
12
+ throw new Error (
13
+ 'When rendering on the server, routes must be passed to the '
14
+ + 'reduxReactRouter() store enhancer; routes as a prop or as children of '
15
+ + '<ReduxRouter> is not supported. To deal with circular dependencies '
16
+ + 'between routes and the store, use the option getRoutes(store).'
17
+ ) ;
18
+ }
19
+
20
+ return next ( options ) ( createStore ) ;
21
+ } ;
22
+ }
23
+
24
+ function matching ( next ) {
10
25
return options => createStore => ( reducer , initialState ) => {
11
26
const store = compose (
12
27
applyMiddleware (
@@ -32,7 +47,8 @@ export function match(url, callback) {
32
47
}
33
48
34
49
export const reduxReactRouter = compose (
50
+ serverInvariants ,
35
51
useDefaults ,
36
52
routeReplacement ,
37
- server
53
+ matching
38
54
) ( baseReduxReactRouter ) ;
You can’t perform that action at this time.
0 commit comments