我有这样的代码:
render () {
...
const {
exampleMethod,
} = this.props;
const { getPath } = this.context;
const exampletData = exampleMethod(getPath());
...
}
我如何在这个例子中模拟,测试上下文?你如何测试上下文?
答案 0 :(得分:3)
如果您使用enzyme
,则可以通过传递选项在浅渲染或完整dom渲染时设置上下文。 (docs here):
例如:
import { shallow, mount } from 'enzyme';
// ......
const testContext = { getPath: () => 'foo' };
const shallowWrapper = shallow(<SomeComponent />, { context: testContext });
// or
const fullWrapper = mount(<SomeComponent />, { context: testContext });
如果要在节点上测试上下文,可以使用enzyme
中的.context()方法