forked from jupyterlab/jupyterlab
/
observablemap.spec.ts
239 lines (214 loc) · 7.27 KB
/
observablemap.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
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import { ObservableMap } from '@jupyterlab/observables';
describe('@jupyterlab/observables', () => {
describe('ObservableMap', () => {
describe('#constructor()', () => {
it('should accept no arguments', () => {
const value = new ObservableMap<number>();
expect(value instanceof ObservableMap).toBe(true);
});
});
describe('#type', () => {
it('should return `Map`', () => {
const value = new ObservableMap<number>();
expect(value.type).toBe('Map');
});
});
describe('#size', () => {
it('should return the number of entries in the map', () => {
const value = new ObservableMap<number>();
value.set('one', 1);
value.set('two', 2);
expect(value.size).toBe(2);
});
});
describe('#changed', () => {
it('should be emitted when the map changes state', () => {
let called = false;
const value = new ObservableMap<number>();
value.changed.connect(() => {
called = true;
});
value.set('entry', 1);
expect(called).toBe(true);
});
it('should have value changed args', () => {
let called = false;
const value = new ObservableMap<number>();
value.changed.connect((sender, args) => {
expect(sender).toBe(value);
expect(args.type).toBe('add');
expect(args.newValue).toBe(0);
expect(args.oldValue).toBeUndefined();
expect(args.key).toBe('entry');
called = true;
});
value.set('entry', 0);
expect(called).toBe(true);
});
});
describe('#isDisposed', () => {
it('should test whether the map is disposed', () => {
const value = new ObservableMap<number>();
expect(value.isDisposed).toBe(false);
value.dispose();
expect(value.isDisposed).toBe(true);
});
});
describe('#dispose()', () => {
it('should dispose of the resources held by the map', () => {
const value = new ObservableMap<number>();
value.set('one', 1);
value.set('two', 2);
value.dispose();
expect(value.isDisposed).toBe(true);
});
});
describe('#set()', () => {
it('should set the item at a specific key', () => {
const value = new ObservableMap<number>();
value.set('one', 1);
expect(value.get('one')).toBe(1);
});
it('should return the old value for that key', () => {
const value = new ObservableMap<number>();
value.set('one', 1);
const x = value.set('one', 1.01);
expect(x).toBe(1);
});
it('should trigger a changed signal', () => {
let called = false;
const value = new ObservableMap<number>();
value.changed.connect((sender, args) => {
expect(sender).toBe(value);
expect(args.type).toBe('add');
expect(args.newValue).toBe(1);
expect(args.oldValue).toBeUndefined();
expect(args.key).toBe('one');
called = true;
});
value.set('one', 1);
expect(called).toBe(true);
});
});
describe('#get()', () => {
it('should get the value for a key', () => {
const value = new ObservableMap<number>();
value.set('one', 1);
expect(value.get('one')).toBe(1);
});
it('should return undefined if the key does not exist', () => {
const value = new ObservableMap<number>();
value.set('one', 1);
expect(value.get('two')).toBeUndefined();
});
});
describe('#has()', () => {
it('should whether the key exists in a map', () => {
const value = new ObservableMap<number>();
value.set('one', 1);
expect(value.has('one')).toBe(true);
expect(value.has('two')).toBe(false);
});
});
describe('#keys()', () => {
it('should return a list of the keys in the map', () => {
const value = new ObservableMap<number>();
value.set('one', 1);
value.set('two', 2);
value.set('three', 3);
const keys = value.keys();
expect(keys).toEqual(['one', 'two', 'three']);
});
});
describe('#values()', () => {
it('should return a list of the values in the map', () => {
const value = new ObservableMap<number>();
value.set('one', 1);
value.set('two', 2);
value.set('three', 3);
const keys = value.values();
expect(keys).toEqual([1, 2, 3]);
});
});
describe('#delete()', () => {
it('should remove an item from the map', () => {
const value = new ObservableMap<number>();
value.set('one', 1);
value.set('two', 2);
value.set('three', 3);
expect(value.get('two')).toBe(2);
value.delete('two');
expect(value.get('two')).toBeUndefined();
});
it('should return the value of the key it removed', () => {
const value = new ObservableMap<number>();
value.set('one', 1);
expect(value.delete('one')).toBe(1);
expect(value.delete('one')).toBeUndefined();
});
it('should trigger a changed signal if actually removed', () => {
const value = new ObservableMap<number>();
value.set('one', 1);
value.set('two', 2);
value.set('three', 3);
let called = false;
value.changed.connect((sender, args) => {
expect(sender).toBe(value);
expect(args.type).toBe('remove');
expect(args.key).toBe('two');
expect(args.oldValue).toBe(2);
expect(args.newValue).toBeUndefined();
called = true;
});
value.delete('two');
expect(called).toBe(true);
});
it('should not trigger a changed signal if not actually removed', () => {
const value = new ObservableMap<number>();
value.set('one', 1);
value.set('three', 3);
let called = false;
value.changed.connect((sender, args) => {
expect(sender).toBe(value);
expect(args.type).toBe('remove');
expect(args.key).toBe('two');
expect(args.oldValue).toBe(2);
expect(args.newValue).toBeUndefined();
called = true;
});
// 'two' is not in the map
value.delete('two');
expect(called).toBe(false);
});
});
describe('#clear()', () => {
it('should remove all items from the map', () => {
const value = new ObservableMap<number>();
value.set('one', 1);
value.set('two', 2);
value.set('three', 3);
value.clear();
expect(value.size).toBe(0);
value.clear();
expect(value.size).toBe(0);
});
it('should trigger a changed signal', () => {
const value = new ObservableMap<number>();
value.set('one', 1);
let called = false;
value.changed.connect((sender, args) => {
expect(sender).toBe(value);
expect(args.type).toBe('remove');
expect(args.key).toBe('one');
expect(args.oldValue).toBe(1);
expect(args.newValue).toBeUndefined();
called = true;
});
value.clear();
expect(called).toBe(true);
});
});
});
});