Reactjs 如何在React测试中测试、模拟上下文?
我有这样的代码:Reactjs 如何在React测试中测试、模拟上下文?,reactjs,unit-testing,jestjs,enzyme,Reactjs,Unit Testing,Jestjs,Enzyme,我有这样的代码: render () { ... const { exampleMethod, } = this.props; const { getPath } = this.context; const exampletData = exampleMethod(getPath()); ... } 在本例中,如何模拟、测试上下文?通常如何测试上下文?如果您使用的是酶,则可以通过传递一个选项来设置浅层渲染或完整dom渲染时的上下文: 例如: 如果要在节点上
render () {
...
const {
exampleMethod,
} = this.props;
const { getPath } = this.context;
const exampletData = exampleMethod(getPath());
...
}
在本例中,如何模拟、测试上下文?通常如何测试上下文?如果您使用的是酶,则可以通过传递一个选项来设置浅层渲染或完整dom渲染时的上下文:
例如:
如果要在节点上测试上下文,可以使用来自Ezyme的方法。如果使用Ezyme,则可以通过传递选项设置浅层渲染或完全dom渲染时的上下文:
例如:
如果要在节点上测试上下文,可以使用来自Ezyme的方法
import { shallow, mount } from 'enzyme';
// ......
const testContext = { getPath: () => 'foo' };
const shallowWrapper = shallow(<SomeComponent />, { context: testContext });
// or
const fullWrapper = mount(<SomeComponent />, { context: testContext });