Skip to content
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

Merged
merged 10 commits into from
Mar 3, 2019
19 changes: 19 additions & 0 deletions index.d.ts
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

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 👍


export { cleanup, act }
43 changes: 40 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Copy link
Contributor

@danielkcz danielkcz Mar 2, 2019

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.

Suggested change
"@types/react": "^16.8.5",

Copy link
Contributor Author

@otofu-square otofu-square Mar 3, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"all-contributors-cli": "^6.1.2",
"babel-eslint": "^10.0.1",
"babel-plugin-module-resolver": "^3.2.0",
Expand All @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions test/typescript.test.js
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')
})
})
60 changes: 60 additions & 0 deletions test/typescript/renderHook.ts
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) => {
Copy link
Contributor Author

@otofu-square otofu-square Feb 28, 2019

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.

Copy link
Member

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.

Copy link
Contributor Author

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 👍

Copy link
Contributor Author

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.

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
}
10 changes: 10 additions & 0 deletions test/typescript/tsconfig.json
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"]
}
}
}