2
votes

I'm trying to test a component that wraps a connected component. So I have (simplified):

const InnerComponent = () => {
  return (
    <p>Hi!</p>
  )
}

const InnerComponentWrapper = connect()(InnerComponent)

const OuterComponent = () => {
  return (
    <div>
      <InnerComponentWrapper>
    </div>
  )
}

(Obviously there's more to these components, including state related stuff)

Now I'm trying to simply test that InnerComponent (or InnerComponentWrapper) is in OuterComponent. I'm using jest+enzyme. When I render OuterComponent (shallow or mount) all I can find in OuterComponent is an element with the tag of 'Connect' which doesn't have anything on it I can find that can tell me what element it's wrapping.

What I tried:

const enzymeWrapper = shallow(<OuterComponent />)
expect(enzymeWrapper.find('Connect').length).toBe(1) // pass
expect(enzymeWrapper.find('Connect(InnerComponentWrapper)').length).toBe(1) // fail
expect(enzymeWrapper.find('InnerComponentWrapper').length).toBe(1) // fail
expect(enzymeWrapper.find('InnerComponent').length).toBe(1) // fail

Thanks!

2

2 Answers

3
votes

For me, this works (your 2nd attempt, marked as not working… perhaps you got some detail wrong, or things got fixed in a newer version?).

const enzymeWrapper = shallow(<OuterComponent />)
expect(enzymeWrapper.find('Connect(InnerComponentWrapper)')

Also, the Enzyme bug tracker points to this solution.

0
votes

I found a solution, there could definitely be a better one, but this hacky one will do for now:

const enzymeWrapper = shallow(<OuterComponent />)
expect(enzymeWrapper.find('Connect').node.type.WrappedComponent({})).toEqual(InnerComponent({}))