-
Notifications
You must be signed in to change notification settings - Fork 2
/
collections-map.spec.ts
462 lines (360 loc) · 14.6 KB
/
collections-map.spec.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
import { autorun, observable, raw } from "@formily/reactive";
// 劫持 Map 类型对象作为 observable 对象
describe("Map", () => {
// 创建一个 Map 类型的 observable 对象
test("should be a proper JS Map", () => {
const map = observable(new Map());
expect(map).toBeInstanceOf(Map);
expect(raw(map)).toBeInstanceOf(Map);
});
// 在 autorun 中响应 map 类型对象
test("should autorun mutations", () => {
const handler = jest.fn();
const map = observable(new Map());
// 初始化先依赖一个永远不存在的 key
autorun(() => handler(map.get("unkey")));
expect(handler).toHaveBeenCalledTimes(1);
expect(handler).toHaveBeenLastCalledWith(undefined);
// 设置 key 不会响应,因为 autorun 没有收集对应的依赖
map.set('key', 'value');
expect(handler).toHaveBeenCalledTimes(1);
expect(handler).toHaveBeenLastCalledWith(undefined);
// 重新监听依赖
autorun(() => handler(map.get("newKey")));
expect(handler).toHaveBeenCalledTimes(2);
expect(handler).toHaveBeenLastCalledWith(undefined);
map.set('newKey', 'value');
expect(handler).toHaveBeenCalledTimes(3);
expect(handler).toHaveBeenLastCalledWith("value");
map.set('newKey', 'value2');
expect(handler).toHaveBeenCalledTimes(4);
expect(handler).toHaveBeenLastCalledWith("value2");
map.delete("newKey");
expect(handler).toHaveBeenCalledTimes(5);
expect(handler).toHaveBeenLastCalledWith(undefined);
});
// 在 autorun 中响应 map.size
test("should autorun size mutations", () => {
const handler = jest.fn();
const map = observable(new Map());
autorun(() => handler(map.size));
expect(handler).toHaveBeenCalledTimes(1);
expect(handler).toHaveBeenLastCalledWith(0);
map.set("key1", "value");
map.set("key2", "value2");
expect(handler).toHaveBeenCalledTimes(3);
expect(handler).toHaveBeenNthCalledWith(2, 1);
expect(handler).toHaveBeenNthCalledWith(3, 2);
map.delete("key1");
expect(handler).toHaveBeenCalledTimes(4);
expect(handler).toHaveBeenNthCalledWith(4, 1);
map.clear();
expect(handler).toHaveBeenCalledTimes(5);
expect(handler).toHaveBeenNthCalledWith(5, 0);
});
// 在 autorun 中通过 for of 迭代 map 获取值
test("should autorun for of iteration", () => {
const handler = jest.fn();
const map = observable(new Map<string, number>());
autorun(() => {
let sum = 0;
for (let [, num] of map) {
sum += num;
}
handler(sum);
});
expect(handler).toHaveBeenCalledTimes(1);
expect(handler).toHaveBeenCalledWith(0);
map.set("key0", 3);
expect(handler).toHaveBeenCalledTimes(2);
expect(handler).toHaveBeenCalledWith(3);
map.set("key1", 2);
expect(handler).toHaveBeenCalledTimes(3);
expect(handler).toHaveBeenCalledWith(5);
map.delete("key0");
expect(handler).toHaveBeenCalledTimes(4);
expect(handler).toHaveBeenCalledWith(2);
map.clear();
expect(handler).toHaveBeenCalledTimes(5);
expect(handler).toHaveBeenCalledWith(0);
});
// 在 autorun 中通过 forEach 迭代 map 获取值
test("should autorun forEach iteration", () => {
const handler = jest.fn();
const map = observable(new Map<string, number>());
autorun(() => {
let sum = 0;
map.forEach(num => (sum += num));
handler(sum);
});
expect(handler).toHaveBeenCalledTimes(1);
expect(handler).toHaveBeenLastCalledWith(0);
map.set("key0", 3);
expect(handler).toHaveBeenCalledTimes(2);
expect(handler).toHaveBeenLastCalledWith(3);
map.set("key1", 2);
expect(handler).toHaveBeenCalledTimes(3);
expect(handler).toHaveBeenLastCalledWith(5);
map.delete("key0");
expect(handler).toHaveBeenCalledTimes(4);
expect(handler).toHaveBeenLastCalledWith(2);
map.clear();
expect(handler).toHaveBeenCalledTimes(5);
expect(handler).toHaveBeenLastCalledWith(0);
});
// 在 autorun 中通过 for of 迭代 map.keys 获取值
test("should autorun keys iteration", () => {
const handler = jest.fn();
const map = observable(new Map<number, number>());
autorun(() => {
let sum = 0;
for (let key of map.keys()) {
sum += key;
}
handler(sum);
});
expect(handler).toHaveBeenCalledTimes(1);
expect(handler).toHaveBeenLastCalledWith(0);
map.set(4, 3);
expect(handler).toHaveBeenCalledTimes(2);
expect(handler).toHaveBeenLastCalledWith(4);
map.set(1, 3);
expect(handler).toHaveBeenCalledTimes(3);
expect(handler).toHaveBeenLastCalledWith(5);
map.delete(4);
expect(handler).toHaveBeenCalledTimes(4);
expect(handler).toHaveBeenLastCalledWith(1);
map.clear();
expect(handler).toHaveBeenCalledTimes(5);
expect(handler).toHaveBeenLastCalledWith(0);
});
// 在 autorun 中通过 for of 迭代 map.values 获取值
test("should autorun values iteration", () => {
const handler = jest.fn();
const map = observable(new Map<string, number>());
autorun(() => {
let sum = 0;
for (let num of map.values()) {
sum += num;
}
handler(sum);
});
expect(handler).toHaveBeenCalledTimes(1);
expect(handler).toHaveBeenLastCalledWith(0);
map.set("key0", 3);
expect(handler).toHaveBeenCalledTimes(2);
expect(handler).toHaveBeenLastCalledWith(3);
map.set("key1", 2);
expect(handler).toHaveBeenCalledTimes(3);
expect(handler).toHaveBeenLastCalledWith(5);
map.delete("key0");
expect(handler).toHaveBeenCalledTimes(4);
expect(handler).toHaveBeenLastCalledWith(2);
map.clear();
expect(handler).toHaveBeenCalledTimes(5);
expect(handler).toHaveBeenLastCalledWith(0);
});
// 在 autorun 中通过 for of 迭代 map.entries 获取值
test("should autorun entries iteration", () => {
const handler = jest.fn();
const map = observable(new Map<string, number>());
autorun(() => {
let sum = 0;
for (let [, num] of map.entries()) {
sum += num;
}
handler(sum);
});
expect(handler).toHaveBeenCalledTimes(1);
expect(handler).toHaveBeenLastCalledWith(0);
map.set("key0", 3);
expect(handler).toHaveBeenCalledTimes(2);
expect(handler).toHaveBeenLastCalledWith(3);
map.set("key1", 2);
expect(handler).toHaveBeenCalledTimes(3);
expect(handler).toHaveBeenLastCalledWith(5);
map.delete("key0");
expect(handler).toHaveBeenCalledTimes(4);
expect(handler).toHaveBeenLastCalledWith(2);
map.clear();
expect(handler).toHaveBeenCalledTimes(5);
expect(handler).toHaveBeenLastCalledWith(0);
});
// 在 autorun 中通过 map.clear 触发响应
test("should be triggered by clearing", () => {
const handler = jest.fn();
const map = observable(new Map<string, number>());
autorun(() => handler(map.get("key")));
expect(handler).toHaveBeenCalledTimes(1);
expect(handler).toHaveBeenLastCalledWith(undefined);
map.set("key", 3);
expect(handler).toHaveBeenCalledTimes(2);
expect(handler).toHaveBeenLastCalledWith(3);
map.clear();
expect(handler).toHaveBeenCalledTimes(3);
expect(handler).toHaveBeenLastCalledWith(undefined);
});
// 在 autorun 中不响应错误的 map 自定义属性
test("should not autorun custom property mutations", () => {
const handler = jest.fn();
const map = observable(new Map<string, string>());
// @ts-ignore
autorun(() => handler(map['customProp']));
expect(handler).toHaveBeenCalledTimes(1);
expect(handler).toHaveBeenCalledWith(undefined);
// @ts-ignore
map["customProp"] = "Hello world";
expect(handler).toHaveBeenCalledTimes(1);
});
// 在 autorun 中不响应未更新的数据
test("should not autorun non value changing mutations", () => {
const handler = jest.fn();
const map = observable(new Map());
autorun(() => handler(map.get("key")));
expect(handler).toHaveBeenCalledTimes(1);
expect(handler).toHaveBeenCalledWith(undefined);
map.set("key", "value");
expect(handler).toHaveBeenCalledTimes(2);
expect(handler).toHaveBeenLastCalledWith("value");
// 重复设置相同的 item 不会触发响应
map.set("key", "value");
expect(handler).toHaveBeenCalledTimes(2);
map.delete("key");
expect(handler).toHaveBeenCalledTimes(3);
expect(handler).toHaveBeenLastCalledWith(undefined);
// 重复删除 item 不会触发响应
map.delete("key");
expect(handler).toHaveBeenCalledTimes(3);
// 清空一个本身为空的 map 也不会触发响应
map.clear();
expect(handler).toHaveBeenCalledTimes(3);
});
// 在 autorun 中不响应 map 类型原始数据
test("should not autorun raw data", () => {
const handler = jest.fn();
const map = observable(new Map());
// 在响应中依赖原始的 Map.key,除了初始化响应 1 次,不再响应更新
autorun(() => handler(raw(map).get("key")));
expect(handler).toHaveBeenCalledTimes(1);
expect(handler).toHaveBeenCalledWith(undefined);
map.set("key", "value");
expect(handler).toHaveBeenCalledTimes(1);
map.delete("key");
expect(handler).toHaveBeenCalledTimes(1);
});
// 在 autorun 中不响应 map 类型原始数据迭代
test("should not autorun raw iterations", () => {
const handler = jest.fn();
const map = observable(new Map());
// 通过 raw 拿到的原始数据不会响应数据迭代
autorun(() => {
const dataRaw = raw(map);
let sum = 0;
for (let [, num] of dataRaw.entries()) {
sum += num;
}
for (let key of dataRaw.keys()) {
sum += dataRaw.get(key);
}
for (let num of dataRaw.values()) {
sum += num;
}
for (let [, num] of dataRaw) {
sum += num;
}
handler(sum);
});
expect(handler).toHaveBeenCalledTimes(1);
expect(handler).toHaveBeenCalledWith(0);
map.set("key1", 2);
map.set("key2", 3);
expect(handler).toHaveBeenCalledTimes(1);
map.delete("key1");
expect(handler).toHaveBeenCalledTimes(1);
});
// 在 autorun 中不响应 map 类型原始数据的增删操作
test("should not be triggered by raw mulations", () => {
const handler = jest.fn();
const map = observable(new Map());
const dataRaw = raw(map);
autorun(() => handler(map.get("key")));
expect(handler).toHaveBeenCalledTimes(1);
expect(handler).toHaveBeenLastCalledWith(undefined);
dataRaw.set("key", "hello");
expect(handler).toHaveBeenCalledTimes(1);
dataRaw.delete("key");
expect(handler).toHaveBeenCalledTimes(1);
dataRaw.clear();
expect(handler).toHaveBeenCalledTimes(1);
});
// 在 autorun 中不响应 map 类型原始数据长度
test("should not autorun raw size mutations", () => {
const handler = jest.fn();
const map = observable(new Map());
autorun(() => handler(raw(map).size));
expect(handler).toHaveBeenCalledTimes(1);
expect(handler).toHaveBeenCalledWith(0);
map.set("key", "value");
expect(handler).toHaveBeenCalledTimes(1);
});
// 在 autorun 中不响应 map 类型原始数据添加
test("should not be triggered by raw size mutaitions", () => {
const handler = jest.fn();
const map = observable(new Map());
autorun(() => handler(map.size));
expect(handler).toHaveBeenCalledTimes(1);
expect(handler).toHaveBeenCalledWith(0);
raw(map).set("key", "value");
expect(handler).toHaveBeenCalledTimes(1);
});
// map 类型的 observable 对象中允许使用 object 作为 key
test("should support object as key", () => {
const handler = jest.fn();
const key = {};
const map = observable(new Map());
autorun(() => handler(map.get(key)));
expect(handler).toHaveBeenCalledTimes(1);
expect(handler).toHaveBeenCalledWith(undefined);
map.set(key, 1);
expect(handler).toHaveBeenCalledTimes(2);
expect(handler).toHaveBeenLastCalledWith(1);
// {} !== key
map.set({}, 2);
expect(handler).toHaveBeenCalledTimes(2);
expect(handler).toHaveBeenLastCalledWith(1);
});
// map 类型的 observable 对象中允许设置一个普通对象,或是 observable 对象作为 value
test("observer object", () => {
const handler = jest.fn();
const map = observable(new Map<string, Partial<Record<string, string>>>([]));
map.set("key", {});
map.set("key2", observable({}));
autorun(() => {
const [obs1, obs2] = map.values();
handler(obs1.aa, obs2.aa);
});
expect(handler).toHaveBeenCalledTimes(1);
const obs1 = map.get("key");
const obs2 = map.get("key2");
if (obs1) obs1.aa = "123";
expect(handler).toHaveBeenCalledTimes(2);
if (obs2) obs2.aa = "234";
expect(handler).toHaveBeenCalledTimes(3);
});
// 浅劫持 map 对象,不会响应对象的属性值修改
test("shallow", () => {
const handler = jest.fn();
const map = observable.shallow(new Map<string, Partial<Record<string, string>>>([]));
map.set("key", {});
autorun(() => {
const [obs] = map.values();
handler(obs.aa);
});
expect(handler).toHaveBeenCalledTimes(1);
const obs = map.get("key");
// 浅劫持对象不会响应修改对象的属性
if (obs) obs.aa = "123";
expect(obs).toBeDefined();
expect(handler).toHaveBeenCalledTimes(1);
});
});