-
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
Changes from all commits
7f7ca2b
ae2d990
bbfb0ff
1ba1bd9
eaee298
3cb8f92
36604a1
2bdb78a
b2478e8
a16d3a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { cleanup, act, RenderOptions, RenderResult } from 'react-testing-library' | ||
|
||
export function renderHook<P, R>( | ||
callback: (props: P) => R, | ||
options?: { | ||
initialProps?: P | ||
} & RenderOptions | ||
): { | ||
readonly result: { | ||
current: R | ||
} | ||
readonly unmount: RenderResult['unmount'] | ||
readonly rerender: (hookProps?: P) => void | ||
} | ||
|
||
export const testHook: typeof renderHook | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'll be unnecessary someday, but it 's so smart 👍 |
||
|
||
export { cleanup, act } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -3,6 +3,7 @@ | |||
"version": "0.3.4", | ||||
"description": "Simple component wrapper for testing React hooks", | ||||
"main": "lib/index.js", | ||||
"typings": "./index.d.ts", | ||||
"author": "Michael Peyper", | ||||
"repository": { | ||||
"type": "git", | ||||
|
@@ -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 commentThe 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.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's necessary to use React hooks in the typing test. |
||||
"all-contributors-cli": "^6.1.2", | ||||
"babel-eslint": "^10.0.1", | ||||
"babel-plugin-module-resolver": "^3.2.0", | ||||
|
@@ -39,7 +41,9 @@ | |||
"prettier-eslint": "^8.8.2", | ||||
"prettier-eslint-cli": "^4.7.1", | ||||
"react": "^16.8.3", | ||||
"react-dom": "^16.8.3" | ||||
"react-dom": "^16.8.3", | ||||
"typescript": "^3.3.3333", | ||||
"typings-tester": "^0.3.2" | ||||
}, | ||||
"peerDependencies": { | ||||
"react": "^16.8.0", | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { checkDirectory } from 'typings-tester' | ||
|
||
describe('TypeScript definitions', function() { | ||
it('should compile against index.d.ts', () => { | ||
checkDirectory(__dirname + '/typescript') | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { useState, useCallback, useEffect } from 'react' | ||
import { renderHook } from 'react-hooks-testing-library' | ||
|
||
const useCounter = (initialCount: number = 0) => { | ||
const [count, setCount] = useState(initialCount) | ||
const incrementBy = useCallback( | ||
(n: number) => { | ||
setCount(count + n) | ||
}, | ||
[count] | ||
) | ||
const decrementBy = useCallback( | ||
(n: number) => { | ||
setCount(count - n) | ||
}, | ||
[count] | ||
) | ||
return { | ||
count, | ||
incrementBy, | ||
decrementBy | ||
} | ||
} | ||
|
||
function checkTypesWithNoInitialProps() { | ||
const { result, unmount, rerender } = renderHook(() => useCounter()) | ||
|
||
// check types | ||
const _result: { | ||
current: { | ||
count: number | ||
incrementBy: (_: number) => void | ||
decrementBy: (_: number) => void | ||
} | ||
} = result | ||
const _unmount: () => boolean = unmount | ||
const _rerender: () => void = rerender | ||
} | ||
|
||
function checkTypesWithInitialProps() { | ||
const { result, unmount, rerender } = renderHook(({ count }) => useCounter(count), { | ||
initialProps: { count: 10 } | ||
}) | ||
|
||
// check types | ||
const _result: { | ||
current: { | ||
count: number | ||
incrementBy: (_: number) => void | ||
decrementBy: (_: number) => void | ||
} | ||
} = result | ||
const _unmount: () => boolean = unmount | ||
const _rerender: (_?: { count: number }) => void = rerender | ||
} | ||
|
||
function checkTypesWhenHookReturnsVoid() { | ||
const { result, unmount, rerender } = renderHook(() => useEffect(() => {})) | ||
|
||
// check types | ||
const _result: { | ||
current: void | ||
} = result | ||
const _unmount: () => boolean = unmount | ||
const _rerender: () => void = rerender | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"compilerOptions": { | ||
"lib": ["es2015", "dom"], | ||
"strict": true, | ||
"baseUrl": "../../", | ||
"paths": { | ||
"react-hooks-testing-library": ["index.d.ts"] | ||
} | ||
} | ||
} |
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 theP
is inferred asundefined
, it should be ok. Otherwise, it should be required to pass the props if there were some initially to avoid surprising results.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 thathookProps.current
is assigned as a default value. (hookProps.current
is the same asinitialProps
)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 orinitialProps
.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
anduseCallback
) can be reliably tested.