Skip to content

Commit e6854b1

Browse files
committed
renamed testHook to renderHook with testHook deprecation warning
1 parent e79069f commit e6854b1

8 files changed

+34
-27
lines changed

src/index.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function TestHook({ callback, hookProps, children }) {
66
return null
77
}
88

9-
function testHook(callback, options = {}) {
9+
function renderHook(callback, options = {}) {
1010
const result = { current: null }
1111
const hookProps = { current: options.initialProps }
1212

@@ -34,4 +34,11 @@ function testHook(callback, options = {}) {
3434
}
3535
}
3636

37-
export { testHook, cleanup, act }
37+
function testHook(...args) {
38+
console.warn(
39+
'`testHook` has been deprecated and will be removed in a future release. Please use `renderHook` instead.'
40+
)
41+
return renderHook(...args)
42+
}
43+
44+
export { renderHook, cleanup, act, testHook }

test/customHook.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react'
22
import { useState, createContext, useContext, useMemo } from 'react'
3-
import { testHook, cleanup, act } from 'src'
3+
import { renderHook, cleanup, act } from 'src'
44

55
describe('custom hook tests', () => {
66
const themes = {
@@ -22,7 +22,7 @@ describe('custom hook tests', () => {
2222
afterEach(cleanup)
2323

2424
test('should use theme', () => {
25-
const { result } = testHook(() => useTheme('light'))
25+
const { result } = renderHook(() => useTheme('light'))
2626

2727
const theme = result.current
2828

@@ -31,7 +31,7 @@ describe('custom hook tests', () => {
3131
})
3232

3333
test('should update theme', () => {
34-
const { result } = testHook(() => useTheme('light'))
34+
const { result } = renderHook(() => useTheme('light'))
3535

3636
const { toggleTheme } = result.current
3737

@@ -53,7 +53,7 @@ describe('custom hook tests', () => {
5353
<ThemesContext.Provider value={customThemes}>{children}</ThemesContext.Provider>
5454
)
5555

56-
const { result } = testHook(() => useTheme('light'), { wrapper })
56+
const { result } = renderHook(() => useTheme('light'), { wrapper })
5757

5858
const theme = result.current
5959

test/useContext.test.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import React from 'react'
22
import { createContext, useContext } from 'react'
3-
import { testHook, cleanup } from 'src'
3+
import { renderHook, cleanup } from 'src'
44

55
describe('useContext tests', () => {
66
afterEach(cleanup)
77

88
test('should get default value from context', () => {
99
const TestContext = createContext('foo')
1010

11-
const { result } = testHook(() => useContext(TestContext))
11+
const { result } = renderHook(() => useContext(TestContext))
1212

1313
const value = result.current
1414

@@ -18,7 +18,7 @@ describe('useContext tests', () => {
1818
test('should get default value from context provider', () => {
1919
const TestContext = createContext('foo')
2020

21-
const { result } = testHook(() => useContext(TestContext))
21+
const { result } = renderHook(() => useContext(TestContext))
2222

2323
const value = result.current
2424

@@ -32,7 +32,7 @@ describe('useContext tests', () => {
3232
<TestContext.Provider value="bar">{children}</TestContext.Provider>
3333
)
3434

35-
const { result } = testHook(() => useContext(TestContext), { wrapper })
35+
const { result } = renderHook(() => useContext(TestContext), { wrapper })
3636

3737
const value = result.current
3838

@@ -46,7 +46,7 @@ describe('useContext tests', () => {
4646
<TestContext.Provider value="bar">{children}</TestContext.Provider>
4747
)
4848

49-
const { result } = testHook(() => useContext(TestContext), { wrapper })
49+
const { result } = renderHook(() => useContext(TestContext), { wrapper })
5050

5151
expect(result.current).toBe('bar')
5252
})
@@ -60,7 +60,7 @@ describe('useContext tests', () => {
6060
<TestContext.Provider value={value.current}>{children}</TestContext.Provider>
6161
)
6262

63-
const { result, rerender } = testHook(() => useContext(TestContext), { wrapper })
63+
const { result, rerender } = renderHook(() => useContext(TestContext), { wrapper })
6464

6565
value.current = 'baz'
6666

test/useEffect.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { useEffect, useLayoutEffect } from 'react'
2-
import { testHook, cleanup } from 'src'
2+
import { renderHook, cleanup } from 'src'
33

44
describe('useEffect tests', () => {
55
afterEach(cleanup)
66

77
test('should handle useEffect hook', () => {
88
const sideEffect = { [1]: false, [2]: false }
99

10-
const { rerender, unmount } = testHook(
10+
const { rerender, unmount } = renderHook(
1111
({ id }) => {
1212
useEffect(() => {
1313
sideEffect[id] = true
@@ -36,9 +36,9 @@ describe('useEffect tests', () => {
3636
test('should handle useLayoutEffect hook', () => {
3737
const sideEffect = { [1]: false, [2]: false }
3838

39-
const { rerender, unmount } = testHook(
39+
const { rerender, unmount } = renderHook(
4040
({ id }) => {
41-
useEffect(() => {
41+
useLayoutEffect(() => {
4242
sideEffect[id] = true
4343
return () => {
4444
sideEffect[id] = false

test/useMemo.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { useMemo, useCallback } from 'react'
2-
import { testHook, cleanup } from 'src'
2+
import { renderHook, cleanup } from 'src'
33

44
describe('useCallback tests', () => {
55
afterEach(cleanup)
66

77
test('should handle useMemo hook', () => {
8-
const { result, rerender } = testHook(({ value }) => useMemo(() => ({ value }), [value]), {
8+
const { result, rerender } = renderHook(({ value }) => useMemo(() => ({ value }), [value]), {
99
initialProps: { value: 1 }
1010
})
1111

@@ -31,7 +31,7 @@ describe('useCallback tests', () => {
3131
})
3232

3333
test('should handle useCallback hook', () => {
34-
const { result, rerender } = testHook(
34+
const { result, rerender } = renderHook(
3535
({ value }) => {
3636
const callback = () => ({ value })
3737
return useCallback(callback, [value])

test/useReducer.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { useReducer } from 'react'
2-
import { testHook, cleanup, act } from 'src'
2+
import { renderHook, cleanup, act } from 'src'
33

44
describe('useReducer tests', () => {
55
afterEach(cleanup)
66

77
test('should handle useReducer hook', () => {
88
const reducer = (state, action) => (action.type === 'inc' ? state + 1 : state)
9-
const { result } = testHook(() => useReducer(reducer, 0))
9+
const { result } = renderHook(() => useReducer(reducer, 0))
1010

1111
const [initialState, dispatch] = result.current
1212

test/useRef.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { useRef, useImperativeHandle } from 'react'
2-
import { testHook, cleanup } from 'src'
2+
import { renderHook, cleanup } from 'src'
33

44
describe('useHook tests', () => {
55
afterEach(cleanup)
66

77
test('should handle useRef hook', () => {
8-
const { result } = testHook(() => useRef())
8+
const { result } = renderHook(() => useRef())
99

1010
const refContainer = result.current
1111

@@ -14,7 +14,7 @@ describe('useHook tests', () => {
1414
})
1515

1616
test('should handle useImperativeHandle hook', () => {
17-
const { result } = testHook(() => {
17+
const { result } = renderHook(() => {
1818
const ref = useRef()
1919
useImperativeHandle(ref, () => ({
2020
fakeImperativeMethod: () => true

test/useState.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import { useState } from 'react'
2-
import { testHook, cleanup, act } from 'src'
2+
import { renderHook, cleanup, act } from 'src'
33

44
describe('useState tests', () => {
55
afterEach(cleanup)
66

77
test('should use setState value', () => {
8-
const { result } = testHook(() => useState('foo'))
8+
const { result } = renderHook(() => useState('foo'))
99

1010
const [value] = result.current
1111

1212
expect(value).toBe('foo')
1313
})
1414

1515
test('should update setState value using setter', () => {
16-
const { result } = testHook(() => useState('foo'))
16+
const { result } = renderHook(() => useState('foo'))
1717

1818
const [_, setValue] = result.current
1919

0 commit comments

Comments
 (0)