Skip to content

Commit 824c816

Browse files
committed
move RoutingContext tests to their own file
1 parent 4fba218 commit 824c816

File tree

2 files changed

+110
-99
lines changed

2 files changed

+110
-99
lines changed

modules/__tests__/Router-test.js

Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -277,105 +277,6 @@ describe('Router', function () {
277277

278278
})
279279

280-
describe('RoutingContext', function () {
281-
it('applies custom RoutingContext', function (done) {
282-
const Parent = ({ children }) => <span>parent:{children}</span>
283-
const Child = () => <span>child</span>
284-
285-
class LabelWrapper extends Component {
286-
constructor(props, context) {
287-
super(props, context)
288-
this.createElement = this.createElement.bind(this)
289-
}
290-
291-
createElement(component, props) {
292-
const { label, createElement } = this.props
293-
294-
return (
295-
<span>
296-
{label}-inner:{createElement(component, props)}
297-
</span>
298-
)
299-
}
300-
301-
render() {
302-
const { label, children } = this.props
303-
const child = React.cloneElement(children, {
304-
createElement: this.createElement
305-
})
306-
307-
return (
308-
<span>
309-
{label}-outer:{child}
310-
</span>
311-
)
312-
}
313-
}
314-
315-
LabelWrapper.defaultProps = {
316-
createElement: React.createElement
317-
}
318-
319-
const CustomRoutingContext = props => (
320-
<LabelWrapper label="m1">
321-
<LabelWrapper label="m2">
322-
<RoutingContext {...props} />
323-
</LabelWrapper>
324-
</LabelWrapper>
325-
)
326-
327-
render((
328-
<Router
329-
history={createHistory('/child')}
330-
RoutingContext={CustomRoutingContext}
331-
>
332-
<Route path="/" component={Parent}>
333-
<Route path="child" component={Child} />
334-
</Route>
335-
</Router>
336-
), node, function () {
337-
// Note that the nesting order is inverted for `createElement`, because
338-
// the order of function application is outermost-first.
339-
expect(node.textContent).toBe(
340-
'm1-outer:m2-outer:m2-inner:m1-inner:parent:m2-inner:m1-inner:child'
341-
)
342-
done()
343-
})
344-
})
345-
346-
it('passes router props to custom RoutingContext', function (done) {
347-
const MyComponent = () => <div />
348-
const route = { path: '/', component: MyComponent }
349-
350-
const Wrapper = (
351-
{ routes, components, foo, RoutingContext, children }
352-
) => {
353-
expect(routes).toEqual([ route ])
354-
expect(components).toEqual([ MyComponent ])
355-
expect(foo).toBe('bar')
356-
expect(RoutingContext).toNotExist()
357-
done()
358-
359-
return children
360-
}
361-
const CustomRoutingContext = props => (
362-
<Wrapper {...props}>
363-
<RoutingContext {...props} />
364-
</Wrapper>
365-
)
366-
367-
render((
368-
<Router
369-
history={createHistory('/')}
370-
routes={route}
371-
RoutingContext={CustomRoutingContext}
372-
foo="bar"
373-
/>
374-
), node)
375-
})
376-
377-
})
378-
379280
describe('async components', function () {
380281
let componentSpy, RoutingSpy
381282

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import expect from 'expect'
2+
import React, { Component } from 'react'
3+
import { render, unmountComponentAtNode } from 'react-dom'
4+
import createHistory from 'history/lib/createMemoryHistory'
5+
import Router from '../Router'
6+
import Route from '../Route'
7+
import RoutingContext from '../RoutingContext'
8+
9+
describe('RoutingContext', function () {
10+
let node
11+
beforeEach(() => node = document.createElement('div'))
12+
afterEach(() => unmountComponentAtNode(node))
13+
14+
it('applies custom RoutingContext', function (done) {
15+
const Parent = ({ children }) => <span>parent:{children}</span>
16+
const Child = () => <span>child</span>
17+
18+
class LabelWrapper extends Component {
19+
constructor(props, context) {
20+
super(props, context)
21+
this.createElement = this.createElement.bind(this)
22+
}
23+
24+
createElement(component, props) {
25+
const { label, createElement } = this.props
26+
27+
return (
28+
<span>
29+
{label}-inner:{createElement(component, props)}
30+
</span>
31+
)
32+
}
33+
34+
render() {
35+
const { label, children } = this.props
36+
const child = React.cloneElement(children, {
37+
createElement: this.createElement
38+
})
39+
40+
return (
41+
<span>
42+
{label}-outer:{child}
43+
</span>
44+
)
45+
}
46+
}
47+
48+
LabelWrapper.defaultProps = {
49+
createElement: React.createElement
50+
}
51+
52+
const CustomRoutingContext = props => (
53+
<LabelWrapper label="m1">
54+
<LabelWrapper label="m2">
55+
<RoutingContext {...props} />
56+
</LabelWrapper>
57+
</LabelWrapper>
58+
)
59+
60+
render((
61+
<Router
62+
history={createHistory('/child')}
63+
RoutingContext={CustomRoutingContext}
64+
>
65+
<Route path="/" component={Parent}>
66+
<Route path="child" component={Child} />
67+
</Route>
68+
</Router>
69+
), node, function () {
70+
// Note that the nesting order is inverted for `createElement`, because
71+
// the order of function application is outermost-first.
72+
expect(node.textContent).toBe(
73+
'm1-outer:m2-outer:m2-inner:m1-inner:parent:m2-inner:m1-inner:child'
74+
)
75+
done()
76+
})
77+
})
78+
79+
it('passes router props to custom RoutingContext', function (done) {
80+
const MyComponent = () => <div />
81+
const route = { path: '/', component: MyComponent }
82+
83+
const Wrapper = (
84+
{ routes, components, foo, RoutingContext, children }
85+
) => {
86+
expect(routes).toEqual([ route ])
87+
expect(components).toEqual([ MyComponent ])
88+
expect(foo).toBe('bar')
89+
expect(RoutingContext).toNotExist()
90+
done()
91+
92+
return children
93+
}
94+
const CustomRoutingContext = props => (
95+
<Wrapper {...props}>
96+
<RoutingContext {...props} />
97+
</Wrapper>
98+
)
99+
100+
render((
101+
<Router
102+
history={createHistory('/')}
103+
routes={route}
104+
RoutingContext={CustomRoutingContext}
105+
foo="bar"
106+
/>
107+
), node)
108+
})
109+
110+
})

0 commit comments

Comments
 (0)