@@ -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,8 +115,20 @@ 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' ) ;
@@ -147,7 +159,6 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
147
159
} ) ;
148
160
149
161
it ( 'syncs redux -> router' , ( ) => {
150
- const { history, store } = createSyncedHistoryAndStore ( createHistory ) ;
151
162
expect ( store . getState ( ) . routing ) . toEqual ( {
152
163
path : '/' ,
153
164
changeId : 1 ,
@@ -205,7 +216,6 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
205
216
} ) ;
206
217
207
218
it ( 'updates the router even if path is the same' , ( ) => {
208
- const { history, store } = createSyncedHistoryAndStore ( createHistory ) ;
209
219
expect ( store . getState ( ) . routing ) . toEqual ( {
210
220
path : '/' ,
211
221
changeId : 1 ,
@@ -239,7 +249,6 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
239
249
} ) ;
240
250
241
251
it ( 'does not update the router for other state changes' , ( ) => {
242
- const { history, store } = createSyncedHistoryAndStore ( createHistory ) ;
243
252
store . dispatch ( {
244
253
type : 'RANDOM_ACTION' ,
245
254
value : 5
@@ -254,7 +263,6 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
254
263
} ) ;
255
264
256
265
it ( 'only updates the router once when dispatching from `listenBefore`' , ( ) => {
257
- const { history, store } = createSyncedHistoryAndStore ( createHistory ) ;
258
266
expect ( store . getState ( ) . routing ) . toEqual ( {
259
267
path : '/' ,
260
268
changeId : 1 ,
@@ -280,10 +288,9 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
280
288
} ) ;
281
289
282
290
it ( 'does not unnecessarily update the store' , ( ) => {
283
- const { history, store } = createSyncedHistoryAndStore ( createHistory ) ;
284
291
const updates = [ ] ;
285
292
286
- const unsubscribe = store . subscribe ( ( ) => {
293
+ const unsubscribeFromStore = store . subscribe ( ( ) => {
287
294
updates . push ( store . getState ( ) )
288
295
} ) ;
289
296
@@ -293,7 +300,7 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
293
300
store . dispatch ( replacePath ( '/bar' ) ) ;
294
301
store . dispatch ( replacePath ( '/bar' , { bar : 'foo' } ) ) ;
295
302
296
- unsubscribe ( ) ;
303
+ unsubscribeFromStore ( ) ;
297
304
298
305
expect ( updates . length ) . toBe ( 5 ) ;
299
306
expect ( updates ) . toEqual ( [
@@ -341,7 +348,6 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
341
348
} ) ;
342
349
343
350
it ( 'allows updating the route from within `listenBefore`' , ( ) => {
344
- const { history, store } = createSyncedHistoryAndStore ( createHistory ) ;
345
351
expect ( store . getState ( ) . routing ) . toEqual ( {
346
352
path : '/' ,
347
353
changeId : 1 ,
0 commit comments