mzgoddard / preact-render-spy

Render preact components with access to the produced virtual dom for testing.
https://www.npmjs.com/package/preact-render-spy
ISC License
178 stars 24 forks source link

Testing with HOC and shallow #84

Closed NoxWings closed 5 years ago

NoxWings commented 6 years ago

Hi everyone!

I'm working with preact and I'm currently doing a dependency injector for some config we have in our app. I decided to use preact-context for the new api and I'm using it through a higher order component that consumes the dependencies and injects it onto the wrapped component.

The problem I'm facing is not testing the HOC itself, but rather, when we test a wrapped component that is part of another component. We usually use shallow for testing specially on the higher level components like the page containers. The problem I'm facing is that now we have a another layer with each component created with the HOC. I've read that enzyme has a dive function that allows you to go further down the hierarchy to the wrapped component even though it is not covered by the initial shallow.

Is there something similar here or any other way to replicate that behavior here?

jahilldev commented 5 years ago

@NoxWings You could switch to use something like:

deep(
  <HOC>
    <Wrapped />
  </HOC>,
  { depth: 2 }
)

You're still restricting the depth (like shallow()) but allowing your wrapped component to run.

NoxWings commented 5 years ago

@jhdevuk thank you for your suggestion. That's actually what we are using and it works quite well for the purpose. Sorry I just forgot I wrote this question here.