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