Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it('should pass context to children when not owner', function() {
var Parent = React.createClass({
render: function() {
return (
);
}
});
var Child = React.createClass({
childContextTypes: {
foo: React.PropTypes.string
},
getChildContext: function() {
return {
foo: 'bar'
};
},
render: function() {
return React.Children.only(this.props.children);
}
});
var Grandchild = React.createClass({
contextTypes: {
foo: React.PropTypes.string
it('supports this.context passed via getChildContext', function() {
class Bar extends React.Component {
render() {
return <div>;
}
}
Bar.contextTypes = { bar: React.PropTypes.string };
class Foo extends React.Component {
getChildContext() {
return { bar: 'bar-through-context' };
}
render() {
return ;
}
}
Foo.childContextTypes = { bar: React.PropTypes.string };
test(, 'DIV', 'bar-through-context');
});
</div>
it('renders based on context getInitialState', function() {
var Foo = React.createClass({
contextTypes: {
className: React.PropTypes.string
},
getInitialState() {
return { className: this.context.className };
},
render() {
return <span>;
}
});
var Outer = React.createClass({
childContextTypes: {
className: React.PropTypes.string
},
getChildContext() {
return { className: 'foo' };
},</span>
it('renders based on context getInitialState', function() {
var Foo = React.createClass({
contextTypes: {
className: React.PropTypes.string
},
getInitialState() {
return { className: this.context.className };
},
render() {
return <span>;
}
});
var Outer = React.createClass({
childContextTypes: {
className: React.PropTypes.string
},
getChildContext() {
return { className: 'foo' };
},
render() {
return ;
}
});
var container = document.createElement('div');
ReactDOM.render(, container);
expect(container.firstChild.className).toBe('foo');
});
</span>
it('renders based on context in the constructor', function() {
class Foo extends React.Component {
constructor(props, context) {
super(props, context);
this.state = { tag: context.tag, className: this.context.className };
}
render() {
var Tag = this.state.tag;
return ;
}
}
Foo.contextTypes = {
tag: React.PropTypes.string,
className: React.PropTypes.string
};
class Outer extends React.Component {
getChildContext() {
return { tag: 'span', className: 'foo' };
}
render() {
return ;
}
}
Outer.childContextTypes = {
tag: React.PropTypes.string,
className: React.PropTypes.string
};
test(, 'SPAN', 'foo');
},
getChildContext: function() {
return {
foo: 'bar'
};
},
render: function() {
return React.Children.only(this.props.children);
}
});
var Grandchild = React.createClass({
contextTypes: {
foo: React.PropTypes.string
},
render: function() {
return <div>{this.context.foo}</div>;
}
});
var component = renderIntoDocument();
expect(ReactDOM.findDOMNode(component).innerHTML).toBe('bar');
});
});