-
Notifications
You must be signed in to change notification settings - Fork 0
/
varbox.test.js
266 lines (209 loc) Β· 6.78 KB
/
varbox.test.js
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
var assert = require('assert');
var ok = assert.ok;
var VarBox;
ok((function () {
VarBox = require('./varbox');
ok('createBox' in VarBox);
ok('function' === typeof VarBox.createBox);
ok(1 === VarBox.createBox.length);
ok('getBox' in VarBox);
ok('function' === typeof VarBox.getBox);
ok(1 === VarBox.getBox.length);
ok('grabVarBox' in VarBox);
ok('function' === typeof VarBox.grabVarBox);
ok(1 === VarBox.grabVarBox.length);
return true;
})(), 'require("varbox")');
var box;
var updateTestingKey = 'testUpdate';
ok((function() {
box = VarBox.createBox('firstBox');
ok('get' in box);
ok('function' === typeof box.get);
ok(1 === box.get.length);
ok('set' in box);
ok('function' === typeof box.set);
ok(2 === box.set.length);
ok('update' in box);
ok('function' === typeof box.update);
ok(2 === box.update.length);
ok('merge' in box);
ok('function' === typeof box.merge);
ok(2 === box.merge.length);
ok('has' in box);
ok('function' === typeof box.has);
ok(1 === box.has.length);
ok('remove' in box);
ok('function' === typeof box.remove);
ok(1 === box.remove.length);
ok('destroy' in box);
ok('function' === typeof box.destroy);
ok(1 === box.destroy.length);
ok('watch' in box);
ok('function' === typeof box.watch);
ok(1 === box.watch.length);
ok('watchPath' in box);
ok('function' === typeof box.watchPath);
ok(3 === box.watchPath.length);
ok('watchVariable' in box);
ok('function' === typeof box.watchVariable);
ok(2 === box.watchVariable.length);
ok('nodeMap' in box);
ok('function' === typeof box.nodeMap);
ok(2 === box.nodeMap.length);
ok('nodeBackMap' in box);
ok('function' === typeof box.nodeBackMap);
ok(2 === box.nodeBackMap.length);
ok('everyNode' in box);
ok('function' === typeof box.everyNode);
ok(2 === box.everyNode.length);
return true;
})(), 'VarBox.createBox()');
ok((function() {
var box = VarBox.createBox('firstBox');
var boxGetAgain1 = VarBox.createBox('firstBox');
var boxGetAgain2 = VarBox.createBox({
BOX_NAME: 'firstBox',
});
for (var k in box) {
ok(box[k] === boxGetAgain1[k]);
ok(box[k] === boxGetAgain2[k]);
ok(boxGetAgain1[k] === box[k]);
ok(boxGetAgain1[k] === boxGetAgain2[k]);
ok(boxGetAgain2[k] === box[k]);
ok(boxGetAgain2[k] === boxGetAgain1[k]);
}
return true;
})(), 'VarBox.createBox() with the same box name: firstBox');
ok((function() {
var box = VarBox.createBox('firstBox');
var otherBox1 = VarBox.createBox('secondBox');
var otherBox2 = VarBox.createBox({
BOX_NAME: 'secondBoxWithJSONConfiguration',
});
var otherBox3 = VarBox.createBox('');
var otherBox4 = VarBox.createBox();
var noneArgumentError = null;
var grabVarBox1 = null;
try {
grabVarBox1 = VarBox.grabVarBox();
} catch (error) {
noneArgumentError = error;
}
ok(noneArgumentError instanceof Error);
ok(grabVarBox1 === null);
var grabVarBox2 = VarBox.grabVarBox('grabVarBox2');
for (var k in box) {
ok(k in grabVarBox2);
ok(box[k] !== otherBox1[k]);
ok(box[k] !== otherBox2[k]);
ok(box[k] !== otherBox3[k]);
ok(box[k] !== otherBox4[k]);
ok(otherBox1[k] !== box[k]);
ok(otherBox1[k] !== otherBox2[k]);
ok(otherBox1[k] !== otherBox3[k]);
ok(otherBox1[k] !== otherBox4[k]);
ok(otherBox2[k] !== box[k]);
ok(otherBox2[k] !== otherBox1[k]);
ok(otherBox2[k] !== otherBox3[k]);
ok(otherBox2[k] !== otherBox4[k]);
ok(otherBox3[k] !== box[k]);
ok(otherBox3[k] !== otherBox1[k]);
ok(otherBox3[k] !== otherBox2[k]);
ok(otherBox3[k] !== otherBox4[k]);
ok(otherBox4[k] !== box[k]);
ok(otherBox4[k] !== otherBox1[k]);
ok(otherBox4[k] !== otherBox2[k]);
ok(otherBox4[k] !== otherBox3[k]);
}
return true;
})(), 'VarBox.createBox() with different box name');
ok((function(){
var box = VarBox.createBox();
var boxGetAgain1 = VarBox.createBox('');
var boxGetAgain2 = VarBox.createBox({
BOX_NAME: '',
});
var boxGetAgain3 = VarBox.createBox({});
for (var k in box) {
ok(box[k] !== boxGetAgain1[k]);
ok(box[k] !== boxGetAgain2[k]);
ok(box[k] !== boxGetAgain3[k]);
ok(boxGetAgain1[k] !== box[k]);
ok(boxGetAgain1[k] !== boxGetAgain2[k]);
ok(boxGetAgain1[k] !== boxGetAgain3[k]);
ok(boxGetAgain2[k] !== box[k]);
ok(boxGetAgain2[k] !== boxGetAgain1[k]);
ok(boxGetAgain2[k] !== boxGetAgain3[k]);
ok(boxGetAgain3[k] !== box[k]);
ok(boxGetAgain3[k] !== boxGetAgain1[k]);
ok(boxGetAgain3[k] !== boxGetAgain2[k]);
}
return true;
})(), 'VarBox.createBox() with empty box name');
box.watch(function (event) {
ok('object' === typeof event);
});
var unwatch = box.watchVariable(updateTestingKey, function (event) {
ok('function' === typeof unwatch);
unwatch();
ok('object' === typeof event);
ok('add' === event.eventType);
});
var newValue = 'new value 1';
var unwatch2 = box.watchPath(/^a\/b\/c\/d$/, (event) => {
ok(event.newValue === newValue);
ok('function' === typeof unwatch2);
unwatch2();
})
box.set(['a'], 'a1');
box.set(['a', 'b', 'c', 'd'], newValue);
ok((function() {
var newValue = { Tom: { age: 3 } };
box.update(updateTestingKey, function (sourceEvent) {
ok(2 === arguments.length);
sourceEvent.newValue = newValue;
})
ok(box.get(updateTestingKey) === newValue);
ok(box.get(updateTestingKey) !== undefined);
ok(box.get(updateTestingKey) !== null);
ok('object' === typeof box.get(updateTestingKey));
ok(box.get(updateTestingKey).Tom !== undefined);
ok(box.get(updateTestingKey).Tom !== null);
ok('object' === typeof box.get(updateTestingKey).Tom);
ok(box.get(updateTestingKey).Tom.age !== undefined);
ok(box.get(updateTestingKey).Tom.age !== null);
ok('number' === typeof box.get(updateTestingKey).Tom.age);
ok(3 === box.get(updateTestingKey).Tom.age);
return true;
})(), 'box.update() a new reference')
var unwatch = box.watchVariable(updateTestingKey, function (event) {
ok('function' === typeof unwatch);
unwatch();
ok('object' === typeof event);
ok('update' === event.eventType);
});
ok((function () {
ok(box.get(updateTestingKey).Tom.age !== undefined);
ok(box.get(updateTestingKey).Tom.age !== null);
ok('number' === typeof box.get(updateTestingKey).Tom.age);
ok(3 === box.get(updateTestingKey).Tom.age);
var oldAge = box.get(updateTestingKey).Tom.age;
box.update(updateTestingKey, function (sourceEvent) {
ok(2 === arguments.length);
ok(Object.prototype.hasOwnProperty.call(sourceEvent, 'exists'));
var oldValue = sourceEvent.oldValue;
oldValue.Tom.age += 1;
sourceEvent.newValue = oldValue;
return sourceEvent;
})
ok((oldAge + 1) === box.get(updateTestingKey).Tom.age);
return true;
})(), 'box.update() the old reference');
/*
todo('box.watchPath()');
todo('box.get()');
todo('box.set()');
todo('box.merge()');
*/
console.info('All testing case passed');