Add support for TypeScript#8
Add support for TypeScript#8mpeyper merged 10 commits intotesting-library:masterfrom otofu-square:add-support-for-typescript
Conversation
test/typescript/renderHook.ts
Outdated
|
|
||
| const ThemesContext = createContext(themes) | ||
|
|
||
| const useTheme = (initialTheme: InitialTheme = DARK) => { |
There was a problem hiding this comment.
Add minimum typing tests with the example code in README.md.
There was a problem hiding this comment.
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.
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.
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 |
|
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.
Where does ReturnType come from?
There was a problem hiding this comment.
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.
It'll be unnecessary someday, but it 's so smart 👍
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.
what is the difference between renderHook<P extends any, R extends any> and renderHook<P, R>?
There was a problem hiding this comment.
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.
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.
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.
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.
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.
| "@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.
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.
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 <otofu.square@gmail.com>
|
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.tsto define typingsindex.d.tswith typings-tester