呈现子组件时无法设置上下文
Unable to set context when rendering child components
我试图测试自定义Material-ui React组件与酶,但得到以下错误:
ERROR: 'Warning: Failed context type: Required context 'muiTheme' was not specified in 'ChildComponent'.
我所尝试的是根据这个设置一个上下文。我想要到达和测试的组件是一个子组件。
const root = shallow(<RootComponent />, {context: {muiTheme}, childContextTypes: {muiTheme: React.PropTypes.object}})
const child = root.find(ChildComponent)
child.render() // <--- this line is throwing the error
我不确定这是解决方案,但它离目标又近了一步。
const root = mount(<RootComponent />, {
context: {muiTheme},
childContextTypes: {muiTheme: React.PropTypes.object}
})
const child = root.find(ChildComponent)
注意,我用mount
代替shallow
。问题是,我不能再使用child.find({prop: 'value'})
-返回0项…
您需要提供<muiThemeProvider>
组件。
下面是一个关于如何做的例子:
import React from 'react';
import { mount } from 'enzyme';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import Input from './Input';
describe('<Input />', () => {
const mountWithTheme = (node) => mount(<MuiThemeProvider>{node}</MuiThemeProvider>);
it('calls componentDidMount', () => {
sinon.spy(Input.prototype, 'componentDidMount');
mountWithTheme(<Input />);
expect(Input.prototype.componentDidMount.calledOnce).to.equal(true);
});
});
相关文章:
- 如何设置EmberJS组件的上下文
- 关于设置超时/间隔上下文
- 为什么 context.restore() 不会将上下文的比例设置回 (1, 1)
- 如何在保留当前上下文的同时设置Timeout
- 如何设置javascript闭包的上下文
- JQuery widget:在子 widget 中设置上下文
- 上下文丢失.设置超时
- 为包定义的模板设置数据上下文
- Angular JS:如何为通过$http的所有调用设置上下文路径
- HTML5 画布:如何为上下文设置动画
- JavaScript 设置超时在 Android 上下文切换上
- TinyMCE onPaste 不设置内容,使用上下文菜单粘贴不会触发更改事件
- Backbone.js,无法在回调上设置上下文
- 为什么我不能设置上下文.在我创建我的画布后立即填充样式
- 呈现子组件时无法设置上下文
- 在设置上下文时,使用bind的替代方法是什么?
- 'this'在Array.prototype.slice.call(arguments)中设置上下文
- 如何在模板事件函数中设置上下文数据
- 设置上下文的不同方式
- 在dojo中动态设置上下文菜单的标签