source: frontend/node_modules/tryer/test/unit.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 16.3 KB
Line 
1/*globals chai, spooks, require, tryer, suite, setup, test, setTimeout, Promise */
2
3(function (require, spooks) {
4 'use strict';
5
6 var assert, modulePath;
7
8 if (require === undefined) {
9 assert = chai.assert;
10 require = function () { return tryer; };
11 } else {
12 assert = require('chai').assert;
13 spooks = require('spooks');
14 modulePath = '../src/tryer';
15 }
16
17 suite('tryer:', function () {
18 test('require does not throw', function () {
19 assert.doesNotThrow(function () {
20 require(modulePath);
21 });
22 });
23
24 suite('require:', function () {
25 var tryer;
26
27 setup(function () {
28 tryer = require(modulePath);
29 });
30
31 test('function is exported', function () {
32 assert.isFunction(tryer);
33 });
34
35 test('tryer does not throw when options is missing', function () {
36 assert.doesNotThrow(function () {
37 tryer();
38 });
39 });
40
41 test('tryer does not throw when options is object', function () {
42 assert.doesNotThrow(function () {
43 tryer({});
44 });
45 });
46
47 suite('when passing immediately:', function () {
48 var log, predicate, action, fail, pass;
49
50 setup(function (done) {
51 log = {};
52 predicate = spooks.fn({ name: 'predicate', log: log, results: [ true ] });
53 action = spooks.fn({ name: 'action', log: log });
54 fail = spooks.fn({ name: 'fail', log: log, callback: done });
55 pass = spooks.fn({ name: 'pass', log: log, callback: done });
56 tryer({
57 when: predicate,
58 action: action,
59 fail: fail,
60 pass: pass,
61 interval: 0,
62 limit: 3
63 });
64 });
65
66 test('predicate was called once', function () {
67 assert.strictEqual(log.counts.predicate, 1);
68 });
69
70 test('action was called once', function () {
71 assert.strictEqual(log.counts.action, 1);
72 });
73
74 test('fail was not called', function () {
75 assert.strictEqual(log.counts.fail, 0);
76 });
77
78 test('pass was called once', function () {
79 assert.strictEqual(log.counts.pass, 1);
80 });
81 });
82
83 suite('when failing three times:', function () {
84 var log, predicate, action, fail, pass;
85
86 setup(function (done) {
87 log = {};
88 predicate = spooks.fn({ name: 'predicate', log: log, results: [ false ] });
89 action = spooks.fn({ name: 'action', log: log });
90 fail = spooks.fn({ name: 'fail', log: log, callback: done });
91 pass = spooks.fn({ name: 'pass', log: log, callback: done });
92 tryer({
93 when: predicate,
94 action: action,
95 fail: fail,
96 pass: pass,
97 interval: 0,
98 limit: 3
99 });
100 });
101
102 test('predicate was called three times', function () {
103 assert.strictEqual(log.counts.predicate, 3);
104 });
105
106 test('action was not called', function () {
107 assert.strictEqual(log.counts.action, 0);
108 });
109
110 test('fail was called once', function () {
111 assert.strictEqual(log.counts.fail, 1);
112 });
113
114 test('pass was not called', function () {
115 assert.strictEqual(log.counts.pass, 0);
116 });
117 });
118
119 suite('when failing five times:', function () {
120 var log, predicate, action, fail;
121
122 setup(function (done) {
123 log = {};
124 predicate = spooks.fn({ name: 'predicate', log: log, results: [ false ] });
125 action = spooks.fn({ name: 'action', log: log, callback: done });
126 fail = spooks.fn({ name: 'fail', log: log, callback: done });
127 tryer({ when: predicate, action: action, fail: fail, interval: 0, limit: 5 });
128 });
129
130 test('predicate was called five times', function () {
131 assert.strictEqual(log.counts.predicate, 5);
132 });
133
134 test('action was not called', function () {
135 assert.strictEqual(log.counts.action, 0);
136 });
137
138 test('fail was called once', function () {
139 assert.strictEqual(log.counts.fail, 1);
140 });
141 });
142
143 suite('when failing exponentially:', function () {
144 var log, timestamps, predicate, action, fail;
145
146 setup(function (done) {
147 log = {};
148 timestamps = [];
149 predicate = spooks.fn({
150 name: 'predicate',
151 log: log,
152 results: [ false ],
153 callback: function () {
154 timestamps.push(Date.now());
155 }
156 });
157 action = spooks.fn({ name: 'action', log: log, callback: done });
158 fail = spooks.fn({ name: 'fail', log: log, callback: done });
159 timestamps.push(Date.now());
160 tryer({ when: predicate, action: action, fail: fail, interval: -10, limit: 4 });
161 });
162
163 test('five timestamps were recorded', function () {
164 assert.lengthOf(timestamps, 5);
165 });
166
167 test('first interval is immediate', function () {
168 assert.isTrue(timestamps[1] < timestamps[0] + 5);
169 });
170
171 test('second interval is about 10 ms', function () {
172 assert.isTrue(timestamps[2] >= timestamps[1] + 10);
173 assert.isTrue(timestamps[2] < timestamps[1] + 15);
174 });
175
176 test('third interval is about 20 ms', function () {
177 assert.isTrue(timestamps[3] >= timestamps[2] + 20);
178 assert.isTrue(timestamps[3] < timestamps[2] + 30);
179 });
180
181 test('fourth interval is about 40 ms', function () {
182 assert.isTrue(timestamps[4] >= timestamps[3] + 40);
183 assert.isTrue(timestamps[4] < timestamps[3] + 50);
184 });
185 });
186
187 suite('until passing immediately:', function () {
188 var log, predicate, action, fail, pass;
189
190 setup(function (done) {
191 log = {};
192 predicate = spooks.fn({ name: 'predicate', log: log, results: [ true ] });
193 action = spooks.fn({ name: 'action', log: log });
194 fail = spooks.fn({ name: 'fail', log: log, callback: done });
195 pass = spooks.fn({ name: 'pass', log: log, callback: done });
196 tryer({
197 until: predicate,
198 action: action,
199 fail: fail,
200 pass: pass,
201 interval: 0,
202 limit: 3
203 });
204 });
205
206 test('predicate was called once', function () {
207 assert.strictEqual(log.counts.predicate, 1);
208 });
209
210 test('action was called once', function () {
211 assert.strictEqual(log.counts.action, 1);
212 });
213
214 test('fail was not called', function () {
215 assert.strictEqual(log.counts.fail, 0);
216 });
217
218 test('pass was called once', function () {
219 assert.strictEqual(log.counts.pass, 1);
220 });
221
222 test('pass was called once', function () {
223 assert.strictEqual(log.counts.pass, 1);
224 });
225 });
226
227 suite('until failing three times:', function () {
228 var log, predicate, action, fail, pass;
229
230 setup(function (done) {
231 log = {};
232 predicate = spooks.fn({ name: 'predicate', log: log, results: [ false ] });
233 action = spooks.fn({ name: 'action', log: log });
234 fail = spooks.fn({ name: 'fail', log: log, callback: done });
235 pass = spooks.fn({ name: 'pass', log: log, callback: done });
236 tryer({
237 until: predicate,
238 action: action,
239 fail: fail,
240 pass: pass,
241 interval: 0,
242 limit: 3
243 });
244 });
245
246 test('predicate was called three times', function () {
247 assert.strictEqual(log.counts.predicate, 3);
248 });
249
250 test('action was called three times', function () {
251 assert.strictEqual(log.counts.action, 3);
252 });
253
254 test('fail was called once', function () {
255 assert.strictEqual(log.counts.fail, 1);
256 });
257
258 test('pass was not called', function () {
259 assert.strictEqual(log.counts.pass, 0);
260 });
261 });
262
263 suite('until failing five times:', function () {
264 var log, predicate, action, fail;
265
266 setup(function (done) {
267 log = {};
268 predicate = spooks.fn({ name: 'predicate', log: log, results: [ false ] });
269 action = spooks.fn({ name: 'action', log: log });
270 fail = spooks.fn({ name: 'fail', log: log, callback: done });
271 tryer({ until: predicate, action: action, fail: fail, interval: 0, limit: 5 });
272 });
273
274 test('predicate was called five times', function () {
275 assert.strictEqual(log.counts.predicate, 5);
276 });
277
278 test('action was called five times', function () {
279 assert.strictEqual(log.counts.action, 5);
280 });
281
282 test('fail was called once', function () {
283 assert.strictEqual(log.counts.fail, 1);
284 });
285 });
286
287 suite('until failing exponentially:', function () {
288 var log, timestamps, predicate, action, fail;
289
290 setup(function (done) {
291 log = {};
292 timestamps = [];
293 predicate = spooks.fn({
294 name: 'predicate',
295 log: log,
296 results: [ false ],
297 callback: function () {
298 timestamps.push(Date.now());
299 }
300 });
301 action = spooks.fn({ name: 'action', log: log });
302 fail = spooks.fn({ name: 'fail', log: log, callback: done });
303 timestamps.push(Date.now());
304 tryer({ until: predicate, action: action, fail: fail, interval: -10, limit: 4 });
305 });
306
307 test('five timestamps were recorded', function () {
308 assert.lengthOf(timestamps, 5);
309 });
310
311 test('first interval is immediate', function () {
312 assert.isTrue(timestamps[1] < timestamps[0] + 5);
313 });
314
315 test('second interval is about 10 ms', function () {
316 assert.isTrue(timestamps[2] >= timestamps[1] + 10);
317 assert.isTrue(timestamps[2] < timestamps[1] + 20);
318 });
319
320 test('third interval is about 20 ms', function () {
321 assert.isTrue(timestamps[3] >= timestamps[2] + 20);
322 assert.isTrue(timestamps[3] < timestamps[2] + 30);
323 });
324
325 test('fourth interval is about 40 ms', function () {
326 assert.isTrue(timestamps[4] >= timestamps[3] + 40);
327 assert.isTrue(timestamps[4] < timestamps[3] + 50);
328 });
329 });
330
331 suite('when failing once then passing and until failing twice then passing', function () {
332 var log, predicateLoggers, predicates, action, fail, pass;
333
334 setup(function (done) {
335 log = {};
336 predicateLoggers = {
337 when: spooks.fn({ name: 'when', log: log }),
338 until: spooks.fn({ name: 'until', log: log })
339 };
340 predicates = {
341 when: function () {
342 predicateLoggers.when.apply(this, arguments);
343 if (log.counts.when === 1) {
344 return false;
345 }
346 return true;
347 },
348 until: function () {
349 predicateLoggers.until.apply(this, arguments);
350 if (log.counts.until < 3) {
351 return false;
352 }
353 return true;
354 }
355 };
356 action = spooks.fn({ name: 'action', log: log });
357 fail = spooks.fn({ name: 'fail', log: log, callback: done });
358 pass = spooks.fn({ name: 'pass', log: log, callback: done });
359 tryer({
360 when: predicates.when,
361 until: predicates.until,
362 action: action,
363 fail: fail,
364 pass: pass,
365 interval: 0,
366 limit: 4
367 });
368 });
369
370 test('when was called twice', function () {
371 assert.strictEqual(log.counts.when, 2);
372 });
373
374 test('action was called three times', function () {
375 assert.strictEqual(log.counts.action, 3);
376 });
377
378 test('until was called three times', function () {
379 assert.strictEqual(log.counts.until, 3);
380 });
381
382 test('fail was not called', function () {
383 assert.strictEqual(log.counts.fail, 0);
384 });
385
386 test('pass was called once', function () {
387 assert.strictEqual(log.counts.pass, 1);
388 });
389 });
390
391 suite('asynchronous action:', function () {
392 var log, timestamps, predicate, action;
393
394 setup(function (done) {
395 log = {};
396 timestamps = [];
397 predicate = function () {
398 timestamps.push(Date.now());
399 return false;
400 };
401 action = function (tryerDone) {
402 setTimeout(tryerDone, 10);
403 };
404 timestamps.push(Date.now());
405 tryer({ until: predicate, action: action, fail: done, interval: 0, limit: 3 });
406 });
407
408 test('four timestamps were recorded', function () {
409 assert.lengthOf(timestamps, 4);
410 });
411
412 test('first interval is about 10 ms', function () {
413 assert.isTrue(timestamps[1] >= timestamps[0] + 10);
414 assert.isTrue(timestamps[1] < timestamps[0] + 20);
415 });
416
417 test('second interval is about 10 ms', function () {
418 assert.isTrue(timestamps[2] >= timestamps[1] + 10);
419 assert.isTrue(timestamps[2] < timestamps[1] + 20);
420 });
421
422 test('third interval is about 10 ms', function () {
423 assert.isTrue(timestamps[3] >= timestamps[2] + 10);
424 assert.isTrue(timestamps[3] < timestamps[2] + 20);
425 });
426 });
427
428 if (typeof Promise === 'function') {
429 suite('promise-resolving action:', function () {
430 var log, timestamps, predicate, action;
431
432 setup(function (done) {
433 log = {};
434 timestamps = [];
435 predicate = function () {
436 timestamps.push(Date.now());
437 return false;
438 };
439 action = function () {
440 return new Promise(function (resolve) {
441 setTimeout(resolve, 10);
442 });
443 };
444 timestamps.push(Date.now());
445 tryer({ until: predicate, action: action, fail: done, interval: 0, limit: 3 });
446 });
447
448 test('four timestamps were recorded', function () {
449 assert.lengthOf(timestamps, 4);
450 });
451
452 test('first interval is about 10 ms', function () {
453 assert.isTrue(timestamps[1] >= timestamps[0] + 10);
454 assert.isTrue(timestamps[1] < timestamps[0] + 20);
455 });
456
457 test('second interval is about 10 ms', function () {
458 assert.isTrue(timestamps[2] >= timestamps[1] + 10);
459 assert.isTrue(timestamps[2] < timestamps[1] + 20);
460 });
461
462 test('third interval is about 10 ms', function () {
463 assert.isTrue(timestamps[3] >= timestamps[2] + 10);
464 assert.isTrue(timestamps[3] < timestamps[2] + 20);
465 });
466 });
467
468 suite('promise-rejecting action:', function () {
469 var log, timestamps, predicate, action;
470
471 setup(function (done) {
472 log = {};
473 timestamps = [];
474 predicate = function () {
475 timestamps.push(Date.now());
476 return false;
477 };
478 action = function () {
479 return new Promise(function (_, reject) {
480 setTimeout(reject, 10);
481 });
482 };
483 timestamps.push(Date.now());
484 tryer({ until: predicate, action: action, fail: done, interval: 0, limit: 3 });
485 });
486
487 test('four timestamps were recorded', function () {
488 assert.lengthOf(timestamps, 4);
489 });
490
491 test('first interval is about 10 ms', function () {
492 assert.isTrue(timestamps[1] >= timestamps[0] + 10);
493 assert.isTrue(timestamps[1] < timestamps[0] + 20);
494 });
495
496 test('second interval is about 10 ms', function () {
497 assert.isTrue(timestamps[2] >= timestamps[1] + 10);
498 assert.isTrue(timestamps[2] < timestamps[1] + 20);
499 });
500
501 test('third interval is about 10 ms', function () {
502 assert.isTrue(timestamps[3] >= timestamps[2] + 10);
503 assert.isTrue(timestamps[3] < timestamps[2] + 20);
504 });
505 });
506 }
507 });
508 });
509}(
510 typeof require === 'function' ? require : undefined,
511 typeof spooks === 'object' ? spooks : undefined)
512);
513
Note: See TracBrowser for help on using the repository browser.