forked from toss/react-simplikit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseMap.spec.ts
More file actions
155 lines (115 loc) · 4.75 KB
/
useMap.spec.ts
File metadata and controls
155 lines (115 loc) · 4.75 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
import { describe, expect, it } from 'vitest';
import { renderHookSSR } from '../../_internal/test-utils/renderHookSSR.tsx';
import { useMap } from './useMap.ts';
describe('useMap', () => {
it('should initialize with a Map', async () => {
const initialMap = new Map([[1, 'initial']]);
const { result } = await renderHookSSR(() => useMap(initialMap));
expect(result.current[0].get(1)).toBe('initial');
});
it('should initialize with an array of entries', async () => {
const { result } = await renderHookSSR(() => useMap([[1, 'initial']]));
expect(result.current[0].get(1)).toBe('initial');
});
it('should initialize with an empty Map when no arguments provided', async () => {
const { result } = await renderHookSSR(() => useMap());
expect(result.current[0].size).toBe(0);
});
it('should add a new value to the Map', async () => {
const { result, rerender } = await renderHookSSR(() => useMap<number, string>());
const [, actions] = result.current;
expect(result.current[0].get(1)).toBeUndefined();
actions.set(1, 'added');
rerender();
expect(result.current[0].get(1)).toBe('added');
});
it('should update an existing value in the Map', async () => {
const initialMap = new Map([[1, 'initial']]);
const { result, rerender } = await renderHookSSR(() => useMap(initialMap));
const [, actions] = result.current;
actions.set(1, 'edited');
rerender();
expect(result.current[0].get(1)).toBe('edited');
});
it('should replace all values with setAll', async () => {
const initialMap = new Map([
[1, 'initial'],
[2, 'example'],
]);
const { result, rerender } = await renderHookSSR(() => useMap(initialMap));
const [, actions] = result.current;
expect(result.current[0].get(1)).toBe('initial');
expect(result.current[0].get(2)).toBe('example');
expect(result.current[0].size).toBe(2);
actions.setAll([[1, 'edited']]);
rerender();
expect(result.current[0].get(1)).toBe('edited');
expect(result.current[0].get(2)).toBeUndefined();
expect(result.current[0].size).toBe(1);
});
it('should remove an existing value from the Map', async () => {
const initialMap = new Map([[1, 'initial']]);
const { result, rerender } = await renderHookSSR(() => useMap(initialMap));
const [, actions] = result.current;
actions.remove(1);
rerender();
expect(result.current[0].get(1)).toBeUndefined();
expect(result.current[0].size).toBe(0);
});
it('should reset the Map to its initial state', async () => {
const initialMap = new Map([[1, 'initial']]);
const { result, rerender } = await renderHookSSR(() => useMap(initialMap));
const [, actions] = result.current;
// First modify the map
actions.set(2, 'added');
actions.set(1, 'modified');
rerender();
expect(result.current[0].get(1)).toBe('modified');
expect(result.current[0].get(2)).toBe('added');
expect(result.current[0].size).toBe(2);
// Then reset to initial state
actions.reset();
rerender();
// Should be back to initial state
expect(result.current[0].get(1)).toBe('initial');
expect(result.current[0].get(2)).toBeUndefined();
expect(result.current[0].size).toBe(1);
});
it('should reset to empty Map when initialized with empty Map', async () => {
const { result, rerender } = await renderHookSSR(() => useMap<number, string>());
const [, actions] = result.current;
// Add some items
actions.set(1, 'one');
actions.set(2, 'two');
rerender();
expect(result.current[0].size).toBe(2);
// Reset should restore to empty state
actions.reset();
rerender();
expect(result.current[0].size).toBe(0);
});
it('should create a new Map reference when values change', async () => {
const initialMap = new Map<number, number>();
const { result, rerender } = await renderHookSSR(() => useMap(initialMap));
const [originalMapRef, actions] = result.current;
actions.set(1, 1);
rerender();
expect(originalMapRef).not.toBe(result.current[0]);
expect(originalMapRef.get(1)).toBeUndefined();
expect(result.current[0].get(1)).toBe(1);
});
it('should maintain stable actions reference after Map changes', async () => {
const initialMap = new Map<number, number>();
const { result, rerender } = await renderHookSSR(() => useMap(initialMap));
const [, originalActionsRef] = result.current;
expect(result.current[1]).toBe(originalActionsRef);
originalActionsRef.set(1, 1);
rerender();
expect(result.current[1]).toBe(originalActionsRef);
});
it('is safe in server-side rendering', () => {
const initialMap = new Map([[1, 'initial']]);
renderHookSSR.serverOnly(() => useMap(initialMap));
// This shouldn't throw any errors
});
});