forked from streamich/react-use
-
Notifications
You must be signed in to change notification settings - Fork 0
/
useUnmount.test.ts
51 lines (39 loc) · 1.27 KB
/
useUnmount.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
import { renderHook } from '@testing-library/react-hooks';
import { useUnmount } from '../src';
describe('useUnmount', () => {
it('should be defined', () => {
expect(useUnmount).toBeDefined();
});
it('should not call provided callback on mount', () => {
const spy = jest.fn();
renderHook(() => useUnmount(spy));
expect(spy).not.toHaveBeenCalled();
});
it('should not call provided callback on re-renders', () => {
const spy = jest.fn();
const hook = renderHook(() => useUnmount(spy));
hook.rerender();
hook.rerender();
hook.rerender();
hook.rerender();
expect(spy).not.toHaveBeenCalled();
});
it('should call provided callback on unmount', () => {
const spy = jest.fn();
const hook = renderHook(() => useUnmount(spy));
hook.unmount();
expect(spy).toHaveBeenCalledTimes(1);
});
it('should call provided callback if is has been changed', () => {
const spy = jest.fn();
const spy2 = jest.fn();
const spy3 = jest.fn();
const hook = renderHook((cb) => useUnmount(cb), { initialProps: spy });
hook.rerender(spy2);
hook.rerender(spy3);
hook.unmount();
expect(spy).not.toHaveBeenCalled();
expect(spy2).not.toHaveBeenCalled();
expect(spy3).toHaveBeenCalledTimes(1);
});
});