-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
507 lines (441 loc) · 13.8 KB
/
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
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
var V = require('./form-validation-kit');
var assert = require("assert");
var Promise = require("bluebird");
var _ = require("lodash");
function expectOk(value, done) {
setTimeout(function() {
done(value === "ok");
}, 0)
}
function min3(value, done) {
setTimeout(function() {
done(value.length >= 3, 'Value must be at least 3 long, got ' + value);
}, 0)
}
function validWithDelay(delay) {
return function(_, done, error) {
setTimeout(function(){
done(true);
}, delay);
}
}
function validResponse(data) {
return function(value, done, error) {
setTimeout(function(){
done(true, data);
}, 10);
}
}
function alwaysValid(_, done, error) {
setTimeout(function(){
done(true);
}, 0);
}
function aInvalidWith(result) {
return function(value, done, error) {
done(false, result);
}
}
function aValidWith(result) {
return function(value, done, error) {
done(true, result);
}
}
function expectError(value, done, error) {
setTimeout(function() {
error("Problem")
}, 0)
}
function synchronousValid(_) {
return;
}
function seq() {
var promises = arguments;
return function(value) {
var sequence = Array.prototype.slice.apply(promises).reduce(function(agg, arg) {
return agg.then(arg);
}, new Promise(function(r) {r(value)}));
return sequence.return(value);
}
}
function call(fn) {
return function() {
fn()
};
}
function unwrap(x) {
if (typeof(x) === 'function') {
return x();
} else {
return x;
}
}
function eq(/*...values*/) {
var items = Array.prototype.slice.call(arguments);
if (items.length < 2) {
throw new Error('Must give at least two arguments to compare, got ' + items.length);
}
return function() {
var first = unwrap(items[0]);
return items.map(unwrap).reduce(function(agg, it) {
return agg && _.isEqual(first, it);
}, true);
}
}
function register(form, validator, opts) {
var registerArgs = Array.prototype.slice.apply(arguments).slice(1);
return function() {
return form.register.apply(this, registerArgs);
}
}
function evaluate(value) {
return function(validator) {
validator.evaluate(value || "");
return validator;
}
}
function evaluateFor(validator, value) {
return seq(
function() { return validator; },
evaluate(value));
}
function log(msg) {
return function(value) {
console.log(msg, value);
return value;
}
}
function wrap(ptr) {
return function() {
return ptr;
}
}
function last(fn) {
return function() {
return fn().slice(-1);
};
}
function isTrue(fn) {
return function(value) {
assert.equal(fn(), true);
return value;
}
}
function sleep(ms) {
return function(value) {
return (new Promise(function(resolve) {
setTimeout(resolve, ms);
})).return(value);
}
}
function poll(/*...checkCbs*/) {
var checkCbs = Array.prototype.slice.apply(arguments);
return function(value) {
return new Promise(function(resolve) {
var timer = -1;
function loop() {
if (checkCbs.reduce(function(agg, cb) {
return agg && cb();
}, true)) {
clearTimeout(timer);
resolve(value);
}
}
timer = setInterval(loop, 100);
loop();
});
};
}
function pushState(array) {
return function(state) {
array.push(state);
}
}
function pushResponse(array) {
return function(response) {
array.push(response);
}
}
var nop = function() {};
describe('Input for asynchronous validator', function() {
it('triggers Valid state', function(done) {
var createStates = [V.Queued, V.Validating, V.Valid];
var form = V.create(function(state) {
assert.equal(state, createStates.shift(1));
if (state === V.Valid) {
done();
}
}, expectOk, {throttle: 10});
form.evaluate("ok");
});
it('minimum idle time for triggering validation can be set', function(done) {
var m = function(x) { return x * 10 };
var threshold = 5;
var combinedStates = [];
var form = V.create(pushState(combinedStates), alwaysValid, {throttle: m(threshold)});
seq(function() { return form; },
evaluate(),
sleep(m(2)),
evaluate(),
sleep(m(3)),
evaluate(),
sleep(m(4)),
isTrue(eq(combinedStates, [V.Queued])),
sleep(m(threshold + 1)),
isTrue(eq(combinedStates, [V.Queued, V.Validating, V.Valid])),
call(done))();
});
it('triggers evaluation callback based on correct state', function(done) {
var combinedStates = [];
var m = function(x) { return x * 10 };
var threshold = 1;
var form = V.create(pushState(combinedStates), min3, {throttle: m(threshold)});
seq(function() { return form; },
evaluate("1"),
sleep(m(threshold + 1)),
evaluate("12"),
sleep(m(threshold + 1)),
evaluate("123"),
sleep(m(threshold + 1)),
isTrue(eq(combinedStates, [
V.Queued, V.Validating, V.Invalid, // 1
V.Queued, V.Validating, V.Invalid, // 12
V.Queued, V.Validating, V.Valid // 123
])),
call(done))();
});
it('while validating will revert back to queueing', function(done) {
var combinedStates = [];
var form = V.create(pushState(combinedStates), validWithDelay(100), {throttle: 10});
seq(function() { return form; },
evaluate(),
sleep(50), // Wait to start validation
evaluate(), // Interrupt ongoing validation
sleep(200), // Wait validation to resolve
isTrue(eq(combinedStates, [
V.Queued, V.Validating, V.Queued, V.Validating, V.Valid
])),
call(done))();
});
it('late responses get discarded', function(done) {
var combinedStates = [];
var form = V.create(pushState(combinedStates), validWithDelay(100), {throttle: 100});
seq(function() { return form; },
evaluate('a'),
sleep(150),
evaluate('b'),
sleep(300),
poll(eq(combinedStates, [
V.Queued, V.Validating, V.Queued, V.Validating, V.Valid
])),
call(done))();
});
it('can return an object to state callback', function(done) {
var combinedStates = [];
var responses = [];
var form = V.create(function(state, response) {
pushState(combinedStates)(state);
pushResponse(responses)(response) ;
}, validResponse({foo: "bar"}));
seq(function() { return form; },
evaluate(),
poll(eq(combinedStates, [
V.Validating, V.Valid
])),
poll(eq(responses, [
[], [{foo: "bar"}]
])),
call(done))();
});
});
describe('Input for synchronous validator', function() {
it('gets queued with throttling', function(done) {
var combinedStates = [];
var form = V.create(pushState(combinedStates), synchronousValid, {throttle: 100});
seq(function() { return form; },
evaluate(),
poll(eq(combinedStates, [
V.Queued, V.Valid
])),
call(done))();
});
it('leads to immediate valid state', function(done) {
var combinedStates = [];
var form = V.create(pushState(combinedStates), synchronousValid, {throttle: 0});
seq(function() { return form; },
evaluate(),
evaluate(),
evaluate(),
evaluate(),
evaluate(),
eq(combinedStates, [V.Valid]),
call(done))();
})
});
describe('Parent validator', function() {
it('events get inherited', function(done) {
var parentStates = [];
var childStates = [];
var child2States = [];
var parent = V.create(pushState(parentStates), alwaysValid);
var child = V.create(pushState(childStates), parent);
var child2 = V.create(pushState(child2States), parent);
seq(evaluateFor(parent),
poll(eq(parentStates, [
V.Validating, V.Valid
])),
function() { return child; },
poll(eq(childStates, [
V.Validating, V.Valid
])),
poll(eq(childStates, [
V.Validating, V.Valid
])),
function() { return child2; },
poll(eq(child2States, [
V.Validating, V.Valid
])),
call(done))();
});
describe('validator order', function() {
var parentStates, state, response, parent;
var stateToResponse = function(s, r) { state = s; response = r; };
beforeEach(function() {
parentStates = [];
response = null;
parent = V.create(pushState(parentStates), aValidWith('return1'));
});
it('responses are in validator order', function(done) {
var child = V.create(stateToResponse, parent, aInvalidWith('return2'));
seq(evaluateFor(parent),
evaluateFor(child),
poll(function() {
return response != null &&
state == 'invalid' &&
response.length == 2 &&
response[0] == 'return1' &&
response[1] == 'return2';
}),
call(done))();
});
it('responses are in validator order2', function(done) {
var child = V.create(stateToResponse, aInvalidWith('return2'), parent);
seq(evaluateFor(parent),
evaluateFor(child),
poll(function() {
return response != null &&
state == 'invalid' &&
response.length == 2 &&
response[0] == 'return2' &&
response[1] == 'return1'
}),
call(done))();
});
})
});
describe('Registration', function() {
function arityError(arity) {
return new RegExp("Synchronous validator type is Function\\(string\\), asynchronous type is Function\\(string, done\\(bool, string\\), error\\(string\\)\\), got function taking " + arity + " arguments.", "g");
}
it('throws exception with too few callback arguments', function() {
assert.throws(function() {
V.create(nop, nop);
}, arityError(0));
});
it('throws exception with too many callback arguments', function() {
assert.throws(function() {
V.create(nop, function(a, b, c, d) {});
}, arityError(4));
});
it('throws exception with zero validators', function() {
assert.throws(function() {
V.create(nop);
}, new RegExp("create\\(\\) requires a callback and at least one validator as argument"));
});
});
describe('Error', function() {
it('triggers Error state', function(done) {
var createStates = [V.Queued, V.Validating, V.Error];
var form = V.create(function(state) {
assert.equal(state, createStates.shift(1));
if (state === V.Error) {
done();
}
}, expectError, {throttle: 10});
var evalStates = [V.Queued, V.Validating, V.Error];
seq(function() { return form; },
evaluate(function(state) {
assert.equal(state, evalStates.shift(1));
}))();
});
});
describe('Validator', function() {
describe('with synchronized assertion', function() {
var states, responses;
beforeEach(function() {
states = [];
responses = [];
});
it('resolves to VALID when returning true', function(done) {
var validator = V.create(pushState(states), function(_) { return true; });
seq(evaluateFor(validator),
poll(eq(states, [V.Valid])),
call(done))();
});
it('resolves to INVALID when returning false', function(done) {
var validator = V.create(pushState(states), function(_) { return false; });
seq(evaluateFor(validator),
poll(eq(states, [V.Invalid])),
call(done))();
});
it('resolves to INVALID and response object when returning string', function(done) {
var validator = V.create(function(s, d) { states.push(s); responses.push(d); }, function(_) { return "error msg"; });
seq(evaluateFor(validator),
poll(eq(states, [V.Invalid])),
poll(eq(responses, [["error msg"]])),
call(done))();
});
it('resolves to ERROR and response object when throwing', function(done) {
var validator = V.create(function(s, d) { states.push(s); responses.push(d); }, function(_) { throw new Error("exception msg"); });
seq(evaluateFor(validator),
poll(eq(states, [V.Error])),
poll(eq(responses, [["exception msg"]])),
call(done))();
});
});
describe('with asynchronized assertion', function() {
var states, responses;
beforeEach(function() {
states = [];
responses = [];
});
it('resolves to VALID and response object with true', function(done) {
var validator = V.create(function(s, d) { states.push(s); responses.push(d); }, function(_, resolve) { resolve(true, "ok"); });
seq(evaluateFor(validator),
poll(eq(states, [V.Validating, V.Valid])),
poll(eq(responses, [[], ["ok"]])),
call(done))();
});
it('resolves to INVALID and response object with false', function(done) {
var validator = V.create(function(s, d) { states.push(s); responses.push(d); }, function(_, resolve) { resolve(false, "nok"); });
seq(evaluateFor(validator),
poll(eq(states, [V.Validating, V.Invalid])),
poll(eq(responses, [[], ["nok"]])),
call(done))();
});
it('resolves to ERROR and response object when calling error', function(done) {
var validator = V.create(function(s, d) { states.push(s); responses.push(d); }, function(_, resolve, reject) { reject("error msg"); });
seq(evaluateFor(validator),
poll(eq(states, [V.Validating, V.Error])),
poll(eq(responses, [[], ["error msg"]])),
call(done))();
});
it('resolves to ERROR and response object when throwing exception', function(done) {
var validator = V.create(function(s, d) { states.push(s); responses.push(d); }, function(_, resolve, reject) { throw new Error("exception msg"); });
seq(evaluateFor(validator),
poll(eq(states, [V.Validating, V.Error])),
poll(eq(responses, [[], ["exception msg"]])),
call(done))();
});
});
});