forked from toss/react-simplikit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseSet.spec.ts
More file actions
218 lines (167 loc) · 6.25 KB
/
useSet.spec.ts
File metadata and controls
218 lines (167 loc) · 6.25 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import { act } from '@testing-library/react';
import { describe, expect, it } from 'vitest';
import { renderHookSSR } from '../../_internal/test-utils/renderHookSSR.tsx';
import { useSet } from './useSet.ts';
describe('useSet', () => {
it('is safe on server side rendering', () => {
const initialSet = new Set([1, 2, 3]);
renderHookSSR.serverOnly(() => useSet(initialSet));
});
it('should initialize with a Set', async () => {
const initialSet = new Set(['a', 'b']);
const { result } = await renderHookSSR(() => useSet(initialSet));
expect(result.current[0].has('a')).toBe(true);
expect(result.current[0].has('b')).toBe(true);
expect(result.current[0].size).toBe(2);
});
it('should initialize with an array of values', async () => {
const { result } = await renderHookSSR(() => useSet([1, 2, 3]));
expect(result.current[0].has(1)).toBe(true);
expect(result.current[0].has(2)).toBe(true);
expect(result.current[0].has(3)).toBe(true);
expect(result.current[0].size).toBe(3);
});
it('should initialize with an empty Set when no arguments provided', async () => {
const { result } = await renderHookSSR(() => useSet());
expect(result.current[0].size).toBe(0);
});
it('should add a value to the Set', async () => {
const { result, rerender } = await renderHookSSR(() => useSet<string>());
const [, actions] = result.current;
expect(result.current[0].has('a')).toBe(false);
await act(async () => {
actions.add('a');
rerender();
});
expect(result.current[0].has('a')).toBe(true);
expect(result.current[0].size).toBe(1);
});
it('should not duplicate values when adding an existing value', async () => {
const { result, rerender } = await renderHookSSR(() => useSet(['a']));
const [, actions] = result.current;
await act(async () => {
actions.add('a');
rerender();
});
expect(result.current[0].size).toBe(1);
});
it('should remove a value from the Set', async () => {
const { result, rerender } = await renderHookSSR(() => useSet(['a', 'b']));
const [, actions] = result.current;
await act(async () => {
actions.remove('a');
rerender();
});
expect(result.current[0].has('a')).toBe(false);
expect(result.current[0].has('b')).toBe(true);
expect(result.current[0].size).toBe(1);
});
it('should do nothing when removing a value not in the Set', async () => {
const { result, rerender } = await renderHookSSR(() => useSet(['a']));
const [, actions] = result.current;
await act(async () => {
actions.remove('z');
rerender();
});
expect(result.current[0].has('a')).toBe(true);
expect(result.current[0].size).toBe(1);
});
it('should toggle a value - add if absent', async () => {
const { result, rerender } = await renderHookSSR(() => useSet<string>());
const [, actions] = result.current;
await act(async () => {
actions.toggle('a');
rerender();
});
expect(result.current[0].has('a')).toBe(true);
});
it('should toggle a value - remove if present', async () => {
const { result, rerender } = await renderHookSSR(() => useSet(['a']));
const [, actions] = result.current;
await act(async () => {
actions.toggle('a');
rerender();
});
expect(result.current[0].has('a')).toBe(false);
});
it('should replace all values with setAll', async () => {
const { result, rerender } = await renderHookSSR(() => useSet([1, 2, 3]));
const [, actions] = result.current;
await act(async () => {
actions.setAll([4, 5]);
rerender();
});
expect(result.current[0].has(1)).toBe(false);
expect(result.current[0].has(4)).toBe(true);
expect(result.current[0].has(5)).toBe(true);
expect(result.current[0].size).toBe(2);
});
it('should replace all values with setAll using a Set', async () => {
const { result, rerender } = await renderHookSSR(() => useSet([1, 2, 3]));
const [, actions] = result.current;
await act(async () => {
actions.setAll(new Set([4, 5]));
rerender();
});
expect(result.current[0].has(1)).toBe(false);
expect(result.current[0].has(4)).toBe(true);
expect(result.current[0].has(5)).toBe(true);
expect(result.current[0].size).toBe(2);
});
it('should reset the Set to its initial state', async () => {
const initialSet = new Set([1, 2]);
const { result, rerender } = await renderHookSSR(() => useSet(initialSet));
const [, actions] = result.current;
await act(async () => {
actions.add(3);
actions.remove(1);
rerender();
});
expect(result.current[0].has(1)).toBe(false);
expect(result.current[0].has(3)).toBe(true);
await act(async () => {
actions.reset();
rerender();
});
expect(result.current[0].has(1)).toBe(true);
expect(result.current[0].has(2)).toBe(true);
expect(result.current[0].has(3)).toBe(false);
expect(result.current[0].size).toBe(2);
});
it('should reset to empty Set when initialized with empty Set', async () => {
const { result, rerender } = await renderHookSSR(() => useSet<number>());
const [, actions] = result.current;
await act(async () => {
actions.add(1);
actions.add(2);
rerender();
});
expect(result.current[0].size).toBe(2);
await act(async () => {
actions.reset();
rerender();
});
expect(result.current[0].size).toBe(0);
});
it('should create a new Set reference when values change', async () => {
const { result, rerender } = await renderHookSSR(() => useSet<number>());
const [originalSetRef, actions] = result.current;
await act(async () => {
actions.add(1);
rerender();
});
expect(originalSetRef).not.toBe(result.current[0]);
expect(originalSetRef.has(1)).toBe(false);
expect(result.current[0].has(1)).toBe(true);
});
it('should maintain stable actions reference after Set changes', async () => {
const { result, rerender } = await renderHookSSR(() => useSet<number>());
const [, originalActionsRef] = result.current;
expect(result.current[1]).toBe(originalActionsRef);
await act(async () => {
originalActionsRef.add(1);
rerender();
});
expect(result.current[1]).toBe(originalActionsRef);
});
});