Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bce403f

Browse files
authored
Merge pull request #8 from otofu-square/add-support-for-typescript
Add support for TypeScript
2 parents f7cd610 + a16d3a3 commit bce403f

File tree

8 files changed

+157
-7
lines changed

8 files changed

+157
-7
lines changed

‎.all-contributorsrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@
2424
"test",
2525
"tool"
2626
]
27+
},
28+
{
29+
"login": "otofu-square",
30+
"name": "otofu-square",
31+
"avatar_url": "https://avatars0.githubusercontent.com/u/10118235?v=4",
32+
"profile": "https://github.com/otofu-square",
33+
"contributions": [
34+
"code"
35+
]
2736
}
2837
]
2938
}

‎README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Simple component wrapper and utilities for testing React hooks.
99
[![downloads](https://img.shields.io/npm/dm/react-hooks-testing-library.svg?style=flat-square)](http://www.npmtrends.com/react-hooks-testing-library)
1010
[![MIT License](https://img.shields.io/npm/l/react-hooks-testing-library.svg?style=flat-square)](https://github.com/mpeyper/react-hooks-testing-library/blob/master/LICENSE.md)
1111

12-
[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors)
12+
[![All Contributors](https://img.shields.io/badge/all_contributors-2-orange.svg?style=flat-square)](#contributors)
1313
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](http://makeapullrequest.com)
1414
[![Code of Conduct](https://img.shields.io/badge/code%20of-conduct-ff69b4.svg?style=flat-square)](https://github.com/mpeyper/react-hooks-testing-library/blob/master/CODE_OF_CONDUCT.md)
1515

@@ -156,8 +156,7 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds
156156

157157
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
158158
<!-- prettier-ignore -->
159-
| [<img src="https://avatars0.githubusercontent.com/u/23029903?v=4" width="100px;"/><br /><sub><b>Michael Peyper</b></sub>](https://github.com/mpeyper)<br />[💻](https://github.com/mpeyper/react-hooks-testing-library/commits?author=mpeyper "Code") [🎨](#design-mpeyper "Design") [📖](https://github.com/mpeyper/react-hooks-testing-library/commits?author=mpeyper "Documentation") [🤔](#ideas-mpeyper "Ideas, Planning, & Feedback") [🚇](#infra-mpeyper "Infrastructure (Hosting, Build-Tools, etc)") [📦](#platform-mpeyper "Packaging/porting to new platform") [⚠️](https://github.com/mpeyper/react-hooks-testing-library/commits?author=mpeyper "Tests") [🔧](#tool-mpeyper "Tools") |
160-
| :---: |
159+
<table><tr><td align="center"><a href="https://github.com/mpeyper"><img src="https://avatars0.githubusercontent.com/u/23029903?v=4" width="100px;" alt="Michael Peyper"/><br /><sub><b>Michael Peyper</b></sub></a><br /><a href="https://github.com/mpeyper/react-hooks-testing-library/commits?author=mpeyper" title="Code">💻</a> <a href="#design-mpeyper" title="Design">🎨</a> <a href="https://github.com/mpeyper/react-hooks-testing-library/commits?author=mpeyper" title="Documentation">📖</a> <a href="#ideas-mpeyper" title="Ideas, Planning, & Feedback">🤔</a> <a href="#infra-mpeyper" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="#platform-mpeyper" title="Packaging/porting to new platform">📦</a> <a href="https://github.com/mpeyper/react-hooks-testing-library/commits?author=mpeyper" title="Tests">⚠️</a> <a href="#tool-mpeyper" title="Tools">🔧</a></td><td align="center"><a href="https://github.com/otofu-square"><img src="https://avatars0.githubusercontent.com/u/10118235?v=4" width="100px;" alt="otofu-square"/><br /><sub><b>otofu-square</b></sub></a><br /><a href="https://github.com/mpeyper/react-hooks-testing-library/commits?author=otofu-square" title="Code">💻</a></td></tr></table>
161160

162161
<!-- ALL-CONTRIBUTORS-LIST:END -->
163162

‎index.d.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { cleanup, act, RenderOptions, RenderResult } from 'react-testing-library'
2+
3+
export function renderHook<P, R>(
4+
callback: (props: P) => R,
5+
options?: {
6+
initialProps?: P
7+
} & RenderOptions
8+
): {
9+
readonly result: {
10+
current: R
11+
}
12+
readonly unmount: RenderResult['unmount']
13+
readonly rerender: (hookProps?: P) => void
14+
}
15+
16+
export const testHook: typeof renderHook
17+
18+
export { cleanup, act }

‎package-lock.json

Lines changed: 40 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"version": "0.3.4",
44
"description": "Simple component wrapper for testing React hooks",
55
"main": "lib/index.js",
6+
"typings": "./index.d.ts",
67
"author": "Michael Peyper",
78
"repository": {
89
"type": "git",
@@ -26,6 +27,7 @@
2627
"@babel/plugin-transform-modules-commonjs": "^7.2.0",
2728
"@babel/preset-env": "^7.3.4",
2829
"@babel/preset-react": "^7.0.0",
30+
"@types/react": "^16.8.5",
2931
"all-contributors-cli": "^6.1.2",
3032
"babel-eslint": "^10.0.1",
3133
"babel-plugin-module-resolver": "^3.2.0",
@@ -39,7 +41,9 @@
3941
"prettier-eslint": "^8.8.2",
4042
"prettier-eslint-cli": "^4.7.1",
4143
"react": "^16.8.3",
42-
"react-dom": "^16.8.3"
44+
"react-dom": "^16.8.3",
45+
"typescript": "^3.3.3333",
46+
"typings-tester": "^0.3.2"
4347
},
4448
"peerDependencies": {
4549
"react": "^16.8.0",

‎test/typescript.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { checkDirectory } from 'typings-tester'
2+
3+
describe('TypeScript definitions', function() {
4+
it('should compile against index.d.ts', () => {
5+
checkDirectory(__dirname + '/typescript')
6+
})
7+
})

‎test/typescript/renderHook.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { useState, useCallback, useEffect } from 'react'
2+
import { renderHook } from 'react-hooks-testing-library'
3+
4+
const useCounter = (initialCount: number = 0) => {
5+
const [count, setCount] = useState(initialCount)
6+
const incrementBy = useCallback(
7+
(n: number) => {
8+
setCount(count + n)
9+
},
10+
[count]
11+
)
12+
const decrementBy = useCallback(
13+
(n: number) => {
14+
setCount(count - n)
15+
},
16+
[count]
17+
)
18+
return {
19+
count,
20+
incrementBy,
21+
decrementBy
22+
}
23+
}
24+
25+
function checkTypesWithNoInitialProps() {
26+
const { result, unmount, rerender } = renderHook(() => useCounter())
27+
28+
// check types
29+
const _result: {
30+
current: {
31+
count: number
32+
incrementBy: (_: number) => void
33+
decrementBy: (_: number) => void
34+
}
35+
} = result
36+
const _unmount: () => boolean = unmount
37+
const _rerender: () => void = rerender
38+
}
39+
40+
function checkTypesWithInitialProps() {
41+
const { result, unmount, rerender } = renderHook(({ count }) => useCounter(count), {
42+
initialProps: { count: 10 }
43+
})
44+
45+
// check types
46+
const _result: {
47+
current: {
48+
count: number
49+
incrementBy: (_: number) => void
50+
decrementBy: (_: number) => void
51+
}
52+
} = result
53+
const _unmount: () => boolean = unmount
54+
const _rerender: (_?: { count: number }) => void = rerender
55+
}
56+
57+
function checkTypesWhenHookReturnsVoid() {
58+
const { result, unmount, rerender } = renderHook(() => useEffect(() => {}))
59+
60+
// check types
61+
const _result: {
62+
current: void
63+
} = result
64+
const _unmount: () => boolean = unmount
65+
const _rerender: () => void = rerender
66+
}

‎test/typescript/tsconfig.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"compilerOptions": {
3+
"lib": ["es2015", "dom"],
4+
"strict": true,
5+
"baseUrl": "../../",
6+
"paths": {
7+
"react-hooks-testing-library": ["index.d.ts"]
8+
}
9+
}
10+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /