This repository has been archived by the owner on Oct 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 642
/
Copy path_createSyncTest.js
315 lines (252 loc) · 8.51 KB
/
_createSyncTest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import expect from 'expect'
import React from 'react'
import ReactDOM from 'react-dom'
import { Router, Route, useRouterHistory } from 'react-router'
import { Provider } from 'react-redux'
import { createStore, combineReducers } from 'redux'
import { ActionCreators, instrument } from 'redux-devtools'
import syncHistoryWithStore from '../src/sync'
import { routerReducer } from '../src/reducer'
expect.extend({
toContainLocation({
pathname,
search = '',
hash = '',
state = null,
query,
action = 'PUSH'
}) {
const { locationBeforeTransitions } = this.actual.getState().routing
const location = locationBeforeTransitions
expect(location.pathname).toEqual(pathname)
expect(location.search).toEqual(search)
expect(location.hash).toEqual(hash)
expect(location.state).toEqual(state)
expect(location.query).toEqual(query)
expect(location.action).toEqual(action)
}
})
function createSyncedHistoryAndStore(originalHistory) {
const store = createStore(combineReducers({
routing: routerReducer
}))
const history = syncHistoryWithStore(originalHistory, store)
return { history, store }
}
const defaultReset = () => {}
export default function createTests(createHistory, name, reset = defaultReset) {
describe(name, () => {
beforeEach(reset)
describe('syncHistoryWithStore', () => {
let history, store
beforeEach(() => {
let synced = createSyncedHistoryAndStore(createHistory())
history = synced.history
store = synced.store
})
afterEach(() => {
history.unsubscribe()
})
it('syncs history -> redux', () => {
expect(store).toContainLocation({
pathname: '/',
action: 'POP'
})
history.push('/foo')
expect(store).toContainLocation({
pathname: '/foo'
})
history.push({ state: { bar: 'baz' }, pathname: '/foo' })
expect(store).toContainLocation({
pathname: '/foo',
state: { bar: 'baz' },
action: 'PUSH'
})
history.replace('/bar')
expect(store).toContainLocation({
pathname: '/bar',
action: 'REPLACE'
})
history.push('/bar')
expect(store).toContainLocation({
pathname: '/bar',
action: 'REPLACE' // Converted by history.
})
history.push('/bar?query=1')
expect(store).toContainLocation({
pathname: '/bar',
search: '?query=1'
})
history.push('/bar#baz')
expect(store).toContainLocation({
pathname: '/bar',
hash: '#baz'
})
history.replace({
pathname: '/bar',
search: '?query=1',
state: { bar: 'baz' }
})
expect(store).toContainLocation({
pathname: '/bar',
search: '?query=1',
state: { bar: 'baz' },
action: 'REPLACE'
})
history.replace({
pathname: '/bar',
search: '?query=1',
hash: '#hash=2',
state: { bar: 'baz' }
})
expect(store).toContainLocation({
pathname: '/bar',
search: '?query=1',
hash: '#hash=2',
state: { bar: 'baz' },
action: 'REPLACE'
})
})
it('provides an unsubscribe method to stop listening to history and store', () => {
history.push('/foo')
expect(store).toContainLocation({
pathname: '/foo'
})
history.unsubscribe()
history.push('/bar')
expect(store).toContainLocation({
pathname: '/foo'
})
})
it('updates the router even if path is the same', () => {
history.push('/')
const updates = []
const historyUnsubscribe = history.listen(location => {
updates.push(location.pathname)
})
history.push('/foo')
history.push('/foo')
history.replace('/foo')
expect(updates).toEqual([ '/foo', '/foo', '/foo' ])
historyUnsubscribe()
})
})
describe('Server', () => {
it('handles inital load correctly', () => {
// Server
const { store: serverStore } = createSyncedHistoryAndStore(createHistory('/'))
expect(serverStore).toContainLocation({
pathname: '/',
action: 'POP'
})
// Client
let clientStore = createStore(combineReducers({
routing: routerReducer
}), serverStore.getState())
let clientHistory = useRouterHistory(createHistory)()
const historyListen = expect.createSpy()
const historyUnsubscribe = clientHistory.listen(historyListen)
syncHistoryWithStore(clientHistory, clientStore)
// History v3: Listener should not be called during initialization
expect(historyListen.calls.length).toBe(0)
clientStore.dispatch({
type: 'non-router'
})
// We expect that we still didn't get any call to history after a non-router action is dispatched
expect(historyListen.calls.length).toBe(0)
historyUnsubscribe()
})
})
describe('Redux DevTools', () => {
let originalHistory, history, store, devToolsStore
beforeEach(() => {
originalHistory = createHistory()
// Set initial URL before syncing
originalHistory.push('/foo')
store = createStore(
combineReducers({
routing: routerReducer
}),
instrument()
)
devToolsStore = store.liftedStore
history = syncHistoryWithStore(originalHistory, store)
})
afterEach(() => {
history.unsubscribe()
})
it('resets to the initial url', () => {
let currentPath
const historyUnsubscribe = history.listen(location => {
currentPath = location.pathname
})
history.push('/bar')
devToolsStore.dispatch(ActionCreators.reset())
expect(currentPath).toEqual('/foo')
historyUnsubscribe()
})
it('handles toggle after history change', () => {
let currentPath
const historyUnsubscribe = history.listen(location => {
currentPath = location.pathname
})
history.push('/foo2') // DevTools action #2
history.push('/foo3') // DevTools action #3
// When we toggle an action, the devtools will revert the action
// and we therefore expect the history to update to the previous path
devToolsStore.dispatch(ActionCreators.toggleAction(3))
expect(currentPath).toEqual('/foo2')
historyUnsubscribe()
})
})
if (typeof(document) !== 'undefined') {
describe('Redux Router component', () => {
let store, history, rootElement
beforeEach(() => {
store = createStore(combineReducers({
routing: routerReducer
}))
history = syncHistoryWithStore(useRouterHistory(createHistory)(), store)
rootElement = document.createElement('div')
document.body.appendChild(rootElement)
})
afterEach(() => {
history.unsubscribe()
rootElement.parentNode.removeChild(rootElement)
})
it('syncs history -> components', () => {
history.push('/foo')
ReactDOM.render(
<Provider store={store}>
<Router history={history}>
<Route path="/" component={props => (<span>{props.children}</span>)}>
<Route path="foo" component={() => (<span>at /foo</span>)} />
<Route path="bar" component={() => (<span>at /bar</span>)} />
</Route>
</Router>
</Provider>,
rootElement
)
expect(rootElement.textContent).toEqual('at /foo')
history.push('/bar')
expect(rootElement.textContent).toEqual('at /bar')
})
it('syncs history -> components when the initial route gets replaced', () => {
history.push('/foo')
ReactDOM.render(
<Provider store={store}>
<Router history={history}>
<Route path="/" component={props => (<span>{props.children}</span>)}>
<Route path="foo" onEnter={(nextState, replace) => replace('/bar')} />
<Route path="bar" component={() => (<span>at /bar</span>)} />
</Route>
</Router>
</Provider>,
rootElement
)
expect(rootElement.textContent).toEqual('at /bar')
})
})
}
})
}