-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusePrevious.spec.ts
39 lines (29 loc) · 957 Bytes
/
usePrevious.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
import { renderHook } from '@testing-library/react-hooks'
import { usePrevious } from '../src'
const stub = jest
.fn()
.mockImplementation((prev, current) => `Prev: ${prev}, Current: ${current}`)
const setUp = () =>
renderHook(({ state }) => usePrevious(state, stub), {
initialProps: { state: 0 }
})
describe('usePrevious', () => {
afterEach(() => {
stub.mockClear()
})
it('should return undefined on first run and execute callback', () => {
const { result } = setUp()
expect(result.current).toBeUndefined()
expect(stub).toHaveBeenCalledTimes(1)
})
it('should return previous value and execute callback after each update', () => {
const { result, rerender } = setUp()
rerender({ state: 1 })
expect(result.current).toBe(0)
rerender({ state: 4 })
expect(result.current).toBe(1)
rerender({ state: 10 })
expect(result.current).toBe(4)
expect(stub).toHaveBeenCalledTimes(4)
})
})