@@ -135,8 +135,8 @@ describe('ConnectedRouter', () => {
135
135
136
136
expect ( onLocationChangedSpy . mock . calls [ 1 ] [ 0 ] . state ) . toEqual ( { foo : 'bar' } )
137
137
} )
138
-
139
- it ( 'changing store location updates history ' , ( ) => {
138
+
139
+ it ( 'updates history when store location state changes ' , ( ) => {
140
140
store = createStore (
141
141
combineReducers ( {
142
142
router : connectRouter ( props . history )
@@ -166,9 +166,44 @@ describe('ConnectedRouter', () => {
166
166
} ,
167
167
action : 'PUSH' ,
168
168
}
169
- } )
170
-
171
- expect ( props . history . entries ) . toHaveLength ( 3 )
169
+ } )
170
+
171
+ expect ( props . history . entries ) . toHaveLength ( 3 )
172
+
173
+ store . dispatch ( {
174
+ type : LOCATION_CHANGE ,
175
+ payload : {
176
+ location : {
177
+ pathname : '/' ,
178
+ search : '' ,
179
+ hash : '' ,
180
+ state : { foo : 'baz' }
181
+ } ,
182
+ action : 'PUSH' ,
183
+ }
184
+ } )
185
+
186
+ expect ( props . history . entries ) . toHaveLength ( 4 )
187
+ } )
188
+
189
+ it ( 'does not update history when store location state is unchanged' , ( ) => {
190
+ store = createStore (
191
+ combineReducers ( {
192
+ router : connectRouter ( props . history )
193
+ } ) ,
194
+ compose ( applyMiddleware ( routerMiddleware ( props . history ) ) )
195
+ )
196
+
197
+ mount (
198
+ < Provider store = { store } >
199
+ < ConnectedRouter { ...props } >
200
+ < Route path = "/" render = { ( ) => < div > Home</ div > } />
201
+ </ ConnectedRouter >
202
+ </ Provider >
203
+ )
204
+
205
+ // Need to add PUSH action to history because initial POP action prevents history updates
206
+ props . history . push ( { pathname : "/" } )
172
207
173
208
store . dispatch ( {
174
209
type : LOCATION_CHANGE ,
@@ -181,27 +216,27 @@ describe('ConnectedRouter', () => {
181
216
} ,
182
217
action : 'PUSH' ,
183
218
}
184
- } )
185
-
186
- expect ( props . history . entries ) . toHaveLength ( 3 )
187
-
219
+ } )
220
+
221
+ expect ( props . history . entries ) . toHaveLength ( 3 )
222
+
188
223
store . dispatch ( {
189
224
type : LOCATION_CHANGE ,
190
225
payload : {
191
226
location : {
192
227
pathname : '/' ,
193
228
search : '' ,
194
229
hash : '' ,
195
- state : { foo : 'baz ' }
230
+ state : { foo : 'bar ' }
196
231
} ,
197
232
action : 'PUSH' ,
198
233
}
199
234
} )
200
235
201
- expect ( props . history . entries ) . toHaveLength ( 4 )
236
+ expect ( props . history . entries ) . toHaveLength ( 3 )
202
237
} )
203
-
204
- it ( 'supports custom state compare function' , ( ) => {
238
+
239
+ it ( 'supports custom location state compare function' , ( ) => {
205
240
store = createStore (
206
241
combineReducers ( {
207
242
router : connectRouter ( props . history )
@@ -212,11 +247,20 @@ describe('ConnectedRouter', () => {
212
247
mount (
213
248
< Provider store = { store } >
214
249
< ConnectedRouter
215
- stateCompareFunction = { ( a , b ) => {
216
- return a === undefined || ( a . foo === "baz" && b . foo === 'bar' ) ? true : false
250
+ stateCompareFunction = { ( storeState , historyState ) => {
251
+ // If the store and history states are not undefined,
252
+ // prevent history from updating when 'baz' is added to the store after 'bar'
253
+ if ( storeState !== undefined && historyState !== undefined ) {
254
+ if ( storeState . foo === "baz" && historyState . foo === 'bar' ) {
255
+ return true
256
+ }
257
+ }
258
+
259
+ // Otherwise return a normal object comparison result
260
+ return JSON . stringify ( storeState ) === JSON . stringify ( historyState )
217
261
} }
218
262
{ ...props }
219
- >
263
+ >
220
264
< Route path = "/" render = { ( ) => < div > Home</ div > } />
221
265
</ ConnectedRouter >
222
266
</ Provider >
@@ -236,7 +280,7 @@ describe('ConnectedRouter', () => {
236
280
} ,
237
281
action : 'PUSH' ,
238
282
}
239
- } )
283
+ } )
240
284
241
285
expect ( props . history . entries ) . toHaveLength ( 3 )
242
286
0 commit comments