UNPKG

2.65 kBJavaScriptView Raw
1var async_all = require('../lib/utils').async_all,
2 Sinon = require('sinon');
3
4exports.async_all = {
5 "calls callback with empty results if no requests": function(test) {
6 var requests = [],
7 handler_fn = Sinon.stub();
8
9 test.expect(2);
10 async_all(requests, handler_fn, function(error, results) {
11 test.equal(error, null, "error should be null");
12 test.equal(results.length, 0, "results should be empty array");
13 test.done();
14 });
15 },
16
17 "runs handler for each request and calls callback with results": function(test) {
18 var requests = [1, 2, 3],
19 handler_fn = Sinon.stub();
20
21 handler_fn
22 .onCall(0).callsArgWithAsync(1, null, 4)
23 .onCall(1).callsArgWithAsync(1, null, 5)
24 .onCall(2).callsArgWithAsync(1, null, 6);
25
26 test.expect(6);
27 async_all(requests, handler_fn, function(error, results) {
28 test.equal(handler_fn.callCount, requests.length, "handler function should be called for each request");
29 test.equal(handler_fn.getCall(0).args[0], 1, "handler called with request value");
30 test.equal(handler_fn.getCall(1).args[0], 2, "handler called with request value");
31 test.equal(handler_fn.getCall(2).args[0], 3, "handler called with request value");
32 test.equal(error, null, "error should be null");
33 test.deepEqual(results, [4, 5, 6], "results should be array of results from handler");
34 test.done();
35 });
36 },
37
38 "calls callback with errors and results from handler": function(test) {
39 var requests = [1, 2, 3],
40 handler_fn = Sinon.stub();
41
42 handler_fn
43 .onCall(0).callsArgWithAsync(1, 'error1', null)
44 .onCall(1).callsArgWithAsync(1, 'error2', null)
45 .onCall(2).callsArgWithAsync(1, null, 6);
46
47 test.expect(6);
48 async_all(requests, handler_fn, function(error, results) {
49 test.equal(handler_fn.callCount, requests.length, "handler function should be called for each request");
50 test.equal(handler_fn.getCall(0).args[0], 1, "handler called with request value");
51 test.equal(handler_fn.getCall(1).args[0], 2, "handler called with request value");
52 test.equal(handler_fn.getCall(2).args[0], 3, "handler called with request value");
53 test.deepEqual(error, ['error1', 'error2'], "errors should be returned in an array");
54 test.deepEqual(results, [null, null, 6], "results should be array of results from handler");
55 test.done();
56 });
57 }
58
59};
60
61