forked from remix-run/react-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapplyRouterMiddleware-test.js
More file actions
144 lines (129 loc) · 3.98 KB
/
applyRouterMiddleware-test.js
File metadata and controls
144 lines (129 loc) · 3.98 KB
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
import expect from 'expect'
import React, { cloneElement } from 'react'
import { render } from 'react-dom'
import Router from '../Router'
import Route from '../Route'
import createMemoryHistory from '../createMemoryHistory'
import applyMiddleware from '../applyRouterMiddleware'
const FOO_ROOT_CONTAINER_TEXT = 'FOO ROOT CONTAINER'
const BAR_ROOT_CONTAINER_TEXT = 'BAR ROOT CONTAINER'
const BAZ_CONTAINER_TEXT = 'BAZ INJECTED'
const FooRootContainer = React.createClass({
propTypes: { children: React.PropTypes.node.isRequired },
childContextTypes: { foo: React.PropTypes.string },
getChildContext() { return { foo: FOO_ROOT_CONTAINER_TEXT } },
render() {
return this.props.children
}
})
const FooContainer = React.createClass({
propTypes: { children: React.PropTypes.node.isRequired },
contextTypes: { foo: React.PropTypes.string.isRequired },
render() {
const { children, ...props } = this.props
const fooFromContext = this.context.foo
return cloneElement(children, { ...props, fooFromContext })
}
})
const useFoo = () => ({
renderRouterContext: (child) => (
<FooRootContainer>{child}</FooRootContainer>
),
renderRouteComponent: (child) => (
<FooContainer>{child}</FooContainer>
)
})
const BarRootContainer = React.createClass({
propTypes: { children: React.PropTypes.node.isRequired },
childContextTypes: { bar: React.PropTypes.string },
getChildContext() { return { bar: BAR_ROOT_CONTAINER_TEXT } },
render() {
return this.props.children
}
})
const BarContainer = React.createClass({
propTypes: { children: React.PropTypes.node.isRequired },
contextTypes: { bar: React.PropTypes.string.isRequired },
render() {
const { children, ...props } = this.props
const barFromContext = this.context.bar
return cloneElement(children, { props, barFromContext })
}
})
const useBar = () => ({
renderRouterContext: (child) => (
<BarRootContainer>{child}</BarRootContainer>
),
renderRouteComponent: (child) => (
<BarContainer>{child}</BarContainer>
)
})
const useBaz = (bazInjected) => ({
renderRouteComponent: (child) => (
cloneElement(child, { bazInjected })
)
})
const run = ({ renderWithMiddleware, Component }, assertion) => {
const div = document.createElement('div')
const routes = <Route path="/" component={Component}/>
render(<Router
render={renderWithMiddleware}
routes={routes}
history={createMemoryHistory('/')}
/>, div, () => assertion(div.innerHTML))
}
describe('applyMiddleware', () => {
it('applies one middleware', (done) => {
run({
renderWithMiddleware: applyMiddleware(useFoo()),
Component: (props) => <div>{props.fooFromContext}</div>
}, (html) => {
expect(html).toContain(FOO_ROOT_CONTAINER_TEXT)
done()
})
})
it('applies more than one middleware', (done) => {
run({
renderWithMiddleware: applyMiddleware(useBar(), useFoo()),
Component: (props) => <div>{props.fooFromContext} {props.barFromContext}</div>
}, (html) => {
expect(html).toContain(FOO_ROOT_CONTAINER_TEXT)
expect(html).toContain(BAR_ROOT_CONTAINER_TEXT)
done()
})
})
it('applies more middleware with only `getContainer`', (done) => {
run({
renderWithMiddleware: applyMiddleware(
useBar(),
useFoo(),
useBaz(BAZ_CONTAINER_TEXT)
),
Component: (props) => (
<div>
{props.fooFromContext}
{props.barFromContext}
{props.bazInjected}
</div>
)
}, (html) => {
expect(html).toContain(FOO_ROOT_CONTAINER_TEXT)
expect(html).toContain(BAR_ROOT_CONTAINER_TEXT)
expect(html).toContain(BAZ_CONTAINER_TEXT)
done()
})
})
it('applies middleware that only has `getContainer`', (done) => {
run({
renderWithMiddleware: applyMiddleware(
useBaz(BAZ_CONTAINER_TEXT)
),
Component: (props) => (
<div>{props.bazInjected}</div>
)
}, (html) => {
expect(html).toContain(BAZ_CONTAINER_TEXT)
done()
})
})
})