Skip to content

Commit bfd571e

Browse files
committed
added context.router
refs remix-run#2646
1 parent d885792 commit bfd571e

File tree

2 files changed

+118
-6
lines changed

2 files changed

+118
-6
lines changed

modules/RoutingContext.js

+17-6
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,25 @@ import getRouteParams from './getRouteParams'
55

66
const { array, func, object } = React.PropTypes
77

8-
/**
9-
* A <RoutingContext> renders the component tree for a given router state
10-
* and sets the history object and the current location in context.
11-
*/
128
class RoutingContext extends Component {
139

1410
getChildContext() {
1511
const { history, location } = this.props
16-
return { history, location }
12+
const router = {
13+
push(...args) {
14+
history.push(...args)
15+
},
16+
replace(...args) {
17+
history.replace(...args)
18+
},
19+
addRouteLeaveHook(...args) {
20+
return history.listenBeforeLeavingRoute(...args)
21+
},
22+
isActive(...args) {
23+
return history.isActive(...args)
24+
}
25+
}
26+
return { history, location, router }
1727
}
1828

1929
createElement(component, props) {
@@ -94,7 +104,8 @@ RoutingContext.defaultProps = {
94104

95105
RoutingContext.childContextTypes = {
96106
history: object.isRequired,
97-
location: object.isRequired
107+
location: object.isRequired,
108+
router: object.isRequired
98109
}
99110

100111
export default RoutingContext
+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import expect from 'expect'
2+
import React from 'react'
3+
import { render, unmountComponentAtNode } from 'react-dom'
4+
import RoutingContext from '../RoutingContext'
5+
import match from '../match'
6+
7+
describe('For Real RoutingContext', () => {
8+
let node, routes, context, history
9+
10+
beforeEach(() => {
11+
node = document.createElement('div')
12+
history = { push() {}, replace() {} }
13+
14+
class Component extends React.Component {
15+
constructor(props, ctx) {
16+
super(props, ctx)
17+
context = ctx
18+
}
19+
render() { return null }
20+
}
21+
22+
Component.contextTypes = {
23+
router: React.PropTypes.object.isRequired
24+
}
25+
26+
routes = { path: '/', component: Component }
27+
})
28+
29+
afterEach(() => unmountComponentAtNode(node))
30+
31+
function renderTest(done) {
32+
match({ location: '/', routes }, (err, redirect, renderProps) => {
33+
render(<RoutingContext {...renderProps} history={history} />, node)
34+
done()
35+
})
36+
}
37+
38+
describe('2.0', () => {
39+
it('exports only `router` to context')
40+
})
41+
42+
it('exports a `router` object to routing context', (done) => {
43+
renderTest(() => {
44+
expect(context.router).toExist()
45+
done()
46+
})
47+
})
48+
49+
describe('interaction with history', () => {
50+
it('proxies calls to `push` to `props.history`', (done) => {
51+
const args = [ 1, 2, 3 ]
52+
history.push = (...params) => {
53+
expect(params).toEqual(args)
54+
done()
55+
}
56+
renderTest(() => {
57+
context.router.push(...args)
58+
})
59+
})
60+
61+
it('proxies calls to `replace` to `props.history`', (done) => {
62+
const args = [ 1, 2, 3 ]
63+
history.replace = (...params) => {
64+
expect(params).toEqual(args)
65+
done()
66+
}
67+
renderTest(() => {
68+
context.router.replace(...args)
69+
})
70+
})
71+
72+
it('proxies calls to `addRouteLeaveHook` to `props.history`', (done) => {
73+
const args = [ 1, 2, 3 ]
74+
const retVal = function () {}
75+
history.listenBeforeLeavingRoute = (...params) => {
76+
expect(params).toEqual(args)
77+
return retVal
78+
}
79+
renderTest(() => {
80+
const remove = context.router.addRouteLeaveHook(...args)
81+
expect(remove).toBe(retVal)
82+
done()
83+
})
84+
})
85+
86+
it('proxies calls to `isActive` to `props.history`', (done) => {
87+
const args = [ 1, 2, 3 ]
88+
const retVal = function () {}
89+
history.isActive = (...params) => {
90+
expect(params).toEqual(args)
91+
return retVal
92+
}
93+
renderTest(() => {
94+
const isActive = context.router.isActive(...args)
95+
expect(isActive).toBe(retVal)
96+
done()
97+
})
98+
})
99+
})
100+
101+
})

0 commit comments

Comments
 (0)