我有一个组件SampleComponent,它可以挂载另一个“连接的组件”(即container)。当我尝试通过SampleComponent ing测试mount时(因为我需要componentDidMount),出现错误:



最好的测试方法是什么?

最佳答案

enzyme 的支架采用可选参数。您需要的两个是
options.context: (Object [optional]): Context to be passed into the componentoptions.childContextTypes: (Object [optional]): Merged contextTypes for all children of the wrapper您可以使用以下选项对象挂载SampleComponent:

const store = {
  subscribe: () => {},
  dispatch: () => {},
  getState: () => ({ ... whatever state you need to pass in ... })
}
const options = {
  context: { store },
  childContextTypes: { store: React.PropTypes.object.isRequired }
}

const _wrapper = mount(<SampleComponent {...defaultProps} />, options)

现在,您的SampleComponent将您提供的上下文传递给connected component

09-17 08:24