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.thunk(...)', () => {
|
9 |
|
10 | it('synchronously returns a thunk', () => {
|
11 | var foo = async.thunk (() => {});
|
12 | var syncResult = foo();
|
13 | expect(syncResult).instanceOf(Function);
|
14 | expect(syncResult.length).to.equal(1);
|
15 | });
|
16 |
|
17 | it('does not execute if the thunk is not invoked', done => {
|
18 | var x = 5;
|
19 | var foo = async.thunk (() => { x = 7; });
|
20 | var thunk = foo();
|
21 | Promise.delay(50)
|
22 | .then(() => expect(x).to.equal(5))
|
23 | .then(() => done())
|
24 | .catch(done);
|
25 | expect(x).to.equal(5);
|
26 | });
|
27 |
|
28 | it('executes if the thunk is invoked without a callback', done => {
|
29 | var x = 5;
|
30 | var foo = async.thunk (() => { x = 7; });
|
31 | foo()();
|
32 | Promise.delay(20)
|
33 | .then(result => expect(x).to.equal(7))
|
34 | .then(() => done())
|
35 | .catch(done);
|
36 | });
|
37 |
|
38 | it('begins executing synchronously and completes asynchronously', done => {
|
39 | var x = 5;
|
40 | var foo = async.thunk (() => { x = 7; });
|
41 | Promise.promisify(foo())()
|
42 | .then(() => expect(x).to.equal(9))
|
43 | .then(() => done())
|
44 | .catch(done);
|
45 | expect(x).to.equal(7);
|
46 | x = 9;
|
47 | });
|
48 |
|
49 | it("preserves the 'this' context of the call", done => {
|
50 | var foo = { bar: async.thunk (function () { return this; }) }, baz = {x:7};
|
51 | Promise.promisify(foo.bar.call(foo))()
|
52 | .then(result => expect(result).to.equal(foo))
|
53 | .then(() => Promise.promisify(foo.bar.call(baz))())
|
54 | .then(result => expect(result).to.equal(baz))
|
55 | .then(() => done())
|
56 | .catch(done);
|
57 | });
|
58 |
|
59 | it('eventually resolves with its definition\'s returned value', done => {
|
60 | var foo = async.thunk (() => { return 'blah'; });
|
61 | Promise.promisify(foo())()
|
62 | .then(result => expect(result).to.equal('blah'))
|
63 | .then(() => done())
|
64 | .catch(done);
|
65 | });
|
66 |
|
67 | it('eventually rejects with its definition\'s thrown value', done => {
|
68 | var act, exp = new Error('Expected thrown value to match rejection value');
|
69 | var foo = async.thunk (() => { throw exp; return 'blah'; });
|
70 | Promise.promisify(foo())()
|
71 | .catch(err => act = err)
|
72 | .then(() => {
|
73 | if (!act) done(new Error("Expected function to throw"))
|
74 | else if (act.message !== exp.message) done(exp);
|
75 | else done();
|
76 | });
|
77 | });
|
78 |
|
79 | it('works with await', done => {
|
80 | var foo = async.thunk (() => { return await (Promise.delay(20).then(() => 'blah')); });
|
81 | Promise.promisify(foo())()
|
82 | .then(result => expect(result).to.equal('blah'))
|
83 | .then(() => done())
|
84 | .catch(done);
|
85 | });
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | });
|