1 | import chai = require('chai');
|
2 | import Promise = require('bluebird');
|
3 | import async = require('../src/async/index');
|
4 | import await = require('../src/await/index');
|
5 | var expect = chai.expect;
|
6 |
|
7 |
|
8 | describe('A suspendable function returned by async.cps(...)', () => {
|
9 |
|
10 | it('synchronously returns nothing', () => {
|
11 | var foo = async.cps (() => {});
|
12 | var syncResult = foo(()=>{});
|
13 | expect(syncResult).to.not.exist;
|
14 | });
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | it('begins executing synchronously and completes asynchronously', done => {
|
27 | var x = 5;
|
28 | var foo = async.cps (() => { x = 7; });
|
29 | Promise.promisify(foo)()
|
30 | .then(() => expect(x).to.equal(9))
|
31 | .then(() => done())
|
32 | .catch(done);
|
33 | expect(x).to.equal(7);
|
34 | x = 9;
|
35 | });
|
36 |
|
37 | it("preserves the 'this' context of the call", done => {
|
38 | var foo = { bar: async.cps (function () { return this; }) }, baz = {x:7};
|
39 | Promise.promisify(foo.bar.bind(foo))()
|
40 | .then(result => expect(result).to.equal(foo))
|
41 | .then(() => Promise.promisify(foo.bar).call(baz))
|
42 | .then(result => expect(result).to.equal(baz))
|
43 | .then(() => done())
|
44 | .catch(done);
|
45 | });
|
46 |
|
47 | it('eventually resolves with its definition\'s returned value', done => {
|
48 | var foo = async.cps (() => { return 'blah'; });
|
49 | Promise.promisify(foo)()
|
50 | .then(result => expect(result).to.equal('blah'))
|
51 | .then(() => done())
|
52 | .catch(done);
|
53 | });
|
54 |
|
55 | it('eventually rejects with its definition\'s thrown value', done => {
|
56 | var act, exp = new Error('Expected thrown value to match rejection value');
|
57 | var foo = async.cps (() => { throw exp; return 'blah'; });
|
58 | Promise.promisify(foo)()
|
59 | .catch(err => act = err)
|
60 | .then(() => {
|
61 | if (!act) done(new Error("Expected function to throw"))
|
62 | else if (act.message !== exp.message) done(exp);
|
63 | else done();
|
64 | });
|
65 | });
|
66 |
|
67 | it('works with await', done => {
|
68 | var foo = async.cps (() => { return await (Promise.delay(20).then(() => 'blah')); });
|
69 | Promise.promisify(foo)()
|
70 | .then(result => expect(result).to.equal('blah'))
|
71 | .then(() => done())
|
72 | .catch(done);
|
73 | });
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | });
|