forked from mantinedev/mantine
/
use-toggle.test.ts
61 lines (46 loc) · 1.99 KB
/
use-toggle.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
import { renderHook, act } from '@testing-library/react';
import { useToggle } from './use-toggle';
describe('@mantine/hooks/use-toggle', () => {
it('returns correct initial state', () => {
const hook = renderHook(() => useToggle(['dark', 'light'] as const));
expect(hook.result.current[0]).toBe('dark');
});
it('correctly toggles value', () => {
const hook = renderHook(() => useToggle(['dark', 'light'] as const));
act(() => hook.result.current[1]());
expect(hook.result.current[0]).toBe('light');
act(() => hook.result.current[1]());
expect(hook.result.current[0]).toBe('dark');
});
it('correctly toggles more than two values', () => {
const hook = renderHook(() => useToggle(['dark', 'light', 'normal'] as const));
act(() => hook.result.current[1]());
expect(hook.result.current[0]).toBe('light');
act(() => hook.result.current[1]());
expect(hook.result.current[0]).toBe('normal');
act(() => hook.result.current[1]());
expect(hook.result.current[0]).toBe('dark');
act(() => hook.result.current[1]('normal'));
expect(hook.result.current[0]).toBe('normal');
act(() => hook.result.current[1]());
expect(hook.result.current[0]).toBe('dark');
});
it('allows to set value', () => {
const hook = renderHook(() => useToggle(['dark', 'light'] as const));
act(() => hook.result.current[1]('dark'));
expect(hook.result.current[0]).toBe('dark');
act(() => hook.result.current[1]('dark'));
expect(hook.result.current[0]).toBe('dark');
});
it('allows to set value with callback function', () => {
const hook = renderHook(() => useToggle(['dark', 'light'] as const));
act(() => hook.result.current[1]((v) => v));
expect(hook.result.current[0]).toBe('dark');
});
it('allows to use hook without options', () => {
const hook = renderHook(() => useToggle());
expect(hook.result.current[0]).toBe(false);
act(() => hook.result.current[1]());
expect(hook.result.current[0]).toBe(true);
});
});