UNPKG

6 kBPlain TextView Raw
1import chai = require('chai');
2import Promise = require('bluebird');
3import async = require('../src/async/index');
4import await = require('../src/await/index');
5var expect = chai.expect;
6
7
8describe('async.iterable(...)', () => {
9
10 var foo = async.iterable ((yield_, count: number, accum?: any[]) => {
11 if (count < 1 || count > 9) throw new Error('out of range');
12 for (var i = 1; i <= count; ++i) {
13 if (accum) accum.push(111 * i);
14 yield_ (111 * i);
15 }
16 return 'done';
17 });
18
19
20 describe('returns a function', () => {
21
22 it('which returns an async iterator with next() and forEach() methods', () => {
23 var syncResult = foo();
24 expect(syncResult).is.an('object');
25 expect(syncResult.next).is.a('function');
26 expect(syncResult.forEach).is.a('function');
27 });
28 });
29
30
31 describe('provides an iterator whose next() method', () => {
32
33 it('synchronously returns a promise', () => {
34 var iter = foo(3);
35 expect(iter.next()).instanceOf(Promise);
36 });
37
38 it('begins executing synchronously and completes asynchronously', done => {
39 var arr = [], iter = foo(3, arr);
40 (<Promise<any>> iter.next())
41 .then(() => expect(arr).to.deep.equal([111, '***']))
42 .then(() => done())
43 .catch(done);
44 expect(arr).to.deep.equal([111]);
45 arr.push('***');
46 });
47
48 it("preserves the 'this' context of the call", async.cps (() => {
49 var foo = { bar: async.iterable (function (yield_) { yield_ (this); return 'done'; }) }, baz = {x:7};
50 var iter = foo.bar();
51 expect(await (iter.next())).to.deep.equal({ done: false, value: foo });
52 expect(await (iter.next())).to.deep.equal({ done: true });
53 iter = foo.bar.call(baz);
54 expect(await (iter.next())).to.deep.equal({ done: false, value: baz });
55 expect(await (iter.next())).to.deep.equal({ done: true });
56 }));
57
58 it('eventually resolves with the definition\'s yielded value', async.cps (() => {
59 var iter = foo(3);
60 expect(await (iter.next())).to.deep.equal({ done: false, value: 111 });
61 expect(await (iter.next())).to.deep.equal({ done: false, value: 222 });
62 expect(await (iter.next())).to.deep.equal({ done: false, value: 333 });
63 expect(await (iter.next())).to.deep.equal({ done: true });
64 }));
65
66 it('eventually rejects with the definition\'s thrown value', async.cps (() => {
67 var err, iter = foo(20);
68 expect(() => await (iter.next())).to.throw(Error, 'out of range');
69 }));
70
71 it('eventually rejects if the iteration is already finished', async.cps(() => {
72 var err, iter = foo(1);
73 expect(await (iter.next())).to.deep.equal({ done: false, value: 111 });
74 expect(await (iter.next())).to.deep.equal({ done: true });
75 expect(() => await (iter.next())).to.throw(Error);
76 }));
77
78 it('works with await', done => {
79 var foo = async.iterable ((yield_) => { yield_ (await (Promise.delay(20).then(() => 'blah'))); });
80 foo().next()
81 .then(result => expect(result).to.deep.equal({ done:false, value:'blah' }))
82 .then(() => done())
83 .catch(done);
84 });
85 });
86
87
88 describe('provides an iterator whose forEach() method', () => {
89
90 function nullFunc() { }
91
92 //it('expects a single callback as its argument', () => {
93 // expect(() => (<any> foo(3)).forEach()).to.throw(Error);
94 // expect(() => (<any> foo(3)).forEach(1)).to.throw(Error);
95 // expect(() => (<any> foo(3)).forEach(1, nullFunc)).to.throw(Error);
96 //});
97
98 it('synchronously returns a promise', () => {
99 var iter = foo(3);
100 expect(iter.forEach(nullFunc)).instanceOf(Promise);
101 });
102
103 it('begins executing synchronously and completes asynchronously', done => {
104 var arr = [], iter = foo(3, arr);
105 (<Promise<any>> iter.forEach(nullFunc))
106 .then(() => expect(arr).to.deep.equal([111, '***', 222, 333]))
107 .then(() => done())
108 .catch(done);
109 expect(arr).to.deep.equal([111]);
110 arr.push('***');
111 });
112
113 it('iterates over all yielded values', async.cps(() => {
114 var arr = [], iter = foo(4);
115 await (iter.forEach(val => arr.push(val)));
116 expect(arr).to.deep.equal([111, 222, 333, 444]);
117 }));
118
119 //it('eventually resolves with the definition\'s returned value', async.cps(() => {
120 // var arr = [], iter = foo(7, arr);
121 // var result = await (iter.forEach(nullFunc));
122 // expect(result).to.equal('done');
123 // expect(arr.length).to.equal(7);
124 //}));
125
126 it('eventually rejects with the definition\'s thrown value', async.cps(() => {
127 var err, iter = foo(20);
128 expect(() => await (iter.forEach(nullFunc))).to.throw(Error, 'out of range');
129 }));
130
131 it('eventually rejects if the iteration is already finished', async.cps(() => {
132 var err, iter = foo(1);
133 await (iter.forEach(nullFunc));
134 expect (() => await (iter.forEach(nullFunc))).to.throw(Error);
135 }));
136
137 it('works with await', done => {
138 var foo = async.iterable ((yield_) => { yield_ (await (Promise.delay(20).then(() => 'blah'))); }), arr = [];
139 foo().forEach(val => arr.push(val))
140 .then(result => expect(result).to.not.exist)
141 .then(() => expect(arr).to.deep.equal(['blah']))
142 .then(() => done())
143 .catch(done);
144 });
145 });
146});