Skip to content

ldclient-react withLDConsumer does not return a React.Component, hence does not work with Enzyme's dive() #145

@shykln

Description

@shykln

Hi,

I'm trying to add ldclient-react to my project and the application runs well. But my unit test failed. I wrote the unit test with jest + Enzyme.

TypeError: ShallowWrapper::dive() can only be called on components

      54 |       .dive()
      55 |       .dive()
    > 56 |       .dive();
         |        ^
      57 |     expect(wrapper.find(GlobalFilter).length).toBe(1);
      58 |   });

The reason why it failed is because that the withLDConsumer method returns (props: P) => JSX.Element, and this return type is not a Component.

And dive() only works with Component. Hence dive() throws exception.

https://airbnb.io/enzyme/docs/api/ShallowWrapper/dive.html

Can you change the return type of LDConsumer to Component? I think based on the definition of React, an HOC should return a new Component. https://reactjs.org/docs/higher-order-components.html
But LDConsumer returns a (props: P) => JSX.Element which is not a Component.

I'm new to React. Could there be a way to work around this, or could we get a code change here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions