Skip to content

Commit 09db27e

Browse files
committed
Split store update test into two + updated custom state compare function test + spacing
1 parent 9901818 commit 09db27e

File tree

2 files changed

+65
-21
lines changed

2 files changed

+65
-21
lines changed

src/ConnectedRouter.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const createConnectedRouter = (structure) => {
1919
constructor(props) {
2020
super(props)
2121

22-
const { store, history, onLocationChanged, stateCompareFunction} = props
22+
const { store, history, onLocationChanged, stateCompareFunction } = props
2323

2424
this.inTimeTravelling = false
2525

@@ -38,7 +38,7 @@ const createConnectedRouter = (structure) => {
3838
search: searchInHistory,
3939
hash: hashInHistory,
4040
state: stateInHistory,
41-
} = history.location
41+
} = history.location
4242

4343
// If we do time travelling, the location in store is changed but location in history is not changed
4444
if (
@@ -105,8 +105,8 @@ const createConnectedRouter = (structure) => {
105105
}).isRequired,
106106
basename: PropTypes.string,
107107
children: PropTypes.oneOfType([ PropTypes.func, PropTypes.node ]),
108-
onLocationChanged: PropTypes.func.isRequired,
109-
stateCompareFunction: PropTypes.func,
108+
onLocationChanged: PropTypes.func.isRequired,
109+
stateCompareFunction: PropTypes.func,
110110
}
111111

112112
const mapDispatchToProps = dispatch => ({

test/ConnectedRouter.test.js

Lines changed: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ describe('ConnectedRouter', () => {
135135

136136
expect(onLocationChangedSpy.mock.calls[1][0].state).toEqual({ foo: 'bar'})
137137
})
138-
139-
it('changing store location updates history', () => {
138+
139+
it('updates history when store location state changes', () => {
140140
store = createStore(
141141
combineReducers({
142142
router: connectRouter(props.history)
@@ -166,9 +166,44 @@ describe('ConnectedRouter', () => {
166166
},
167167
action: 'PUSH',
168168
}
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: "/" })
172207

173208
store.dispatch({
174209
type: LOCATION_CHANGE,
@@ -181,27 +216,27 @@ describe('ConnectedRouter', () => {
181216
},
182217
action: 'PUSH',
183218
}
184-
})
185-
186-
expect(props.history.entries).toHaveLength(3)
187-
219+
})
220+
221+
expect(props.history.entries).toHaveLength(3)
222+
188223
store.dispatch({
189224
type: LOCATION_CHANGE,
190225
payload: {
191226
location: {
192227
pathname: '/',
193228
search: '',
194229
hash: '',
195-
state: { foo: 'baz' }
230+
state: { foo: 'bar' }
196231
},
197232
action: 'PUSH',
198233
}
199234
})
200235

201-
expect(props.history.entries).toHaveLength(4)
236+
expect(props.history.entries).toHaveLength(3)
202237
})
203-
204-
it('supports custom state compare function', () => {
238+
239+
it('supports custom location state compare function', () => {
205240
store = createStore(
206241
combineReducers({
207242
router: connectRouter(props.history)
@@ -212,11 +247,20 @@ describe('ConnectedRouter', () => {
212247
mount(
213248
<Provider store={store}>
214249
<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)
217261
}}
218262
{...props}
219-
>
263+
>
220264
<Route path="/" render={() => <div>Home</div>} />
221265
</ConnectedRouter>
222266
</Provider>
@@ -236,7 +280,7 @@ describe('ConnectedRouter', () => {
236280
},
237281
action: 'PUSH',
238282
}
239-
})
283+
})
240284

241285
expect(props.history.entries).toHaveLength(3)
242286

0 commit comments

Comments
 (0)