简体   繁体   English

酶测试,上下文不渲染元素

[英]enzyme test, context not rendering elements

I am using enzyme to write my tests, before adding a context to my component which is the consumer component in this case, the snapshot rendered is not showing any elements and every time i add a dive() or shallow() I got the same error how can i access the elements while using context我正在使用酶来编写我的测试,在向我的consumer component在这种情况下是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>

Enzyme prior to v3.10.0 lacked support of React's new Context API . Enzyme之前v3.10.0缺乏支持阵营的新语境下的API

From the v3.10.0 Changelog :v3.10.0日志

shallow: Support rendering and dive()ing createContext() providers and consumers (#1966)浅层:支持渲染和潜水()创建上下文()提供者和消费者(#1966)

Make sure you are using enzyme v3.10.0 or later and enzyme-adapter-react-16 v1.15.1 or later.确保您使用的是enzyme v3.10.0或更高版本enzyme-adapter-react-16 v1.15.1或更高版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM