@@ -7,8 +7,8 @@ function createSyncedHistoryAndStore(createHistory) {
7
7
routing : routeReducer
8
8
} ) ) ;
9
9
const history = createHistory ( ) ;
10
- syncReduxAndRouter ( history , store ) ;
11
- return { history, store } ;
10
+ const unsubscribe = syncReduxAndRouter ( history , store ) ;
11
+ return { history, store, unsubscribe } ;
12
12
}
13
13
14
14
const defaultReset = ( ) => { } ;
@@ -115,39 +115,57 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
115
115
} ) ;
116
116
117
117
describe ( 'syncReduxAndRouter' , ( ) => {
118
+ let history , store , unsubscribe ;
119
+
120
+ beforeEach ( ( ) => {
121
+ let synced = createSyncedHistoryAndStore ( createHistory ) ;
122
+ history = synced . history ;
123
+ store = synced . store ;
124
+ unsubscribe = synced . unsubscribe ;
125
+ } ) ;
126
+
127
+ afterEach ( ( ) => {
128
+ unsubscribe ( ) ;
129
+ } ) ;
130
+
118
131
it ( 'syncs router -> redux' , ( ) => {
119
- const { history, store } = createSyncedHistoryAndStore ( createHistory ) ;
120
132
expect ( store . getState ( ) . routing . path ) . toEqual ( '/' ) ;
121
133
122
134
history . pushState ( null , '/foo' ) ;
123
135
expect ( store . getState ( ) . routing . path ) . toEqual ( '/foo' ) ;
124
136
expect ( store . getState ( ) . routing . state ) . toBe ( null ) ;
137
+ expect ( store . getState ( ) . routing . replace ) . toBe ( false ) ;
125
138
126
139
history . pushState ( { bar : 'baz' } , '/foo' ) ;
127
140
expect ( store . getState ( ) . routing . path ) . toEqual ( '/foo' ) ;
128
141
expect ( store . getState ( ) . routing . state ) . toEqual ( { bar : 'baz' } ) ;
142
+ expect ( store . getState ( ) . routing . replace ) . toBe ( true ) ;
129
143
130
144
history . replaceState ( null , '/bar' ) ;
131
145
expect ( store . getState ( ) . routing . path ) . toEqual ( '/bar' ) ;
132
146
expect ( store . getState ( ) . routing . state ) . toBe ( null ) ;
147
+ expect ( store . getState ( ) . routing . replace ) . toBe ( true ) ;
133
148
134
149
history . pushState ( null , '/bar' ) ;
135
150
expect ( store . getState ( ) . routing . path ) . toEqual ( '/bar' ) ;
136
151
expect ( store . getState ( ) . routing . state ) . toBe ( null ) ;
152
+ expect ( store . getState ( ) . routing . replace ) . toBe ( true ) ;
137
153
138
154
history . pushState ( null , '/bar?query=1' ) ;
139
155
expect ( store . getState ( ) . routing . path ) . toEqual ( '/bar?query=1' ) ;
156
+ expect ( store . getState ( ) . routing . replace ) . toBe ( false ) ;
140
157
141
158
history . replaceState ( { bar : 'baz' } , '/bar?query=1' ) ;
142
159
expect ( store . getState ( ) . routing . path ) . toEqual ( '/bar?query=1' ) ;
143
160
expect ( store . getState ( ) . routing . state ) . toEqual ( { bar : 'baz' } ) ;
161
+ expect ( store . getState ( ) . routing . replace ) . toBe ( true ) ;
144
162
145
- history . pushState ( null , '/bar?query=1#hash=2' ) ;
163
+ history . pushState ( { bar : 'baz' } , '/bar?query=1#hash=2' ) ;
146
164
expect ( store . getState ( ) . routing . path ) . toEqual ( '/bar?query=1#hash=2' ) ;
165
+ expect ( store . getState ( ) . routing . replace ) . toBe ( true ) ;
147
166
} ) ;
148
167
149
168
it ( 'syncs redux -> router' , ( ) => {
150
- const { history, store } = createSyncedHistoryAndStore ( createHistory ) ;
151
169
expect ( store . getState ( ) . routing ) . toEqual ( {
152
170
path : '/' ,
153
171
changeId : 1 ,
@@ -205,7 +223,6 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
205
223
} ) ;
206
224
207
225
it ( 'updates the router even if path is the same' , ( ) => {
208
- const { history, store } = createSyncedHistoryAndStore ( createHistory ) ;
209
226
expect ( store . getState ( ) . routing ) . toEqual ( {
210
227
path : '/' ,
211
228
changeId : 1 ,
@@ -239,7 +256,6 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
239
256
} ) ;
240
257
241
258
it ( 'does not update the router for other state changes' , ( ) => {
242
- const { history, store } = createSyncedHistoryAndStore ( createHistory ) ;
243
259
store . dispatch ( {
244
260
type : 'RANDOM_ACTION' ,
245
261
value : 5
@@ -254,7 +270,6 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
254
270
} ) ;
255
271
256
272
it ( 'only updates the router once when dispatching from `listenBefore`' , ( ) => {
257
- const { history, store } = createSyncedHistoryAndStore ( createHistory ) ;
258
273
expect ( store . getState ( ) . routing ) . toEqual ( {
259
274
path : '/' ,
260
275
changeId : 1 ,
@@ -280,10 +295,9 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
280
295
} ) ;
281
296
282
297
it ( 'does not unnecessarily update the store' , ( ) => {
283
- const { history, store } = createSyncedHistoryAndStore ( createHistory ) ;
284
298
const updates = [ ] ;
285
299
286
- const unsubscribe = store . subscribe ( ( ) => {
300
+ const unsubscribeFromStore = store . subscribe ( ( ) => {
287
301
updates . push ( store . getState ( ) )
288
302
} ) ;
289
303
@@ -293,7 +307,7 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
293
307
store . dispatch ( replacePath ( '/bar' ) ) ;
294
308
store . dispatch ( replacePath ( '/bar' , { bar : 'foo' } ) ) ;
295
309
296
- unsubscribe ( ) ;
310
+ unsubscribeFromStore ( ) ;
297
311
298
312
expect ( updates . length ) . toBe ( 5 ) ;
299
313
expect ( updates ) . toEqual ( [
@@ -341,7 +355,6 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
341
355
} ) ;
342
356
343
357
it ( 'allows updating the route from within `listenBefore`' , ( ) => {
344
- const { history, store } = createSyncedHistoryAndStore ( createHistory ) ;
345
358
expect ( store . getState ( ) . routing ) . toEqual ( {
346
359
path : '/' ,
347
360
changeId : 1 ,
0 commit comments