Skip to content

Commit

Permalink
added context.router
Browse files Browse the repository at this point in the history
refs #2646
  • Loading branch information
ryanflorence committed Dec 5, 2015
1 parent d885792 commit bfd571e
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 6 deletions.
23 changes: 17 additions & 6 deletions modules/RoutingContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,25 @@ import getRouteParams from './getRouteParams'

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

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

getChildContext() {
const { history, location } = this.props
return { history, location }
const router = {
push(...args) {
history.push(...args)
},
replace(...args) {
history.replace(...args)
},
addRouteLeaveHook(...args) {
return history.listenBeforeLeavingRoute(...args)
},
isActive(...args) {
return history.isActive(...args)
}
}
return { history, location, router }
}

createElement(component, props) {
Expand Down Expand Up @@ -94,7 +104,8 @@ RoutingContext.defaultProps = {

RoutingContext.childContextTypes = {
history: object.isRequired,
location: object.isRequired
location: object.isRequired,
router: object.isRequired
}

export default RoutingContext
101 changes: 101 additions & 0 deletions modules/__tests__/RoutingContext-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import expect from 'expect'
import React from 'react'
import { render, unmountComponentAtNode } from 'react-dom'
import RoutingContext from '../RoutingContext'
import match from '../match'

describe('For Real RoutingContext', () => {
let node, routes, context, history

beforeEach(() => {
node = document.createElement('div')
history = { push() {}, replace() {} }

class Component extends React.Component {
constructor(props, ctx) {
super(props, ctx)
context = ctx
}
render() { return null }
}

Component.contextTypes = {
router: React.PropTypes.object.isRequired
}

routes = { path: '/', component: Component }
})

afterEach(() => unmountComponentAtNode(node))

function renderTest(done) {
match({ location: '/', routes }, (err, redirect, renderProps) => {
render(<RoutingContext {...renderProps} history={history} />, node)
done()
})
}

describe('2.0', () => {
it('exports only `router` to context')
})

it('exports a `router` object to routing context', (done) => {
renderTest(() => {
expect(context.router).toExist()
done()
})
})

describe('interaction with history', () => {
it('proxies calls to `push` to `props.history`', (done) => {
const args = [ 1, 2, 3 ]
history.push = (...params) => {
expect(params).toEqual(args)
done()
}
renderTest(() => {
context.router.push(...args)
})
})

it('proxies calls to `replace` to `props.history`', (done) => {
const args = [ 1, 2, 3 ]
history.replace = (...params) => {
expect(params).toEqual(args)
done()
}
renderTest(() => {
context.router.replace(...args)
})
})

it('proxies calls to `addRouteLeaveHook` to `props.history`', (done) => {
const args = [ 1, 2, 3 ]
const retVal = function () {}
history.listenBeforeLeavingRoute = (...params) => {
expect(params).toEqual(args)
return retVal
}
renderTest(() => {
const remove = context.router.addRouteLeaveHook(...args)
expect(remove).toBe(retVal)
done()
})
})

it('proxies calls to `isActive` to `props.history`', (done) => {
const args = [ 1, 2, 3 ]
const retVal = function () {}
history.isActive = (...params) => {
expect(params).toEqual(args)
return retVal
}
renderTest(() => {
const isActive = context.router.isActive(...args)
expect(isActive).toBe(retVal)
done()
})
})
})

})

0 comments on commit bfd571e

Please sign in to comment.