-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathRouter-test.js
461 lines (400 loc) · 11.9 KB
/
Router-test.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
import expect from 'expect'
import React, { Component } from 'react'
import { render, unmountComponentAtNode } from 'react-dom'
import createHistory from 'history/lib/createMemoryHistory'
import Route from '../Route'
import Router from '../Router'
import RoutingContext from '../RoutingContext'
describe('Router', function () {
class Parent extends Component {
render() {
return <div>parent {this.props.children}</div>
}
}
class Child extends Component {
render() {
return <div>child</div>
}
}
let node
beforeEach(function () {
node = document.createElement('div')
})
afterEach(function () {
unmountComponentAtNode(node)
})
it('renders routes', function (done) {
render((
<Router history={createHistory('/')}>
<Route path="/" component={Parent} />
</Router>
), node, function () {
expect(node.textContent).toEqual('parent ')
done()
})
})
it('renders child routes when the parent does not have a path', function (done) {
render((
<Router history={createHistory('/')}>
<Route component={Parent}>
<Route component={Parent}>
<Route path="/" component={Child} />
</Route>
</Route>
</Router>
), node, function () {
expect(node.textContent).toEqual('parent parent child')
done()
})
})
it('renders nested children correctly', function (done) {
render((
<Router history={createHistory('/hello')}>
<Route component={Parent}>
<Route path="hello" component={Child} />
</Route>
</Router>
), node, function () {
expect(node.textContent).toMatch(/parent/)
expect(node.textContent).toMatch(/child/)
done()
})
})
it("renders the child's component when it has no component", function (done) {
render((
<Router history={createHistory('/hello')}>
<Route>
<Route path="hello" component={Child} />
</Route>
</Router>
), node, function () {
expect(node.textContent).toMatch(/child/)
done()
})
})
it('renders with a custom "createElement" prop', function (done) {
class Wrapper extends Component {
render() {
return <this.props.component fromWrapper="wrapped" />
}
}
class Child extends Component {
render() {
return <div>{this.props.fromWrapper}</div>
}
}
render((
<Router history={createHistory('/')} createElement={x => <Wrapper component={x} />}>
<Route path="/" component={Child}/>
</Router>
), node, function () {
expect(node.textContent).toEqual('wrapped')
done()
})
})
describe('with named components', function () {
class Parent extends Component {
render() {
return (
<div>{this.props.sidebar}-{this.props.content}</div>
)
}
}
class Sidebar extends Component {
render() {
return <div>sidebar</div>
}
}
class Content extends Component {
render() {
return <div>content</div>
}
}
let routes
beforeEach(function () {
routes = (
<Route component={Parent}>
<Route path="/" components={{ sidebar: Sidebar, content: Content }} />
</Route>
)
})
it('receives those components as props', function (done) {
render((
<Router history={createHistory('/')} routes={routes} />
), node, function () {
expect(node.textContent).toEqual('sidebar-content')
done()
})
})
it('sets the key on those components', function (done) {
const components = {}
function createElementSpy(Component, props) {
if (props.key) {
components[props.key] = Component
}
return null
}
render((
<Router
history={createHistory('/')} routes={routes}
createElement={createElementSpy}
/>
), node, function () {
expect(components.sidebar).toBe(Sidebar)
expect(components.content).toBe(Content)
done()
})
})
})
describe('at a route with special characters', function () {
it('does not double escape', function (done) {
// https://github.com/rackt/react-router/issues/1574
class MyComponent extends Component {
render() {
return <div>{this.props.params.someToken}</div>
}
}
render((
<Router history={createHistory('/point/aaa%2Bbbb')}>
<Route path="point/:someToken" component={MyComponent} />
</Router>
), node, function () {
expect(node.textContent).toEqual('aaa+bbb')
done()
})
})
it('does not double escape when nested', function (done) {
// https://github.com/rackt/react-router/issues/1574
class MyWrapperComponent extends Component {
render() {
return this.props.children
}
}
class MyComponent extends Component {
render() {
return <div>{this.props.params.someToken}</div>
}
}
render((
<Router history={createHistory('/point/aaa%2Bbbb')}>
<Route component={MyWrapperComponent}>
<Route path="point/:someToken" component={MyComponent} />
</Route>
</Router>
), node, function () {
expect(node.textContent).toEqual('aaa+bbb')
done()
})
})
it('is happy to have colons in parameter values', function (done) {
// https://github.com/rackt/react-router/issues/1759
class MyComponent extends Component {
render() {
return <div>{this.props.params.foo}</div>
}
}
render((
<Router history={createHistory('/ns/aaa:bbb/bar')}>
<Route path="ns/:foo/bar" component={MyComponent} />
</Router>
), node, function () {
expect(node.textContent).toEqual('aaa:bbb')
done()
})
})
it('handles % in parameters', function (done) {
// https://github.com/rackt/react-router/issues/1766
class MyComponent extends Component {
render() {
return <div>{this.props.params.name}</div>
}
}
render((
<Router history={createHistory('/company/CADENCE%20DESIGN%20SYSTEM%20INC%20NOTE%202.625%25%2060')}>
<Route path="/company/:name" component={MyComponent} />
</Router>
), node, function () {
expect(node.textContent).toEqual('CADENCE DESIGN SYSTEM INC NOTE 2.625% 60')
done()
})
})
it('handles forward slashes', function (done) {
// https://github.com/rackt/react-router/issues/1865
class Parent extends Component {
render() {
return <div>{this.props.children}</div>
}
}
class Child extends Component {
render() {
return <h1>{this.props.params.name}</h1>
}
}
render((
<Router history={createHistory('/apple%2Fbanana')}>
<Route component={Parent}>
<Route path="/:name" component={Child} />
</Route>
</Router>
), node, function () {
expect(node.textContent).toEqual('apple/banana')
done()
})
})
})
describe('RoutingContext', function () {
it('applies custom RoutingContext', function (done) {
const Parent = ({ children }) => <span>parent:{children}</span>
const Child = () => <span>child</span>
class LabelWrapper extends Component {
constructor(props, context) {
super(props, context)
this.createElement = this.createElement.bind(this)
}
createElement(component, props) {
const { label, createElement } = this.props
return (
<span>
{label}-inner:{createElement(component, props)}
</span>
)
}
render() {
const { label, children } = this.props
const child = React.cloneElement(children, {
createElement: this.createElement
})
return (
<span>
{label}-outer:{child}
</span>
)
}
}
LabelWrapper.defaultProps = {
createElement: React.createElement
}
const CustomRoutingContext = props => (
<LabelWrapper label="m1">
<LabelWrapper label="m2">
<RoutingContext {...props} />
</LabelWrapper>
</LabelWrapper>
)
render((
<Router
history={createHistory('/child')}
RoutingContext={CustomRoutingContext}
>
<Route path="/" component={Parent}>
<Route path="child" component={Child} />
</Route>
</Router>
), node, function () {
// Note that the nesting order is inverted for `createElement`, because
// the order of function application is outermost-first.
expect(node.textContent).toBe(
'm1-outer:m2-outer:m2-inner:m1-inner:parent:m2-inner:m1-inner:child'
)
done()
})
})
it('passes router props to custom RoutingContext', function (done) {
const MyComponent = () => <div />
const route = { path: '/', component: MyComponent }
const Wrapper = (
{ routes, components, foo, RoutingContext, children }
) => {
expect(routes).toEqual([ route ])
expect(components).toEqual([ MyComponent ])
expect(foo).toBe('bar')
expect(RoutingContext).toNotExist()
done()
return children
}
const CustomRoutingContext = props => (
<Wrapper {...props}>
<RoutingContext {...props} />
</Wrapper>
)
render((
<Router
history={createHistory('/')}
routes={route}
RoutingContext={CustomRoutingContext}
foo="bar"
/>
), node)
})
})
describe('async components', function () {
let componentSpy, RoutingSpy
beforeEach(function () {
componentSpy = expect.createSpy()
RoutingSpy = ({ components }) => {
componentSpy(components)
return <div />
}
})
it('should support getComponent', function (done) {
const Component = () => <div />
const getComponent = (_, callback) => {
setTimeout(() => callback(null, Component))
}
render((
<Router history={createHistory('/')} RoutingContext={RoutingSpy}>
<Route path="/" getComponent={getComponent} />
</Router>
), node, function () {
setTimeout(function () {
expect(componentSpy).toHaveBeenCalledWith([ Component ])
done()
})
})
})
it('should support getComponents', function (done) {
const foo = () => <div />
const bar = () => <div />
const getComponents = (_, callback) => {
setTimeout(() => callback(null, { foo, bar }))
}
render((
<Router history={createHistory('/')} RoutingContext={RoutingSpy}>
<Route path="/" getComponents={getComponents} />
</Router>
), node, function () {
setTimeout(function () {
expect(componentSpy).toHaveBeenCalledWith([ { foo, bar } ])
done()
})
})
})
})
describe('error handling', function () {
let error, getComponent
beforeEach(function () {
error = new Error('error fixture')
getComponent = (_, callback) => callback(error)
})
it('should work with onError', function (done) {
const errorSpy = expect.createSpy()
render((
<Router history={createHistory('/')} onError={errorSpy}>
<Route path="/" getComponent={getComponent} />
</Router>
), node, function () {
expect(errorSpy).toHaveBeenCalledWith(error)
done()
})
})
it('should throw without onError', function () {
expect(function () {
render((
<Router history={createHistory('/')}>
<Route path="/" getComponent={getComponent} />
</Router>
), node)
}).toThrow('error fixture')
})
})
})