-
Notifications
You must be signed in to change notification settings - Fork 232
Replace core code with testHook from react-testing-library #2
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
Conversation
Looks good to me 👍
|
Dropped those tests in and they worked without any changes (other than formatting to match my prettier config). For changing the props, I added a new e.g. given the hook const sideEffect = { '1': false, '2': false }
function useSideEffect(id) {
useEffect(() => {
sideEffect[id] = true
return () => {
sideEffect[id] = false
}
}, [id])
} You can change the test('should update side effect when id changes', () => {
const { rerender, unmount } = testHook(
({ id }) => useSideEffect(id)),
{ initialProps: { id: '1' } }
)
expect(sideEffect['1']).toBe(true)
expect(sideEffect['2']).toBe(false)
rerender({ id: '2' })
expect(sideEffect['1']).toBe(false)
expect(sideEffect['2']).toBe(true)
unmount()
expect(sideEffect['1']).toBe(false)
expect(sideEffect['2']).toBe(false)
}) |
Hey @mpeyper thanks for putting this together, I started using this branch in a project of mine and all is 👍 at the moment. Minor naming comment: what do you think of calling the method |
This branch actually renamed it from I also find I would consider an alias export though as I do like your suggestion. |
Yeah, I saw that it was Maybe @alexkrolick or @donavon have stronger opinions on this? I anyway now do something like: function renderHook(initialProps = {}) {
return testHook(({ query }) => useMovieSearch(query), { initialProps })
} so, it's up to you 😄 |
with I'm going to change the export to be function testHook(...params) {
console.warn('testHook is deprecated. Please use renderHook instead.')
return renderHook(...params)
} |
Ok, I'm going to merge and publish this v0.3.0. I'm still not a fan of the |
So my review found that
useHook
andtestHook
offered practically identical features. ThetestHook
implementation is simpler than what I had inuseHook
so I took that as the base for moving forward.I made one change to
testHook
to allow the callback to take props (similar touseHook
). In my opinion, this offers a slightly nicer story for updating the values passed to the hook to test how the result changed when inputs changed. It's also backwards compatible with the oldtestHook
so whatever people were doing before will continue to work when migrating across.I'm also not a big fan of the
options.wrapper
usage. Having to wrap a context provider in order to pass thevalue
to it seemed clunky in practice. Even the tests inreact-testing-library
had to turn off lint warnings when using it. I'll sleep on it and see if I can't come up with something a little bit more ergonomic for it but I'll probably keepoptions.wrapper
regardless for backwards compatibility reasons, at least to begin with.Other than that, I'm pretty happy with this.
Resolves #1