@@ -28,15 +28,18 @@ expect.extend({
2828 }
2929} )
3030
31- function createSyncedHistoryAndStore ( createHistory ) {
31+ function createSyncedHistoryAndStore ( createHistory , syncOptions , initialState ) {
3232 const history = createHistory ( )
3333 const middleware = syncHistory ( history )
34- const { unsubscribe } = middleware
35-
36- const createStoreWithMiddleware = applyMiddleware ( middleware ) ( createStore )
37- const store = createStoreWithMiddleware ( combineReducers ( {
34+ const reducer = combineReducers ( {
3835 routing : routeReducer
39- } ) )
36+ } )
37+ const store = createStore (
38+ reducer ,
39+ initialState ,
40+ applyMiddleware ( middleware )
41+ )
42+ const unsubscribe = middleware . syncWith ( store , syncOptions )
4043
4144 return { history, store, unsubscribe }
4245}
@@ -197,18 +200,17 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
197200 history . push ( '/foo' )
198201
199202 const middleware = syncHistory ( history )
200- unsubscribe = middleware . unsubscribe
201-
202- const finalCreateStore = compose (
203+ store = createStore ( combineReducers ( {
204+ routing : routeReducer
205+ } ) , compose (
203206 applyMiddleware ( middleware ) ,
204207 instrument ( )
205- ) ( createStore )
206- store = finalCreateStore ( combineReducers ( {
207- routing : routeReducer
208- } ) )
208+ ) )
209209 devToolsStore = store . liftedStore
210-
211- middleware . listenForReplays ( store )
210+ unsubscribe = middleware . syncWith ( store , {
211+ stateToUrl : true ,
212+ urlToState : true
213+ } )
212214 } )
213215
214216 afterEach ( ( ) => {
@@ -273,11 +275,103 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
273275 } )
274276 } )
275277
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+
276368 describe ( 'syncReduxAndRouter' , ( ) => {
277369 let history , store , unsubscribe
278370
279371 beforeEach ( ( ) => {
280- let synced = createSyncedHistoryAndStore ( createHistory )
372+ let synced = createSyncedHistoryAndStore ( createHistory , {
373+ urlToState : true
374+ } )
281375 history = synced . history
282376 store = synced . store
283377 unsubscribe = synced . unsubscribe
@@ -545,7 +639,9 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
545639 let history , store , unsubscribe
546640
547641 beforeEach ( ( ) => {
548- const synced = createSyncedHistoryAndStore ( useQueries ( createHistory ) )
642+ const synced = createSyncedHistoryAndStore ( useQueries ( createHistory ) , {
643+ urlToState : true
644+ } )
549645 history = synced . history
550646 store = synced . store
551647 unsubscribe = synced . unsubscribe
@@ -583,7 +679,8 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
583679
584680 beforeEach ( ( ) => {
585681 const synced = createSyncedHistoryAndStore (
586- ( ) => useBasename ( createHistory ) ( { basename : '/foobar' } )
682+ ( ) => useBasename ( createHistory ) ( { basename : '/foobar' } ) ,
683+ { urlToState : true }
587684 )
588685 history = synced . history
589686 store = synced . store
0 commit comments