6
6
import { expect } from 'chai' ;
7
7
import { describe , it } from 'mocha' ;
8
8
9
+ import invariant from '../../jsutils/invariant' ;
10
+
9
11
import mapAsyncIterator from '../mapAsyncIterator' ;
10
12
11
13
describe ( 'mapAsyncIterator' , ( ) => {
12
- it ( 'maps over async values ' , async ( ) => {
14
+ it ( 'maps over async generator ' , async ( ) => {
13
15
async function * source ( ) {
14
16
yield 1 ;
15
17
yield 2 ;
@@ -27,6 +29,49 @@ describe('mapAsyncIterator', () => {
27
29
} ) ;
28
30
} ) ;
29
31
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
+
30
75
it ( 'maps over async values with async function' , async ( ) => {
31
76
async function * source ( ) {
32
77
yield 1 ;
@@ -49,10 +94,12 @@ describe('mapAsyncIterator', () => {
49
94
} ) ;
50
95
} ) ;
51
96
52
- it ( 'allows returning early from async values ' , async ( ) => {
97
+ it ( 'allows returning early from mapped async generator ' , async ( ) => {
53
98
async function * source ( ) {
54
99
yield 1 ;
55
100
yield 2 ;
101
+
102
+ /* istanbul ignore next (shouldn't be reached) */
56
103
yield 3 ;
57
104
}
58
105
@@ -78,11 +125,41 @@ describe('mapAsyncIterator', () => {
78
125
} ) ;
79
126
} ) ;
80
127
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
+
81
156
it ( 'passes through early return from async values' , async ( ) => {
82
157
async function * source ( ) {
83
158
try {
84
159
yield 1 ;
85
160
yield 2 ;
161
+
162
+ /* istanbul ignore next (shouldn't be reached) */
86
163
yield 3 ;
87
164
} finally {
88
165
yield 'Done' ;
@@ -112,14 +189,23 @@ describe('mapAsyncIterator', () => {
112
189
} ) ;
113
190
} ) ;
114
191
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 ] ;
121
194
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 ) ;
123
209
124
210
expect ( await doubles . next ( ) ) . to . deep . equal ( { value : 2 , done : false } ) ;
125
211
expect ( await doubles . next ( ) ) . to . deep . equal ( { value : 4 , done : false } ) ;
@@ -132,22 +218,15 @@ describe('mapAsyncIterator', () => {
132
218
caughtError = e ;
133
219
}
134
220
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
- } ) ;
144
221
} ) ;
145
222
146
223
it ( 'passes through caught errors through async generators' , async ( ) => {
147
224
async function * source ( ) {
148
225
try {
149
226
yield 1 ;
150
227
yield 2 ;
228
+
229
+ /* istanbul ignore next (shouldn't be reached) */
151
230
yield 3 ;
152
231
} catch ( e ) {
153
232
yield e ;
@@ -232,6 +311,8 @@ describe('mapAsyncIterator', () => {
232
311
try {
233
312
yield 1 ;
234
313
yield 2 ;
314
+
315
+ /* istanbul ignore next (shouldn't be reached) */
235
316
yield 3 ;
236
317
} finally {
237
318
didVisitFinally = true ;
@@ -250,10 +331,8 @@ describe('mapAsyncIterator', () => {
250
331
expectedError = error ;
251
332
}
252
333
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' ) ;
257
336
258
337
expect ( await throwOver1 . next ( ) ) . to . deep . equal ( {
259
338
value : undefined ,
@@ -297,10 +376,8 @@ describe('mapAsyncIterator', () => {
297
376
expectedError = error ;
298
377
}
299
378
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' ) ;
304
381
305
382
expect ( await throwOver1 . next ( ) ) . to . deep . equal ( {
306
383
value : undefined ,
0 commit comments