-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #429 from hunterbmt/master
[New] `render`: add support for context
- Loading branch information
Showing
4 changed files
with
117 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import React from 'react'; | ||
import objectAssign from 'object.assign'; | ||
import cheerio from 'cheerio'; | ||
import { renderToStaticMarkup } from './react-compat'; | ||
|
||
/** | ||
* Renders a react component into static HTML and provides a cheerio wrapper around it. This is | ||
* somewhat asymmetric with `mount` and `shallow`, which don't use any external libraries, but | ||
* Cheerio's API is pretty close to what we actually want and has a significant amount of utility | ||
* that would be recreating the wheel if we didn't use it. | ||
* | ||
* I think there are a lot of good use cases to use `render` instead of `shallow` or `mount`, and | ||
* thus I'd like to keep this API in here even though it's not really "ours". | ||
* | ||
* @param node | ||
* @param options | ||
* @returns {Cheerio} | ||
*/ | ||
|
||
function createContextWrapperForNode(node, context, childContextTypes) { | ||
class ContextWrapper extends React.Component { | ||
getChildContext() { | ||
return context; | ||
} | ||
render() { | ||
return node; | ||
} | ||
} | ||
ContextWrapper.childContextTypes = childContextTypes; | ||
return ContextWrapper; | ||
} | ||
|
||
export default function render(node, options = {}) { | ||
if (options.context && (node.type.contextTypes || options.childContextTypes)) { | ||
const childContextTypes = objectAssign( | ||
{}, | ||
node.type.contextTypes || {}, | ||
options.childContextTypes, | ||
); | ||
const ContextWrapper = createContextWrapperForNode(node, options.context, childContextTypes); | ||
const html = renderToStaticMarkup(<ContextWrapper />); | ||
return cheerio.load(html).root(); | ||
} | ||
const html = renderToStaticMarkup(node); | ||
return cheerio.load(html).root(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import React from 'react'; | ||
import { expect } from 'chai'; | ||
import { describeWithDOM, describeIf } from './_helpers'; | ||
import { render } from '../src/'; | ||
import { REACT013 } from '../src/version'; | ||
|
||
describeWithDOM('render', () => { | ||
describeIf(!REACT013, 'context', () => { | ||
it('can pass in context', () => { | ||
const SimpleComponent = React.createClass({ | ||
contextTypes: { | ||
name: React.PropTypes.string, | ||
}, | ||
render() { | ||
return <div>{this.context.name}</div>; | ||
}, | ||
}); | ||
|
||
const context = { name: 'foo' }; | ||
const wrapper = render(<SimpleComponent />, { context }); | ||
expect(wrapper.text()).to.equal('foo'); | ||
}); | ||
it('can pass context to the child of mounted component', () => { | ||
const SimpleComponent = React.createClass({ | ||
contextTypes: { | ||
name: React.PropTypes.string, | ||
}, | ||
render() { | ||
return <div>{this.context.name}</div>; | ||
}, | ||
}); | ||
const ComplexComponent = React.createClass({ | ||
render() { | ||
return <div><SimpleComponent /></div>; | ||
}, | ||
}); | ||
|
||
const childContextTypes = { | ||
name: React.PropTypes.string.isRequired, | ||
}; | ||
const context = { name: 'foo' }; | ||
const wrapper = render(<ComplexComponent />, { context, childContextTypes }); | ||
expect(wrapper.children()).to.have.length(1); | ||
expect(wrapper.children().first().text()).to.equal('foo'); | ||
}); | ||
it('should not throw if context is passed in but contextTypes is missing', () => { | ||
const SimpleComponent = React.createClass({ | ||
render() { | ||
return <div>{this.context.name}</div>; | ||
}, | ||
}); | ||
|
||
const context = { name: 'foo' }; | ||
expect(() => render(<SimpleComponent />, { context })).to.not.throw(Error); | ||
}); | ||
}); | ||
}); |