-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathuseSet.test.ts
174 lines (130 loc) Β· 3.78 KB
/
useSet.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { act, renderHook } from '@testing-library/react-hooks';
import useSet from '../src/useSet';
const setUp = <K>(initialSet?: Set<K>) => renderHook(() => useSet(initialSet));
it('should init set and utils', () => {
const { result } = setUp(new Set([1, 2]));
const [set, utils] = result.current;
expect(set).toEqual(new Set([1, 2]));
expect(utils).toStrictEqual({
has: expect.any(Function),
add: expect.any(Function),
remove: expect.any(Function),
toggle: expect.any(Function),
reset: expect.any(Function),
clear: expect.any(Function),
});
});
it('should init empty set if no initial set provided', () => {
const { result } = setUp();
expect(result.current[0]).toEqual(new Set());
});
it('should have an initially provided key', () => {
const { result } = setUp(new Set(['a']));
const [, utils] = result.current;
let value;
act(() => {
value = utils.has('a');
});
expect(value).toBe(true);
});
it('should have an added key', () => {
const { result } = setUp(new Set());
act(() => {
result.current[1].add('newKey');
});
let value;
act(() => {
value = result.current[1].has('newKey');
});
expect(value).toBe(true);
});
it('should get false for non-existing key', () => {
const { result } = setUp(new Set(['a']));
const [, utils] = result.current;
let value;
act(() => {
value = utils.has('nonExisting');
});
expect(value).toBe(false);
});
it('should add a new key', () => {
const { result } = setUp(new Set(['oldKey']));
const [, utils] = result.current;
act(() => {
utils.add('newKey');
});
expect(result.current[0]).toEqual(new Set(['oldKey', 'newKey']));
});
it('should work if setting existing key', () => {
const { result } = setUp(new Set(['oldKey']));
const [, utils] = result.current;
act(() => {
utils.add('oldKey');
});
expect(result.current[0]).toEqual(new Set(['oldKey']));
});
it('should remove existing key', () => {
const { result } = setUp(new Set([1, 2]));
const [, utils] = result.current;
act(() => {
utils.remove(2);
});
expect(result.current[0]).toEqual(new Set([1]));
});
it('should remove an existing key on toggle', () => {
const { result } = setUp(new Set([1, 2]));
const [, utils] = result.current;
act(() => {
utils.toggle(2);
});
expect(result.current[0]).toEqual(new Set([1]));
});
it('should add a new key on toggle', () => {
const { result } = setUp(new Set([1]));
const [, utils] = result.current;
act(() => {
utils.toggle(2);
});
expect(result.current[0]).toEqual(new Set([1, 2]));
});
it('should do nothing if removing non-existing key', () => {
const { result } = setUp(new Set(['a', 'b']));
const [, utils] = result.current;
act(() => {
utils.remove('nonExisting');
});
expect(result.current[0]).toEqual(new Set(['a', 'b']));
});
it('should reset to initial set provided', () => {
const { result } = setUp(new Set([1]));
const [, utils] = result.current;
act(() => {
utils.add(2);
});
expect(result.current[0]).toEqual(new Set([1, 2]));
act(() => {
utils.reset();
});
expect(result.current[0]).toEqual(new Set([1]));
});
it('should be empty', () => {
const { result } = setUp(new Set([1]));
const [, utils] = result.current;
act(() => {
utils.clear();
});
expect(result.current[0]).toEqual(new Set([]));
});
it('should memoized its utils methods', () => {
const { result } = setUp(new Set(['a', 'b']));
const [, utils] = result.current;
const { add, remove, reset, clear, toggle } = utils;
act(() => {
add('foo');
});
expect(result.current[1].add).toBe(add);
expect(result.current[1].remove).toBe(remove);
expect(result.current[1].toggle).toBe(toggle);
expect(result.current[1].reset).toBe(reset);
expect(result.current[1].clear).toBe(clear);
});