-
Notifications
You must be signed in to change notification settings - Fork 234
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 3 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,19 @@ | ||
import { cleanup, act, RenderOptions, RenderResult } from 'react-testing-library' | ||
|
||
export function renderHook<T extends (...args: any[]) => any>( | ||
callback: T, | ||
options?: { | ||
initialProps?: Parameters<T>[0] | ||
options?: RenderOptions | ||
} | ||
): { | ||
readonly result: { | ||
current: ReturnType<T> | ||
} | ||
readonly unmount: RenderResult['unmount'] | ||
readonly rerender: (hookProps?: Parameters<T>[0]) => 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. 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,60 @@ | ||
import { useState, createContext, useContext, useMemo } from 'react' | ||
import { renderHook } from 'react-hooks-testing-library' | ||
|
||
const DARK: 'dark' = 'dark' | ||
const LIGHT: 'light' = 'light' | ||
|
||
type InitialTheme = typeof DARK | typeof LIGHT | undefined | ||
|
||
const themes = { | ||
light: { primaryLight: '#FFFFFF', primaryDark: '#000000' }, | ||
dark: { primaryLight: '#000000', primaryDark: '#FFFFFF' } | ||
} | ||
|
||
const ThemesContext = createContext(themes) | ||
|
||
const useTheme = (initialTheme: InitialTheme = DARK) => { | ||
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. Add minimum typing tests with the example code in README.md. 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. 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 commentThe reason will be displayed to describe this comment to others. Learn more.
I see.
I'll give it a try soon 👍 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. Replace |
||
const themes = useContext(ThemesContext) | ||
const [theme, setTheme] = useState(initialTheme) | ||
const toggleTheme = () => { | ||
setTheme(theme === 'light' ? 'dark' : 'light') | ||
} | ||
return useMemo(() => ({ ...themes[theme], toggleTheme }), [theme]) | ||
} | ||
|
||
type InitialProps = { initialTheme: InitialTheme } | ||
|
||
function checkTypesWithNoInitialProps() { | ||
const { result, unmount, rerender } = renderHook(() => useTheme()) | ||
|
||
// check types | ||
const _result: { | ||
current: { | ||
primaryDark: string | ||
primaryLight: string | ||
toggleTheme: () => void | ||
} | ||
} = result | ||
const _unmount: () => boolean = unmount | ||
const _rerender: () => void = rerender | ||
} | ||
|
||
function checkTypesWithInitialProps() { | ||
const { result, unmount, rerender } = renderHook( | ||
({ initialTheme }: InitialProps) => useTheme(initialTheme), | ||
{ | ||
initialProps: { initialTheme: DARK } | ||
} | ||
) | ||
|
||
// check types | ||
const _result: { | ||
current: { | ||
primaryDark: string | ||
primaryLight: string | ||
toggleTheme: () => void | ||
} | ||
} = result | ||
const _unmount: () => boolean = unmount | ||
const _rerender: (_: InitialProps) => 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.
nice