-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseWatch.spec.ts
67 lines (51 loc) · 1.44 KB
/
useWatch.spec.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
63
64
65
66
67
import { renderHook } from '@testing-library/react-hooks'
import { useWatch } from '../src'
const setUp = () => renderHook((state) => useWatch(state), { initialProps: 0 })
const mockConsole = jest
.spyOn(console, 'log')
.mockImplementation((previous: any, current: any) => ({
previous,
current
}))
const stub = jest.fn().mockImplementation((prev: any, current: any) => ({
prev,
current
}))
const setUpWithFn = () =>
renderHook((state) => useWatch(state, stub), {
initialProps: 0
})
describe('useWatch', () => {
afterEach(() => {
stub.mockClear()
mockConsole.mockClear()
})
it('should log with initial value on first render', () => {
setUp()
expect(mockConsole).toHaveBeenCalledWith({
previous: undefined,
current: 0
})
expect(mockConsole).toHaveBeenCalledTimes(1)
})
it('should log with initial value on first render', () => {
const { rerender } = setUp()
expect(mockConsole).toHaveBeenCalledWith({
previous: undefined,
current: 0
})
rerender(1)
expect(mockConsole).toHaveBeenCalledWith({
previous: 0,
current: 1
})
expect(mockConsole).toHaveBeenCalledTimes(2)
})
it('should call custom function instead of console.log', () => {
const { rerender } = setUpWithFn()
expect(stub).toHaveBeenCalledWith(undefined, 0)
rerender(1)
expect(stub).toHaveBeenCalledWith(0, 1)
expect(mockConsole).not.toHaveBeenCalled()
})
})