React-hooks-testing-library: How to wrap a hook with a Provider?

Created on 28 Mar 2019  路  3Comments  路  Source: testing-library/react-hooks-testing-library

What is your question:


How can I wrap a hook with a necessary Provider?

question

Most helpful comment

Hi @stramel, you can use the wrapper option of renderHook to wrap the test component in anything you want. An example of using a context Provider can be seen in our test suite

const wrapper = ({ children }) => (
  <YourContext.Provider value="some context">{children}</YourContext.Provider>
)

const { result } = renderHook(() => useYourHook(), { wrapper })

Please feel free to submit a PR improving the docs as some examples are definitely missing.

All 3 comments

Hi @stramel, you can use the wrapper option of renderHook to wrap the test component in anything you want. An example of using a context Provider can be seen in our test suite

const wrapper = ({ children }) => (
  <YourContext.Provider value="some context">{children}</YourContext.Provider>
)

const { result } = renderHook(() => useYourHook(), { wrapper })

Please feel free to submit a PR improving the docs as some examples are definitely missing.

@mpeyper Thank you for the answer. This is indeed what I was looking for. I will try to get a PR out for this in the near future.

Thanks. I've added an item to #19 for wrapper documentation. I'll close this one now and any PR can be made against that.

Was this page helpful?
0 / 5 - 0 ratings