【发布时间】:2019-12-05 23:53:26
【问题描述】:
我正在使用酶来编写我的测试,在向我的组件添加上下文之前,在这种情况下是 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>
【问题讨论】:
-
您使用的是浅渲染,请尝试使用 mount 代替。
标签: reactjs testing jestjs enzyme