Nested components testing with Enzyme inside of React & Redux

后端 未结 6 1113
甜味超标
甜味超标 2020-12-24 05:55

I have a component SampleComponent that mounts another \"connected component\" (i.e. container). When I try to test SampleComponent by

6条回答
  •  死守一世寂寞
    2020-12-24 06:46

    Enzyme's mount takes optional parameters. The two that are necessary for what you need are

    options.context: (Object [optional]): Context to be passed into the component

    options.childContextTypes: (Object [optional]): Merged contextTypes for all children of the wrapper You would mount SampleComponent with an options object like so:

    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(, options)
    

    Now your SampleComponent will pass the context you provided down to the connected component.

提交回复
热议问题