@@ -28,15 +28,18 @@ expect.extend({
28
28
}
29
29
} )
30
30
31
- function createSyncedHistoryAndStore ( createHistory ) {
31
+ function createSyncedHistoryAndStore ( createHistory , syncOptions , initialState ) {
32
32
const history = createHistory ( )
33
33
const middleware = syncHistory ( history )
34
- const { unsubscribe } = middleware
35
-
36
- const createStoreWithMiddleware = applyMiddleware ( middleware ) ( createStore )
37
- const store = createStoreWithMiddleware ( combineReducers ( {
34
+ const reducer = combineReducers ( {
38
35
routing : routeReducer
39
- } ) )
36
+ } )
37
+ const store = createStore (
38
+ reducer ,
39
+ initialState ,
40
+ applyMiddleware ( middleware )
41
+ )
42
+ const unsubscribe = middleware . syncWith ( store , syncOptions )
40
43
41
44
return { history, store, unsubscribe }
42
45
}
@@ -197,18 +200,17 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
197
200
history . push ( '/foo' )
198
201
199
202
const middleware = syncHistory ( history )
200
- unsubscribe = middleware . unsubscribe
201
-
202
- const finalCreateStore = compose (
203
+ store = createStore ( combineReducers ( {
204
+ routing : routeReducer
205
+ } ) , compose (
203
206
applyMiddleware ( middleware ) ,
204
207
instrument ( )
205
- ) ( createStore )
206
- store = finalCreateStore ( combineReducers ( {
207
- routing : routeReducer
208
- } ) )
208
+ ) )
209
209
devToolsStore = store . liftedStore
210
-
211
- middleware . listenForReplays ( store )
210
+ unsubscribe = middleware . syncWith ( store , {
211
+ stateToUrl : true ,
212
+ urlToState : true
213
+ } )
212
214
} )
213
215
214
216
afterEach ( ( ) => {
@@ -273,11 +275,103 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
273
275
} )
274
276
} )
275
277
278
+ describe ( 'initialState' , ( ) => {
279
+ it ( 'does not respect initialState when syncing url to state' , ( ) => {
280
+ let synced = createSyncedHistoryAndStore ( createHistory , {
281
+ urlToState : true
282
+ } , {
283
+ routing : {
284
+ location : {
285
+ pathname : '/init' ,
286
+ search : '' ,
287
+ hash : '' ,
288
+ state : null ,
289
+ action : 'PUSH' ,
290
+ key : 'abcde'
291
+ }
292
+ }
293
+ } )
294
+
295
+ let history = synced . history
296
+ let unsubscribe = synced . unsubscribe
297
+
298
+ let currentPath
299
+ const historyUnsubscribe = history . listen ( location => {
300
+ currentPath = location . pathname
301
+ } )
302
+
303
+ expect ( currentPath ) . toEqual ( '/' )
304
+ historyUnsubscribe ( )
305
+ unsubscribe ( )
306
+ } )
307
+
308
+ it ( 'respects initialState when syncing state to url' , ( ) => {
309
+ let synced = createSyncedHistoryAndStore ( createHistory , {
310
+ stateToUrl : true
311
+ } , {
312
+ routing : {
313
+ location : {
314
+ pathname : '/init' ,
315
+ search : '' ,
316
+ hash : '' ,
317
+ state : null ,
318
+ action : 'PUSH' ,
319
+ key : 'abcde'
320
+ }
321
+ }
322
+ } )
323
+
324
+ let history = synced . history
325
+ let unsubscribe = synced . unsubscribe
326
+
327
+ let currentPath
328
+ const historyUnsubscribe = history . listen ( location => {
329
+ currentPath = location . pathname
330
+ } )
331
+
332
+ expect ( currentPath ) . toEqual ( '/init' )
333
+ historyUnsubscribe ( )
334
+ unsubscribe ( )
335
+ } )
336
+
337
+ it ( 'respects initialState when syncing both ways' , ( ) => {
338
+ let synced = createSyncedHistoryAndStore ( createHistory , {
339
+ stateToUrl : true ,
340
+ urlToState : true
341
+ } , {
342
+ routing : {
343
+ location : {
344
+ pathname : '/init' ,
345
+ search : '' ,
346
+ hash : '' ,
347
+ state : null ,
348
+ action : 'PUSH' ,
349
+ key : 'abcde'
350
+ }
351
+ }
352
+ } )
353
+
354
+ let history = synced . history
355
+ let unsubscribe = synced . unsubscribe
356
+
357
+ let currentPath
358
+ const historyUnsubscribe = history . listen ( location => {
359
+ currentPath = location . pathname
360
+ } )
361
+
362
+ expect ( currentPath ) . toEqual ( '/init' )
363
+ historyUnsubscribe ( )
364
+ unsubscribe ( )
365
+ } )
366
+ } )
367
+
276
368
describe ( 'syncReduxAndRouter' , ( ) => {
277
369
let history , store , unsubscribe
278
370
279
371
beforeEach ( ( ) => {
280
- let synced = createSyncedHistoryAndStore ( createHistory )
372
+ let synced = createSyncedHistoryAndStore ( createHistory , {
373
+ urlToState : true
374
+ } )
281
375
history = synced . history
282
376
store = synced . store
283
377
unsubscribe = synced . unsubscribe
@@ -545,7 +639,9 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
545
639
let history , store , unsubscribe
546
640
547
641
beforeEach ( ( ) => {
548
- const synced = createSyncedHistoryAndStore ( useQueries ( createHistory ) )
642
+ const synced = createSyncedHistoryAndStore ( useQueries ( createHistory ) , {
643
+ urlToState : true
644
+ } )
549
645
history = synced . history
550
646
store = synced . store
551
647
unsubscribe = synced . unsubscribe
@@ -583,7 +679,8 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
583
679
584
680
beforeEach ( ( ) => {
585
681
const synced = createSyncedHistoryAndStore (
586
- ( ) => useBasename ( createHistory ) ( { basename : '/foobar' } )
682
+ ( ) => useBasename ( createHistory ) ( { basename : '/foobar' } ) ,
683
+ { urlToState : true }
587
684
)
588
685
history = synced . history
589
686
store = synced . store
0 commit comments