1 | var chai = require('chai');
|
2 | var Promise = require('bluebird');
|
3 | var async = require('../src/async/index');
|
4 | var await = require('../src/await/index');
|
5 | var expect = chai.expect;
|
6 |
|
7 | function runTestsFor(variant, acceptsCallback) {
|
8 | if (acceptsCallback === void 0) { acceptsCallback = false; }
|
9 | var name = 'async' + (variant ? ('.' + variant) : '');
|
10 | var func = async;
|
11 | if (variant)
|
12 | variant.split('.').forEach(function (prop) { return func = func[prop]; });
|
13 | var arity = function (fn) { return fn.length + (acceptsCallback ? 1 : 0); };
|
14 | describe('The ' + name + '(...) function', function () {
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | it('synchronously returns a function', function () {
|
22 | var foo = func(function () { });
|
23 | expect(foo).to.be.a('function');
|
24 | });
|
25 | it('returns a function whose arity matches that of its definition', function () {
|
26 | var defns = [
|
27 | function (a, b, c) { },
|
28 | function () { },
|
29 | function (a, b, c, d, e, f, g, h) { },
|
30 | function (x) { }
|
31 | ];
|
32 | for (var i = 0; i < defns.length; ++i) {
|
33 | var foo = func(defns[i]);
|
34 | expect(foo.length).to.equal(arity(defns[i]));
|
35 | }
|
36 | });
|
37 | });
|
38 | }
|
39 | runTestsFor(null);
|
40 | runTestsFor('cps', true);
|
41 | runTestsFor('thunk');
|
42 | runTestsFor('iterable');
|
43 | describe('A suspendable function returned by async(...)', function () {
|
44 | it('synchronously returns a promise', function () {
|
45 | var foo = async(function () { });
|
46 | var syncResult = foo();
|
47 | expect(syncResult).instanceOf(Promise);
|
48 | });
|
49 | it('begins executing synchronously and completes asynchronously', function (done) {
|
50 | var x = 5;
|
51 | var foo = async(function () { x = 7; });
|
52 | foo()
|
53 | .then(function () { return expect(x).to.equal(9); })
|
54 | .then(function () { return done(); })
|
55 | .catch(done);
|
56 | expect(x).to.equal(7);
|
57 | x = 9;
|
58 | });
|
59 | it("preserves the 'this' context of the call", function (done) {
|
60 | var foo = { bar: async(function () { return this; }) }, baz = { x: 7 };
|
61 | foo.bar()
|
62 | .then(function (result) { return expect(result).to.equal(foo); })
|
63 | .then(function () { return foo.bar.call(baz); })
|
64 | .then(function (result) { return expect(result).to.equal(baz); })
|
65 | .then(function () { return done(); })
|
66 | .catch(done);
|
67 | });
|
68 | it('eventually resolves with its definition\'s returned value', function (done) {
|
69 | var foo = async(function () { return 'blah'; });
|
70 | foo()
|
71 | .then(function (result) { return expect(result).to.equal('blah'); })
|
72 | .then(function () { return done(); })
|
73 | .catch(done);
|
74 | });
|
75 | it('eventually rejects with its definition\'s thrown value', function (done) {
|
76 | var act, exp = new Error('Expected thrown value to match rejection value');
|
77 | var foo = async(function () { throw exp; return 'blah'; });
|
78 | foo()
|
79 | .catch(function (err) { return act = err; })
|
80 | .then(function () {
|
81 | if (!act)
|
82 | done(new Error("Expected function to throw"));
|
83 | else if (act !== exp)
|
84 | done(exp);
|
85 | else
|
86 | done();
|
87 | });
|
88 | });
|
89 | it('works with await', function (done) {
|
90 | var foo = async(function () { return await(Promise.delay(20).then(function () { return 'blah'; })); });
|
91 | foo()
|
92 | .then(function (result) { return expect(result).to.equal('blah'); })
|
93 | .then(function () { return done(); })
|
94 | .catch(done);
|
95 | });
|
96 | it('triggers the global unhandledException event if a rejection goes unhandled', function (done) {
|
97 | var foo = async(function () { throw new Error('nobody handled me'); });
|
98 | foo();
|
99 | var didTrigger = false;
|
100 | process.on('unhandledRejection', function (err) {
|
101 | didTrigger = true;
|
102 | done();
|
103 | });
|
104 | setTimeout(function () {
|
105 | if (!didTrigger)
|
106 | done(new Error('unhandledRejection event not triggered'));
|
107 | }, 20);
|
108 | });
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 | });
|
120 |
|
\ | No newline at end of file |