简体   繁体   English

酶测试中缺少上下文功能

[英]Context missing functions in Enzyme test

So I'm using Enzyme to render a component with a bunch of context: 因此,我正在使用酶来渲染具有大量上下文的组件:

const surveysByType = "test";
const wrapper = shallow(
  <Dashboard routeParams={{}} />,
  {
      context: {
          assetUrl: () => {
              return "https://www.example.com/broken.gif"
          },
          executeAction: () => {},
          getStore: (whatever) => {
              return {
                  getState: () => {
                      return {
                          _surveysByType: surveysByType,
                      };
                  },
                  on: () => {
                      return {};
                  }
              }
          },
          router: {
              createHref: () => {},
              go: () => {},
              goBack: () => {},
              goForward: () => {},
              isActive: () => {},
              push: () => {},
              replace: () => {},
              setRouteLeaveHook: () => {},
          },
          siteUrl: () => {
              return "https://www.example.com"
          },
      }
  }
);

However, when I start trying to use said context, I find that only a single function is there, ie 但是,当我开始尝试使用上述上下文时,我发现那里只有一个功能,即

console.log(wrapper.context())

yields 产量

{ getStore: [Function: getStore] }

rather than all the functions. 而不是所有功能。 Indeed, if I try to do 确实,如果我尝试做

expect(wrapper.context().assetUrl())

I get an error: 我收到一个错误:

TypeError: wrapper.context(...).assetUrl is not a function TypeError:wrapper.context(...)。assetUrl不是一个函数

while this works fine: 虽然可以正常工作:

expect(wrapper.context().getStore().getState())    

What version of enzyme are you on? 您使用的是哪种酶?

Shallow takes an options param and there are some predefined options that it honors, but I don't see a context object: https://github.com/airbnb/enzyme/blob/master/src/shallow.js Shallow具有选项参数,并且有一些预定义的选项可以使用,但是我看不到上下文对象: https : //github.com/airbnb/enzyme/blob/master/src/shallow.js

How are you expecting the context object to be used exactly? 您如何期望上下文对象被正确使用?

You probably need to provide more code to help diagnose this. 您可能需要提供更多代码来帮助诊断。 Where is the console.log statement you quote above being called from, for example? 例如,上面引用的console.log语句在哪里调用?

Making some assumptions, you should check what Dashboard.contextTypes contains. 做一些假设,您应该检查Dashboard.contextTypes包含什么。 If it contains only getStore: PropTypes.function, that may explain why you only see that in the shallow render. 如果它仅包含getStore:PropTypes.function,则可以解释为什么只在浅渲染中看到它。 If the other properties are used by components that wrap dashboard (eg a wrapper), the code where console.log is being called may not be getting the whole context you're passing into shallow(), because as I understand it, React only provides the objects from context that are defined in the component's contextTypes static method. 如果包装仪表板的组件使用了其他属性(例如,包装器),则调用console.log的代码可能无法获取您要传递给shallow()的整个上下文,因为据我了解,这仅是React提供在组件的contextTypes静态方法中定义的上下文中的对象。

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

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