Skip to content
This repository was archived by the owner on Nov 5, 2024. It is now read-only.

Commit 53d2b38

Browse files
committed
Adds test for unmount function; Adds example of useEffect
1 parent 9f28605 commit 53d2b38

File tree

4 files changed

+85
-39
lines changed

4 files changed

+85
-39
lines changed

examples/__tests__/react-hooks.js

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,76 @@
66
*/
77
import {testHook, act, cleanup} from 'react-testing-library'
88

9-
import useCounter from '../react-hooks'
9+
import { useCounter, useDocumentTitle } from '../react-hooks'
1010

1111
afterEach(cleanup)
1212

13-
test('accepts default initial values', () => {
14-
let count
15-
testHook(() => ({count} = useCounter()))
13+
describe('useCounter', () => {
14+
test('accepts default initial values', () => {
15+
let count
16+
testHook(() => ({count} = useCounter()))
1617

17-
expect(count).toBe(0)
18-
})
18+
expect(count).toBe(0)
19+
})
1920

20-
test('accepts a default initial value for `count`', () => {
21-
let count
22-
testHook(() => ({count} = useCounter({})))
21+
test('accepts a default initial value for `count`', () => {
22+
let count
23+
testHook(() => ({count} = useCounter({})))
2324

24-
expect(count).toBe(0)
25-
})
25+
expect(count).toBe(0)
26+
})
2627

27-
test('provides an `increment` function', () => {
28-
let count, increment
29-
testHook(() => ({count, increment} = useCounter({step: 2})))
28+
test('provides an `increment` function', () => {
29+
let count, increment
30+
testHook(() => ({count, increment} = useCounter({step: 2})))
3031

31-
expect(count).toBe(0)
32-
act(() => {
33-
increment()
32+
expect(count).toBe(0)
33+
act(() => {
34+
increment()
35+
})
36+
expect(count).toBe(2)
3437
})
35-
expect(count).toBe(2)
36-
})
3738

38-
test('provides an `decrement` function', () => {
39-
let count, decrement
40-
testHook(() => ({count, decrement} = useCounter({step: 2})))
39+
test('provides an `decrement` function', () => {
40+
let count, decrement
41+
testHook(() => ({count, decrement} = useCounter({step: 2})))
4142

42-
expect(count).toBe(0)
43-
act(() => {
44-
decrement()
43+
expect(count).toBe(0)
44+
act(() => {
45+
decrement()
46+
})
47+
expect(count).toBe(-2)
4548
})
46-
expect(count).toBe(-2)
47-
})
4849

49-
test('accepts a default initial value for `step`', () => {
50-
let count, increment
51-
testHook(() => ({count, increment} = useCounter({})))
50+
test('accepts a default initial value for `step`', () => {
51+
let count, increment
52+
testHook(() => ({count, increment} = useCounter({})))
5253

53-
expect(count).toBe(0)
54-
act(() => {
55-
increment()
54+
expect(count).toBe(0)
55+
act(() => {
56+
increment()
57+
})
58+
expect(count).toBe(1)
5659
})
57-
expect(count).toBe(1)
5860
})
61+
62+
describe('useDocumentTitle', () => {
63+
test('sets a title', () => {
64+
document.title = 'original title'
65+
testHook(() => {
66+
useDocumentTitle('modified title')
67+
})
68+
69+
expect(document.title).toBe('modified title')
70+
})
71+
72+
test('returns to original title when component is unmounted', () => {
73+
document.title = 'original title'
74+
const { unmount } = testHook(() => {
75+
useDocumentTitle('modified title')
76+
})
77+
78+
unmount()
79+
expect(document.title).toBe('original title')
80+
})
81+
})

examples/react-hooks.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
import {useState} from 'react'
1+
import {useState, useEffect} from 'react'
22

3-
function useCounter({initialCount = 0, step = 1} = {}) {
3+
export function useCounter({initialCount = 0, step = 1} = {}) {
44
const [count, setCount] = useState(initialCount)
55
const increment = () => setCount(c => c + step)
66
const decrement = () => setCount(c => c - step)
77
return {count, increment, decrement}
88
}
99

10-
export default useCounter
10+
export function useDocumentTitle(title) {
11+
const [originalTitle, setOriginalTitle] = useState(document.title)
12+
useEffect(() => {
13+
setOriginalTitle(document.title)
14+
document.title = title
15+
return () => {
16+
document.title = originalTitle
17+
}
18+
}, [title])
19+
}

src/__tests__/test-hook.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {useState} from 'react'
1+
import {useState, useEffect} from 'react'
22
import 'jest-dom/extend-expect'
33
import {testHook, cleanup} from '../'
44

@@ -12,3 +12,17 @@ test('testHook calls the callback', () => {
1212
test('confirm we can safely call a React Hook from within the callback', () => {
1313
testHook(() => useState())
1414
})
15+
test('returns a function to unmount the component', () => {
16+
let isMounted
17+
const { unmount } = testHook(() => {
18+
useEffect(() => {
19+
isMounted = true
20+
return () => {
21+
isMounted = false
22+
}
23+
})
24+
})
25+
expect(isMounted).toBe(true)
26+
unmount()
27+
expect(isMounted).toBe(false)
28+
})

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function TestHook({callback}) {
6868

6969
function testHook(callback) {
7070
const { unmount } = render(<TestHook callback={callback} />)
71-
return unmount
71+
return { unmount }
7272
}
7373

7474
function cleanup() {

0 commit comments

Comments
 (0)