Skip to content

Commit bbfb0ff

Browse files
committed
Add test for TS types
1 parent ae2d990 commit bbfb0ff

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { useState, createContext, useContext, useMemo } from 'react'
2+
import { renderHook } from 'react-hooks-testing-library'
3+
4+
const DARK: 'dark' = 'dark'
5+
const LIGHT: 'light' = 'light'
6+
7+
type InitialTheme = typeof DARK | typeof LIGHT | undefined
8+
9+
const themes = {
10+
light: { primaryLight: '#FFFFFF', primaryDark: '#000000' },
11+
dark: { primaryLight: '#000000', primaryDark: '#FFFFFF' }
12+
}
13+
14+
const ThemesContext = createContext(themes)
15+
16+
const useTheme = (initialTheme: InitialTheme = DARK) => {
17+
const themes = useContext(ThemesContext)
18+
const [theme, setTheme] = useState(initialTheme)
19+
const toggleTheme = () => {
20+
setTheme(theme === 'light' ? 'dark' : 'light')
21+
}
22+
return useMemo(() => ({ ...themes[theme], toggleTheme }), [theme])
23+
}
24+
25+
type InitialProps = { initialTheme: InitialTheme }
26+
27+
function checkTypesWithNoInitialProps() {
28+
const { result, unmount, rerender } = renderHook(() => useTheme())
29+
30+
// check types
31+
const _result: {
32+
current: {
33+
primaryDark: string
34+
primaryLight: string
35+
toggleTheme: () => void
36+
}
37+
} = result
38+
const _unmount: () => boolean = unmount
39+
const _rerender: () => void = rerender
40+
}
41+
42+
function checkTypesWithInitialProps() {
43+
const { result, unmount, rerender } = renderHook(
44+
({ initialTheme }: InitialProps) => useTheme(initialTheme),
45+
{
46+
initialProps: { initialTheme: DARK }
47+
}
48+
)
49+
50+
// check types
51+
const _result: {
52+
current: {
53+
primaryDark: string
54+
primaryLight: string
55+
toggleTheme: () => void
56+
}
57+
} = result
58+
const _unmount: () => boolean = unmount
59+
const _rerender: (_: InitialProps) => void = rerender
60+
}

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)