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