UNPKG

4.35 kBJavaScriptView Raw
1/* global describe, it */
2'use strict';
3
4delete require.cache[require.resolve('..')];
5var batch = require('..');
6var assert = require('power-assert');
7var async = require('async');
8
9describe('glob-batch', function () {
10 it('should support domains `on(\'error\', ...)` without callback', function (done) {
11 var domain = require('domain').create();
12 domain.on('error', function (err) {
13 done();
14 });
15 var receiver = domain.bind(batch({ timeout: 10 }, function () {
16 throw new Error('Bang!');
17 }));
18 receiver('one');
19 });
20
21 it('should support domains `on(\'error\', ...)` with callback', function (done) {
22 var domain = require('domain').create();
23 domain.on('error', function (err) {
24 done();
25 });
26 var receiver = domain.bind(batch({ timeout: 10 }, function (events, async) {
27 async(new Error('Bang!'));
28 }));
29 receiver('one');
30 });
31
32 it('should support domains `on(\'error\', ...)` with callback, but with throw', function (done) {
33 var domain = require('domain').create();
34 domain.on('error', function (err) {
35 done();
36 });
37 var receiver = domain.bind(batch({ timeout: 10 }, function (events, async) {
38 throw new Error('Bang!');
39 }));
40 receiver('one');
41 });
42
43 it('should batch sync calls to array', function (done) {
44 var receiver = batch({ timeout: 10 }, function (events) {
45 assert.equal(events.length, 2);
46 done();
47 });
48 receiver('one');
49 receiver('two');
50 });
51
52 it('should batch async calls to array', function (done) {
53 var receiver = batch({ timeout: 10 }, function (events) {
54 assert.equal(events.length, 2);
55 done();
56 });
57 receiver('one');
58 setTimeout(receiver.bind(null, 'two'), 5);
59 });
60
61 it('should flush, if we exceed timeout', function (done) {
62 var iterator = async.iterator([
63 function (events) { assert.equal(events.length, 1); },
64 function () { done(); }
65 ]);
66
67 var receiver = batch({ timeout: 5 }, function (events) {
68 iterator = iterator(events);
69 });
70
71 receiver('one');
72 setTimeout(receiver.bind(null, 'two'), 10);
73 });
74
75 it('should flush, if we exceed limit', function (done) {
76 var iterator = async.iterator([
77 function (events) { assert.equal(events.length, 2); },
78 function (events) {
79 assert.equal(events.length, 1);
80 done();
81 }
82 ]);
83 var receiver = batch({ timeout: 10, limit: 2 }, function (events) {
84 iterator = iterator(events);
85 });
86 receiver('one');
87 receiver('two');
88 receiver('three');
89 });
90
91 it('should support done callback function', function (done) {
92 var iterator = async.iterator([
93 function (events, cb) {
94 receiver('two');
95 setTimeout(function () {
96 cb();
97 receiver('three');
98 }, 15);
99 },
100 function (events) {
101 assert.equal(events.length, 2);
102 done();
103 }
104 ]);
105 var receiver = batch({ timeout: 10 }, function (events, cb) {
106 iterator = iterator(events, cb);
107 });
108 receiver('one');
109 });
110
111 it('should support debounce option', function (done) {
112 var iterator = async.iterator([
113 function (events, cb) {
114 receiver('two');
115 setTimeout(function () {
116 cb();
117 setTimeout(receiver.bind(null, 'three'), 15);
118 }, 15);
119 },
120 function (events) {
121 assert.equal(events.length, 2);
122 done();
123 }
124 ]);
125 var receiver = batch({ timeout: 10, debounce: 10 }, function (events, cb) {
126 iterator = iterator(events, cb);
127 });
128 receiver('one');
129 });
130
131 it('should throw, if we provide invalid callback', function () {
132 assert.throws(batch, /Provided callback is not a function/);
133 assert.throws(batch.bind(null, 'string'), /Provided callback is not a function/);
134 });
135});
136
\No newline at end of file