Custom renderHook implementation for testing custom hooks#5381
Custom renderHook implementation for testing custom hooks#5381jtoar merged 14 commits intoredwoodjs:mainfrom
Conversation
✅ Deploy Preview for redwoodjs-docs ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
|
This looks great to me Curtis 🙂
I'm all for it, but let me check with the team |
|
@cjreimer We all agree. This would be a nice addition to RW. Please go-ahead 🙂 |
|
Great, thanks @Tobbe! |
|
OK, I've made progress, but I could use some help from someone more familiar with the redwood jest configuration between packages. I've tried to set up a test that is similar to what I'm putting in the documentation, but I'm getting the following error: Would someone be able to help advise if we can modify our jest configuration to run this test within the |
|
Hi @cjreimer, thank you for your contribution thus far! So what I think is happening is the new test, has a transitive dependency on
I have gotten it a little bit further by using a valid path in I am working with the other core team members to figure out how best to advise we unblock you. |
|
Thanks @virtuoushub! I appreciate the help! I've cleaned up the code and docs, and other than the testing block, the code is ready for review. I'll note that one other option is to just omit the test. The base code addition is pretty straightforward. I'll leave that up to the core team to decide. |
|
@cjreimer thanks! I ran into this while writing tests for the example store and had to make a new react component just so I could call the hook inside it, so I definitely see the use case here. I had a quick look and I'll keep reviewing, but have a question: it looks like It's in their docs too: https://testing-library.com/docs/react-testing-library/api/#renderhook. The package you added is in the testing-library organization, and is published under the testing-library namespace, so it's official. But do you know what the difference is, or why we should prefer one over the other? |
|
Hey @jtoar, great catch! I looked and I believe the answer is in: It looks like they are in the process of depreciating the |
|
@jtoar, Sorry, I looked a bit further and they are dropping support for React 17 in version 13 of the See: I'll make a comment in the code. |
I think this was an accident
jtoar
left a comment
There was a problem hiding this comment.
Thanks @cjreimer, this looks good! Tried it out on the example store and it streamlined writing a test where I was using a custom hook.
I'm with you and @virtuoushub—we can get rid of the test. The logic is virtually the same as the customRender, which we don't have a specific test for, but we do test the MockProviders which are used in the wrapper of both functions. It's important that those work.
|
Thanks @jtoar! I appreciate your cleanup and I removed the test. It should be ready to go. |
|
Thanks @cjreimer! Had to make one small last change to get storybook working. We just had to be a little more specific with the import or else |
|
Sounds good @jtoar ! Thanks! |
Status:
The purpose of this PR is to facilitate testing of custom react hooks within redwood.
@testing-library/react-hookshas arenderHookfunction appropriate for this. However, when callingrenderHook, it does not by default have theMockProviderswrapper and thus any typical redwood testing features, such as mocking of graphQL queries and mutations, fails. It is possible to add this manually, but it is already added automatically for the Redwood user for the similarrenderfunctionRedwood currently addresses the render function for testing well by providing a
customRenderfunction that includes the required wrapper and aliasing it to be calledrender.As it took me a bit to chase this all down when I encountered it, this PR proposes a similar implementation for renderHook. A 'customRenderHook' function with the required wrapper is created and it is aliased to 'renderHook'. Note that this does require the addition of
@testing-library/react-hookstopackage.json.I would love to get feedback to confirm we would like to add this to Redwood before I complete this PR. If we decide not to add this, then documentation on how to do this manually would be appropriate.