@@ -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 = ( ) => { } ;
@@ -22,18 +22,22 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
22
22
it ( 'creates actions' , ( ) => {
23
23
expect ( pushPath ( '/foo' , { bar : 'baz' } ) ) . toEqual ( {
24
24
type : UPDATE_PATH ,
25
- path : '/foo' ,
26
- replace : false ,
27
- state : { bar : 'baz' } ,
28
- avoidRouterUpdate : false
25
+ payload : {
26
+ path : '/foo' ,
27
+ replace : false ,
28
+ state : { bar : 'baz' } ,
29
+ avoidRouterUpdate : false
30
+ }
29
31
} ) ;
30
32
31
33
expect ( pushPath ( '/foo' , undefined , { avoidRouterUpdate : true } ) ) . toEqual ( {
32
34
type : UPDATE_PATH ,
33
- path : '/foo' ,
34
- state : undefined ,
35
- replace : false ,
36
- avoidRouterUpdate : true
35
+ payload : {
36
+ path : '/foo' ,
37
+ state : undefined ,
38
+ replace : false ,
39
+ avoidRouterUpdate : true
40
+ }
37
41
} ) ;
38
42
} ) ;
39
43
} ) ;
@@ -42,26 +46,32 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
42
46
it ( 'creates actions' , ( ) => {
43
47
expect ( replacePath ( '/foo' , { bar : 'baz' } ) ) . toEqual ( {
44
48
type : UPDATE_PATH ,
45
- path : '/foo' ,
46
- replace : true ,
47
- state : { bar : 'baz' } ,
48
- avoidRouterUpdate : false
49
+ payload : {
50
+ path : '/foo' ,
51
+ replace : true ,
52
+ state : { bar : 'baz' } ,
53
+ avoidRouterUpdate : false
54
+ }
49
55
} ) ;
50
56
51
57
expect ( replacePath ( '/foo' , undefined , { avoidRouterUpdate : true } ) ) . toEqual ( {
52
58
type : UPDATE_PATH ,
53
- path : '/foo' ,
54
- state : undefined ,
55
- replace : true ,
56
- avoidRouterUpdate : true
59
+ payload : {
60
+ path : '/foo' ,
61
+ state : undefined ,
62
+ replace : true ,
63
+ avoidRouterUpdate : true
64
+ }
57
65
} ) ;
58
66
59
67
expect ( replacePath ( '/foo' , undefined , { avoidRouterUpdate : false } ) ) . toEqual ( {
60
68
type : UPDATE_PATH ,
61
- path : '/foo' ,
62
- state : undefined ,
63
- replace : true ,
64
- avoidRouterUpdate : false
69
+ payload : {
70
+ path : '/foo' ,
71
+ state : undefined ,
72
+ replace : true ,
73
+ avoidRouterUpdate : false
74
+ }
65
75
} ) ;
66
76
} ) ;
67
77
} ) ;
@@ -75,8 +85,10 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
75
85
it ( 'updates the path' , ( ) => {
76
86
expect ( routeReducer ( state , {
77
87
type : UPDATE_PATH ,
78
- path : '/bar' ,
79
- replace : false
88
+ payload : {
89
+ path : '/bar' ,
90
+ replace : false
91
+ }
80
92
} ) ) . toEqual ( {
81
93
path : '/bar' ,
82
94
replace : false ,
@@ -88,9 +100,11 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
88
100
it ( 'respects replace' , ( ) => {
89
101
expect ( routeReducer ( state , {
90
102
type : UPDATE_PATH ,
91
- path : '/bar' ,
92
- replace : true ,
93
- avoidRouterUpdate : false
103
+ payload : {
104
+ path : '/bar' ,
105
+ replace : true ,
106
+ avoidRouterUpdate : false
107
+ }
94
108
} ) ) . toEqual ( {
95
109
path : '/bar' ,
96
110
replace : true ,
@@ -102,9 +116,11 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
102
116
it ( 'respects `avoidRouterUpdate` flag' , ( ) => {
103
117
expect ( routeReducer ( state , {
104
118
type : UPDATE_PATH ,
105
- path : '/bar' ,
106
- replace : false ,
107
- avoidRouterUpdate : true
119
+ payload : {
120
+ path : '/bar' ,
121
+ replace : false ,
122
+ avoidRouterUpdate : true
123
+ }
108
124
} ) ) . toEqual ( {
109
125
path : '/bar' ,
110
126
replace : false ,
@@ -115,39 +131,57 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
115
131
} ) ;
116
132
117
133
describe ( 'syncReduxAndRouter' , ( ) => {
134
+ let history , store , unsubscribe ;
135
+
136
+ beforeEach ( ( ) => {
137
+ let synced = createSyncedHistoryAndStore ( createHistory ) ;
138
+ history = synced . history ;
139
+ store = synced . store ;
140
+ unsubscribe = synced . unsubscribe ;
141
+ } ) ;
142
+
143
+ afterEach ( ( ) => {
144
+ unsubscribe ( ) ;
145
+ } ) ;
146
+
118
147
it ( 'syncs router -> redux' , ( ) => {
119
- const { history, store } = createSyncedHistoryAndStore ( createHistory ) ;
120
148
expect ( store . getState ( ) . routing . path ) . toEqual ( '/' ) ;
121
149
122
150
history . pushState ( null , '/foo' ) ;
123
151
expect ( store . getState ( ) . routing . path ) . toEqual ( '/foo' ) ;
124
152
expect ( store . getState ( ) . routing . state ) . toBe ( null ) ;
153
+ expect ( store . getState ( ) . routing . replace ) . toBe ( false ) ;
125
154
126
155
history . pushState ( { bar : 'baz' } , '/foo' ) ;
127
156
expect ( store . getState ( ) . routing . path ) . toEqual ( '/foo' ) ;
128
157
expect ( store . getState ( ) . routing . state ) . toEqual ( { bar : 'baz' } ) ;
158
+ expect ( store . getState ( ) . routing . replace ) . toBe ( true ) ;
129
159
130
160
history . replaceState ( null , '/bar' ) ;
131
161
expect ( store . getState ( ) . routing . path ) . toEqual ( '/bar' ) ;
132
162
expect ( store . getState ( ) . routing . state ) . toBe ( null ) ;
163
+ expect ( store . getState ( ) . routing . replace ) . toBe ( true ) ;
133
164
134
165
history . pushState ( null , '/bar' ) ;
135
166
expect ( store . getState ( ) . routing . path ) . toEqual ( '/bar' ) ;
136
167
expect ( store . getState ( ) . routing . state ) . toBe ( null ) ;
168
+ expect ( store . getState ( ) . routing . replace ) . toBe ( true ) ;
137
169
138
170
history . pushState ( null , '/bar?query=1' ) ;
139
171
expect ( store . getState ( ) . routing . path ) . toEqual ( '/bar?query=1' ) ;
172
+ expect ( store . getState ( ) . routing . replace ) . toBe ( false ) ;
140
173
141
174
history . replaceState ( { bar : 'baz' } , '/bar?query=1' ) ;
142
175
expect ( store . getState ( ) . routing . path ) . toEqual ( '/bar?query=1' ) ;
143
176
expect ( store . getState ( ) . routing . state ) . toEqual ( { bar : 'baz' } ) ;
177
+ expect ( store . getState ( ) . routing . replace ) . toBe ( true ) ;
144
178
145
- history . pushState ( null , '/bar?query=1#hash=2' ) ;
179
+ history . pushState ( { bar : 'baz' } , '/bar?query=1#hash=2' ) ;
146
180
expect ( store . getState ( ) . routing . path ) . toEqual ( '/bar?query=1#hash=2' ) ;
181
+ expect ( store . getState ( ) . routing . replace ) . toBe ( true ) ;
147
182
} ) ;
148
183
149
184
it ( 'syncs redux -> router' , ( ) => {
150
- const { history, store } = createSyncedHistoryAndStore ( createHistory ) ;
151
185
expect ( store . getState ( ) . routing ) . toEqual ( {
152
186
path : '/' ,
153
187
changeId : 1 ,
@@ -205,7 +239,6 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
205
239
} ) ;
206
240
207
241
it ( 'updates the router even if path is the same' , ( ) => {
208
- const { history, store } = createSyncedHistoryAndStore ( createHistory ) ;
209
242
expect ( store . getState ( ) . routing ) . toEqual ( {
210
243
path : '/' ,
211
244
changeId : 1 ,
@@ -239,10 +272,13 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
239
272
} ) ;
240
273
241
274
it ( 'does not update the router for other state changes' , ( ) => {
242
- const { history, store } = createSyncedHistoryAndStore ( createHistory ) ;
243
275
store . dispatch ( {
244
276
type : 'RANDOM_ACTION' ,
245
- value : 5
277
+ payload : {
278
+ payload : {
279
+ value : 5
280
+ }
281
+ }
246
282
} ) ;
247
283
248
284
expect ( store . getState ( ) . routing ) . toEqual ( {
@@ -254,7 +290,6 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
254
290
} ) ;
255
291
256
292
it ( 'only updates the router once when dispatching from `listenBefore`' , ( ) => {
257
- const { history, store } = createSyncedHistoryAndStore ( createHistory ) ;
258
293
expect ( store . getState ( ) . routing ) . toEqual ( {
259
294
path : '/' ,
260
295
changeId : 1 ,
@@ -266,7 +301,11 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
266
301
expect ( location . pathname ) . toEqual ( '/foo' ) ;
267
302
store . dispatch ( {
268
303
type : 'RANDOM_ACTION' ,
269
- value : 5
304
+ payload : {
305
+ payload : {
306
+ value : 5
307
+ }
308
+ }
270
309
} ) ;
271
310
} ) ;
272
311
@@ -280,10 +319,9 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
280
319
} ) ;
281
320
282
321
it ( 'does not unnecessarily update the store' , ( ) => {
283
- const { history, store } = createSyncedHistoryAndStore ( createHistory ) ;
284
322
const updates = [ ] ;
285
323
286
- const unsubscribe = store . subscribe ( ( ) => {
324
+ const unsubscribeFromStore = store . subscribe ( ( ) => {
287
325
updates . push ( store . getState ( ) )
288
326
} ) ;
289
327
@@ -293,7 +331,7 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
293
331
store . dispatch ( replacePath ( '/bar' ) ) ;
294
332
store . dispatch ( replacePath ( '/bar' , { bar : 'foo' } ) ) ;
295
333
296
- unsubscribe ( ) ;
334
+ unsubscribeFromStore ( ) ;
297
335
298
336
expect ( updates . length ) . toBe ( 5 ) ;
299
337
expect ( updates ) . toEqual ( [
@@ -341,7 +379,6 @@ module.exports = function createTests(createHistory, name, reset = defaultReset)
341
379
} ) ;
342
380
343
381
it ( 'allows updating the route from within `listenBefore`' , ( ) => {
344
- const { history, store } = createSyncedHistoryAndStore ( createHistory ) ;
345
382
expect ( store . getState ( ) . routing ) . toEqual ( {
346
383
path : '/' ,
347
384
changeId : 1 ,
0 commit comments