|
| 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