-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Custom Hooks with useContext #283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I'm in favor. What do you think @donavon? |
Actually nevermind. I honestly think that I suggest just going with the alternative you've described. That's a lot simpler IMO... |
Yeah the only one that seems general enough to maybe include would be the fixture idea in option 2 which is something that could also be added to |
Adding a fixture to |
I recommend you give this a quick watch: https://www.youtube.com/watch?v=0e6WCQYg5tU&index=22&list=PLV5CVI1eNcJgCrPH_e6d57KRUTiDZgs0u Then I recommend you create a |
Thanks for sharing that video. I ended up doing this for my helper which I'm pretty happy with—it's closer to the existing import {NameContext} from '../react-context-hook'
function TestHook({ callback }) {
callback()
return null
}
export function testHookWithNameContext(callback, { value, ...options }) {
render(
<NameContext.Provider value={value}>
<TestHook callback={callback} />
</NameContext.Provider>,
options,
)
} |
I think maybe this would be good in: https://github.com/kentcdodds/react-testing-library-examples What do you think @alexkrolick? |
Yes, with the addition of some tests showing how to use it. Probably wouldn't want to even export the Note that this is pretty much the recommended approach for anything requiring context. |
Would you be willing to open a pull request for that @ajlende? You could do it in the browser :) |
This adds an example for how to test custom hooks that use `useContext()`. React and react-testing-library were updated to [The One With Hooks](https://reactjs.org/blog/2019/02/06/react-v16.8.0.html). Addresses testing-library/react-testing-library#283
I want to add my two cents here. I believe that Context will become the go-to solution for mocking stuff. Today, I've tweeted the following. import React, { useContext } from 'react'
const context = React.createContext(() => new Date())
export function useNow() {
return useContext(context)
}
export const FixedNowProvider: React.FC<{ dateTime: Date }> = ({
dateTime,
children,
}) => {
return React.createElement(
context.Provider,
{ value: () => dateTime },
children,
)
} https://twitter.com/danielk_cz/status/1094908590836596737 Point is that using more of such providers will make tests rather bloated. I am not saying it should be a part of Hopefully, it will get even easier with RFC for Context.write. |
I'm somewhat I'm favor of adding a "wrapper [component]" option to render and testHook. The problem with wrapping render to provide your required helpers is you also need to reimplement rerender and that involves both "render"and "act". @kentcdodds what do you think? |
Why do you need to reimplement rerender? Wouldn't have to do that if you did this right? #283 (comment) |
Won't the rerender returned by the wrapped render not include the wrapping component? |
Oh, nevermind, I see now.... Hmmm, yeah. That rerender is a beast and pretty annoying to have to re-implement. Ok, I'm in favor. But for the record I really want to avoid making |
And by that I mean: It's fairly simple and I don't think it should become complex. |
Idea:
Custom
// define
const customTestHook = (callback, {wrapperProps}) =>
testHook(callback, {
wrapper: props => <NameContext.Provider {...wrapperProps} {...props} />
})
// use
const [a, b] = customTestHook(myCustomHook, {wrapperProps: {value: "ABC"}}) Custom
// define
const customRender = (component) =>
render(component, {wrapper: SomeContext.Provider})
// use
const {rerender} = customRender(<Abc />)
// define
const customRender = (component, {wrapperProps}) =>
render(component, {
// "props" for the wrapper option is probably just `{ children }`
wrapper: props => <NameContext.Provider {...wrapperProps} {...props} />
})
// use
const {rerender} = customRender(<Abc />, {wrapperProps: {name: 'Foo'}}) |
I'm in favor 👍 |
Definitely, like that idea as it would allow to supply more providers if needed or actually any other environment stuff that might be needed by a hook. Btw, a slight correction of your example, getting a result doesn't work the way you have here. It was rather confusing for me at first too... // define
const testHookWithNameContext = (callback, wrapperProps) => {
let result
testHook(() => {
result = callback()
}, {
wrapper: props => <NameContext.Provider {...wrapperProps} {...props} />
})
return result
}
// use
const [a, b] = testHookWithNameContext(myCustomHook, {value: "ABC"}) Implementation note: Please update TypeScript declaration accordingly. interface TestHookOptions {
wrapper: React.FunctionalComponent
} |
This can be also closed now, implemented in #296 |
…rary#283) Co-authored-by: Greg Shtilman <gshtilma@yahoo-inc.com>
This adds an example for how to test custom hooks that use `useContext()`. React and react-testing-library were updated to [The One With Hooks](https://reactjs.org/blog/2019/02/06/react-v16.8.0.html). Addresses testing-library/react-testing-library#283
This adds an example for how to test custom hooks that use `useContext()`. React and react-testing-library were updated to [The One With Hooks](https://reactjs.org/blog/2019/02/06/react-v16.8.0.html). Addresses testing-library/react-testing-library#283
First, I'd like to thank you for getting
react-testing-library
updated so quickly after the release of hooks!Describe the feature you'd like:
I'd like to add an option to
testHook()
for testing custom hooks that useuseContext()
and need to be wrapped in theProvider
.Below is a minimal (albeit silly) example of how
useContext()
could be used in a custom hook. I've useduseContext()
in a similar manner to this in more complicated custom hooks.Suggested implementation:
[Option 1] Providing the Context
[Option 2] Providing a Fixture
[Option 3] Providing a Component and Props
[Option 4] Some variation on the options above
I like the conciseness of 1 and 3, but 2 seems like it would probably be the most versatile. These were just my initial thoughts, so there's probably something that will work better than any of these.
Describe alternatives you've considered:
I'm doing something similar to this right now, but I could probably update my helper to work more like how
testHook
is implemented.Teachability, Documentation, Adoption, Migration Strategy:
Examples from above can be used. Adding the additional argument would be a minor release probably, so existing tests wouldn't need to be updated.
The text was updated successfully, but these errors were encountered: