我正在使用 enzyme 编写我的测试,在向我的组件(在本例中是 consumer component
)添加上下文之前,渲染的快照没有显示任何元素,每次我添加一个 dive()
或 shallow()
我得到了同样的错误
我如何在使用上下文时访问元素
const wrapper = shallow(
<MyComponent {...initialProps} store={mockStore(initialState)} />,
{context},
);
const contents = wrapper
.dive() // dive in connect
.dive() // dive in withLogss
.dive() // dive in Motion
.find("MyComponent")
.dive(); // dive in Host
expect(contents.context()).toEqual({myBoolean: true});
it("should render properly", () => {
expect(contents).toMatchSnapshot();
});
Result:
<ContextConsumer>
<Component />
</ContextConsumer>
最佳答案
v3.10.0
之前的
enzyme
lacked support of React's new Context API .
shallow: Support rendering and dive()ing createContext() providers and consumers (#1966)
确保您使用的是enzyme
v3.10.0
或更高版本和 enzyme-adapter-react-16
v1.15.1
或更高版本。
关于reactjs - enzyme 测试,上下文不渲染元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52830983/