-
Notifications
You must be signed in to change notification settings - Fork 233
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
Add support for TypeScript #8
Add support for TypeScript #8
Conversation
test/typescript/renderHook.ts
Outdated
|
||
const ThemesContext = createContext(themes) | ||
|
||
const useTheme = (initialTheme: InitialTheme = DARK) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add minimum typing tests with the example code in README.md.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a big fan of that example, but I wanted something that used a few different hooks, so it became a little contrived. Happy for you to come up with something better if you want, or just keep it as is that's easier.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so it became a little contrived
I see.
Happy for you to come up with something better
I'll give it a try soon 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace useTheme
hook with useCounter
here 3cb8f92.
Also fix type definitions because I just realized they are broken.
@mpeyper |
Created minimum TS project with react-hooks-testing-library: https://github.com/otofu-square/react-hooks-testing-library-sandbox |
readonly rerender: (hookProps?: P) => void | ||
} | ||
|
||
export const testHook: typeof renderHook |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice
I'm reviewing this, but I'm far from a Typescript expert (I don't use it day to day, just created basic type definitions for some of my other libraries before, and not of them are as cool as what you've done here), so I'm sorry for any dumb questions. I'd also like it if someone that does use Typescript regularly could chip in and give me an indiation of whether the types here make sense. |
index.d.ts
Outdated
import { cleanup, act, RenderOptions, RenderResult } from 'react-testing-library' | ||
|
||
export function renderHook<P extends any, T extends (...args: [P]) => any>( | ||
callback: (_: P) => ReturnType<T>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where does ReturnType
come from?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind, I found something about it in the TS docs
This looks great to me. Waaaaaay better than anything I could have done myself. As I said before, I'd love a TS user to put a comment on here with approval before merging. Also can you please add yourself as a contributor to the README (by running this). I haven't got around to setting up a contributing document yet, so I don't blame you for not knowing this. If anyone TS users do review this, please submit a PR adding yourself as a contributer as well. |
Oh, sorry! Will work on it soon. |
Done.
Let me ask someone I know. |
readonly rerender: (hookProps?: P) => void | ||
} | ||
|
||
export const testHook: typeof renderHook |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'll be unnecessary someday, but it 's so smart 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, if this PR is the minimum typing or even if it does not 👍
I think that there is no problem to merge.
index.d.ts
Outdated
@@ -0,0 +1,18 @@ | |||
import { cleanup, act, RenderOptions, RenderResult } from 'react-testing-library' | |||
|
|||
export function renderHook<P extends any, R extends any>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is the difference between renderHook<P extends any, R extends any>
and renderHook<P, R>
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rednerHook<P, R>
is the same as renderHook<P extends Object, R extends Object>
.
Maybe it's more simple to use <P, R>
instead of <P extends any, R extends any>
since P
and R
will be inferred by giving callback
& initialProps
.
How do the typings look when the callback returns |
Currently the typing of
Maybe it's worth to test that |
current: R | ||
} | ||
readonly unmount: RenderResult['unmount'] | ||
readonly rerender: (hookProps?: P) => void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feels unnecessary to have ?
here. If the P
is inferred as undefined
, it should be ok. Otherwise, it should be required to pass the props if there were some initially to avoid surprising results.
readonly rerender: (hookProps?: P) => void | |
readonly rerender: (hookProps: P) => void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at the implementation of the rerender
function, it seems that hookProps.current
is assigned as a default value. (hookProps.current
is the same as initialProps
)
https://github.com/mpeyper/react-hooks-testing-library/blob/f7cd61035d8fb08cbfac376a5a8da1780708a64e/src/index.js#L26
As long as I see this, we can call rerender
function with no argument or initialProps
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, so it will use initialProps if none are passed. Makes sense I guess although it feels less explicit and might be confusing for tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct. Subsequent rerenders will use the previously supplied props until new props are supplied.
The idea is that the props don't change until the hook is rendered with new props, so hooks that use a conditional array (i.e. useEffect
, useLayoutEffect
, useMemo
and useCallback
) can be reliably tested.
@@ -26,6 +27,7 @@ | |||
"@babel/plugin-transform-modules-commonjs": "^7.2.0", | |||
"@babel/preset-env": "^7.3.4", | |||
"@babel/preset-react": "^7.0.0", | |||
"@types/react": "^16.8.5", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where does it depend on React types actually? I don't see any reference to it.
"@types/react": "^16.8.5", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's necessary to use React hooks in the typing test.
https://github.com/mpeyper/react-hooks-testing-library/blob/a16d3a303bfcfdd08ed007e524198b061b89d6ef/test/typescript/renderHook.ts#L1-L23
Co-Authored-By: otofu-square <[email protected]>
Ok, I feel like all comments have either been addressed or explained so I'm going to merge this. @FredyC, @tatsushitoji (and anyone else who may have contributed in some way to the PR), please open a PR adding yourself code review contributors by running |
Close #7
Introduce TypeScript type definitions 🎉
typescript
,typings-tester
,@types/react
)index.d.ts
to define typingsindex.d.ts
with typings-tester