-
-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathWeakDependencyMap.test.ts
More file actions
102 lines (89 loc) · 2.59 KB
/
WeakDependencyMap.test.ts
File metadata and controls
102 lines (89 loc) · 2.59 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
import { Temporal } from 'temporal-polyfill';
import { EntityPath } from '../interface';
import { MemoPolicy } from '../memo/Policy';
import WeakDependencyMap from '../memo/WeakDependencyMap';
describe('WeakDependencyMap', () => {
const a = { hi: '5' };
const b = [1, 2, 3];
const c = Temporal.Instant.fromEpochMilliseconds(0);
const state: Record<string, Record<string, object>> = {
A: {
'1': a,
},
B: {
'2': b,
},
C: {
'3': c,
},
};
const getEntity = MemoPolicy.getEntities(state);
const depA = { path: { key: 'A', pk: '1' }, entity: a };
const depB = { path: { key: 'B', pk: '2' }, entity: b };
const depC = { path: { key: 'C', pk: '3' }, entity: c };
it('should construct', () => {
const wem = new WeakDependencyMap<EntityPath>();
});
it('should set one item', () => {
const wem = new WeakDependencyMap<EntityPath>();
const deps = [depA];
wem.set(deps, 'myvalue');
expect(wem.get(a, getEntity)[0]).toBe('myvalue');
expect(wem.get(b, getEntity)[0]).toBeUndefined();
expect(wem.get(c, getEntity)[0]).toBeUndefined();
});
it('should set multiple on same path', () => {
const wem = new WeakDependencyMap<EntityPath>();
const attempts = [
{ key: [depA], value: 'first' },
{
key: [depA, depB],
value: 'second',
},
{
key: [depA, depB, depC],
value: 'third',
},
];
for (const attempt of attempts) {
wem.set(attempt.key, attempt.value);
}
expect(wem.get(a, getEntity)[0]).toBe('third');
});
it('should set multiple on distinct paths', () => {
const wem = new WeakDependencyMap<EntityPath>();
const attempts = [
{
key: [depA, depB],
value: 'first',
},
{
key: [depB, depA],
value: 'second',
},
{
key: [depC, depA],
value: 'third',
},
{
key: [depA, depB, depC],
value: 'fourth',
},
{ key: [depC], value: 'fifth' },
];
for (const attempt of attempts) {
wem.set(attempt.key, attempt.value);
}
expect(wem.get(a, getEntity)[0]).toBe('fourth');
expect(wem.get(b, getEntity)[0]).toBe('second');
expect(wem.get(c, getEntity)[0]).toBe('fifth');
expect(wem.get({}, getEntity)[0]).toBeUndefined();
});
it('considers empty key list invalid', () => {
const wem = new WeakDependencyMap<EntityPath>();
expect(() => wem.set([], 'whatever')).toThrowErrorMatchingInlineSnapshot(
`"Keys must include at least one member"`,
);
expect(wem.get([], getEntity)[0]).toBeUndefined();
});
});