-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathuseEffect.test.ts
62 lines (48 loc) · 1.42 KB
/
useEffect.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { useEffect, useLayoutEffect } from 'react'
import { renderHook } from '..'
describe('useEffect tests', () => {
test('should handle useEffect hook', () => {
const sideEffect: { [key: number]: boolean } = { 1: false, 2: false }
const { rerender, unmount } = renderHook(
({ id }) => {
useEffect(() => {
sideEffect[id] = true
return () => {
sideEffect[id] = false
}
}, [id])
},
{ initialProps: { id: 1 } }
)
expect(sideEffect[1]).toBe(true)
expect(sideEffect[2]).toBe(false)
rerender({ id: 2 })
expect(sideEffect[1]).toBe(false)
expect(sideEffect[2]).toBe(true)
unmount()
expect(sideEffect[1]).toBe(false)
expect(sideEffect[2]).toBe(false)
})
test('should handle useLayoutEffect hook', () => {
const sideEffect: { [key: number]: boolean } = { 1: false, 2: false }
const { rerender, unmount } = renderHook(
({ id }) => {
useLayoutEffect(() => {
sideEffect[id] = true
return () => {
sideEffect[id] = false
}
}, [id])
},
{ initialProps: { id: 1 } }
)
expect(sideEffect[1]).toBe(true)
expect(sideEffect[2]).toBe(false)
rerender({ id: 2 })
expect(sideEffect[1]).toBe(false)
expect(sideEffect[2]).toBe(true)
unmount()
expect(sideEffect[1]).toBe(false)
expect(sideEffect[2]).toBe(false)
})
})