Skip to content

Commit b418ad6

Browse files
mapAsyncIterator: improve coverage (#2353)
1 parent 8c7fe9d commit b418ad6

File tree

1 file changed

+103
-26
lines changed

1 file changed

+103
-26
lines changed

src/subscription/__tests__/mapAsyncIterator-test.js

Lines changed: 103 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
import { expect } from 'chai';
77
import { describe, it } from 'mocha';
88

9+
import invariant from '../../jsutils/invariant';
10+
911
import mapAsyncIterator from '../mapAsyncIterator';
1012

1113
describe('mapAsyncIterator', () => {
12-
it('maps over async values', async () => {
14+
it('maps over async generator', async () => {
1315
async function* source() {
1416
yield 1;
1517
yield 2;
@@ -27,6 +29,49 @@ describe('mapAsyncIterator', () => {
2729
});
2830
});
2931

32+
it('maps over async iterator', async () => {
33+
const items = [1, 2, 3];
34+
35+
const iterator: any = {
36+
// $FlowFixMe Blocked by https://github.com/facebook/flow/issues/3258
37+
[Symbol.asyncIterator]() {
38+
return this;
39+
},
40+
next() {
41+
return Promise.resolve({
42+
done: items.length === 0,
43+
value: items.shift(),
44+
});
45+
},
46+
};
47+
48+
const doubles = mapAsyncIterator(iterator, x => x + x);
49+
50+
expect(await doubles.next()).to.deep.equal({ value: 2, done: false });
51+
expect(await doubles.next()).to.deep.equal({ value: 4, done: false });
52+
expect(await doubles.next()).to.deep.equal({ value: 6, done: false });
53+
expect(await doubles.next()).to.deep.equal({
54+
value: undefined,
55+
done: true,
56+
});
57+
});
58+
59+
it('compatible with for-await-of', async () => {
60+
async function* source() {
61+
yield 1;
62+
yield 2;
63+
yield 3;
64+
}
65+
66+
const doubles = mapAsyncIterator(source(), x => x + x);
67+
68+
const result = [];
69+
for await (const x of doubles) {
70+
result.push(x);
71+
}
72+
expect(result).to.deep.equal([2, 4, 6]);
73+
});
74+
3075
it('maps over async values with async function', async () => {
3176
async function* source() {
3277
yield 1;
@@ -49,10 +94,12 @@ describe('mapAsyncIterator', () => {
4994
});
5095
});
5196

52-
it('allows returning early from async values', async () => {
97+
it('allows returning early from mapped async generator', async () => {
5398
async function* source() {
5499
yield 1;
55100
yield 2;
101+
102+
/* istanbul ignore next (shouldn't be reached) */
56103
yield 3;
57104
}
58105

@@ -78,11 +125,41 @@ describe('mapAsyncIterator', () => {
78125
});
79126
});
80127

128+
it('allows returning early from mapped async iterator', async () => {
129+
const items = [1, 2, 3];
130+
131+
const iterator: any = {
132+
// $FlowFixMe Blocked by https://github.com/facebook/flow/issues/3258
133+
[Symbol.asyncIterator]() {
134+
return this;
135+
},
136+
next() {
137+
return Promise.resolve({
138+
done: items.length === 0,
139+
value: items.shift(),
140+
});
141+
},
142+
};
143+
144+
const doubles = mapAsyncIterator(iterator, x => x + x);
145+
146+
expect(await doubles.next()).to.deep.equal({ value: 2, done: false });
147+
expect(await doubles.next()).to.deep.equal({ value: 4, done: false });
148+
149+
// Early return
150+
expect(await doubles.return()).to.deep.equal({
151+
value: undefined,
152+
done: true,
153+
});
154+
});
155+
81156
it('passes through early return from async values', async () => {
82157
async function* source() {
83158
try {
84159
yield 1;
85160
yield 2;
161+
162+
/* istanbul ignore next (shouldn't be reached) */
86163
yield 3;
87164
} finally {
88165
yield 'Done';
@@ -112,14 +189,23 @@ describe('mapAsyncIterator', () => {
112189
});
113190
});
114191

115-
it('allows throwing errors through async generators', async () => {
116-
async function* source() {
117-
yield 1;
118-
yield 2;
119-
yield 3;
120-
}
192+
it('allows throwing errors through async iterators', async () => {
193+
const items = [1, 2, 3];
121194

122-
const doubles = mapAsyncIterator(source(), x => x + x);
195+
const iterator: any = {
196+
// $FlowFixMe Blocked by https://github.com/facebook/flow/issues/3258
197+
[Symbol.asyncIterator]() {
198+
return this;
199+
},
200+
next() {
201+
return Promise.resolve({
202+
done: items.length === 0,
203+
value: items.shift(),
204+
});
205+
},
206+
};
207+
208+
const doubles = mapAsyncIterator(iterator, x => x + x);
123209

124210
expect(await doubles.next()).to.deep.equal({ value: 2, done: false });
125211
expect(await doubles.next()).to.deep.equal({ value: 4, done: false });
@@ -132,22 +218,15 @@ describe('mapAsyncIterator', () => {
132218
caughtError = e;
133219
}
134220
expect(caughtError).to.equal('ouch');
135-
136-
expect(await doubles.next()).to.deep.equal({
137-
value: undefined,
138-
done: true,
139-
});
140-
expect(await doubles.next()).to.deep.equal({
141-
value: undefined,
142-
done: true,
143-
});
144221
});
145222

146223
it('passes through caught errors through async generators', async () => {
147224
async function* source() {
148225
try {
149226
yield 1;
150227
yield 2;
228+
229+
/* istanbul ignore next (shouldn't be reached) */
151230
yield 3;
152231
} catch (e) {
153232
yield e;
@@ -232,6 +311,8 @@ describe('mapAsyncIterator', () => {
232311
try {
233312
yield 1;
234313
yield 2;
314+
315+
/* istanbul ignore next (shouldn't be reached) */
235316
yield 3;
236317
} finally {
237318
didVisitFinally = true;
@@ -250,10 +331,8 @@ describe('mapAsyncIterator', () => {
250331
expectedError = error;
251332
}
252333

253-
expect(expectedError).to.be.an('error');
254-
if (expectedError) {
255-
expect(expectedError.message).to.equal('Cannot count to 2');
256-
}
334+
invariant(expectedError instanceof Error);
335+
expect(expectedError.message).to.equal('Cannot count to 2');
257336

258337
expect(await throwOver1.next()).to.deep.equal({
259338
value: undefined,
@@ -297,10 +376,8 @@ describe('mapAsyncIterator', () => {
297376
expectedError = error;
298377
}
299378

300-
expect(expectedError).to.be.an('error');
301-
if (expectedError) {
302-
expect(expectedError.message).to.equal('Cannot count to 2');
303-
}
379+
invariant(expectedError instanceof Error);
380+
expect(expectedError.message).to.equal('Cannot count to 2');
304381

305382
expect(await throwOver1.next()).to.deep.equal({
306383
value: undefined,

0 commit comments

Comments
 (0)