@@ -10,6 +10,13 @@ function getMultiPartResponse(data, boundary) {
10
10
return [ 'Content-Type: application/json' , '' , json , `--${ boundary } \r\n` ] . join ( '\r\n' ) ;
11
11
}
12
12
13
+ function assertChunksRecieved ( mockCall , chunks ) {
14
+ const nonIncrementalChunks = chunks . map ( ( chunk ) =>
15
+ chunk . incremental ? chunk . incremental [ 0 ] : chunk
16
+ ) ;
17
+ expect ( mockCall ) . toEqual ( nonIncrementalChunks ) ;
18
+ }
19
+
13
20
describe ( 'PathResolver' , function ( ) {
14
21
for ( const boundary of [ '-' , 'gc0p4Jq0M2Yt08jU534c0p' ] ) {
15
22
describe ( `boundary ${ boundary } ` , ( ) => {
@@ -32,21 +39,33 @@ describe('PathResolver', function () {
32
39
const chunk1 = getMultiPartResponse ( chunk1Data , boundary ) ;
33
40
34
41
const chunk2Data = {
35
- path : [ 'viewer' , 'currencies' ] ,
36
- data : [ 'USD' , 'GBP' , 'EUR' , 'CAD' , 'AUD' , 'CHF' , '😂' ] , // test unicode
37
- errors : [ { message : 'Not So Bad Error' } ] ,
42
+ incremental : [
43
+ {
44
+ path : [ 'viewer' , 'currencies' ] ,
45
+ data : [ 'USD' , 'GBP' , 'EUR' , 'CAD' , 'AUD' , 'CHF' , '😂' ] , // test unicode
46
+ errors : [ { message : 'Not So Bad Error' } ] ,
47
+ } ,
48
+ ] ,
38
49
} ;
39
50
const chunk2 = getMultiPartResponse ( chunk2Data , boundary ) ;
40
51
41
52
const chunk3Data = {
42
- path : [ 'viewer' , 'user' , 'profile' ] ,
43
- data : { displayName : 'Steven Seagal' } ,
53
+ incremental : [
54
+ {
55
+ path : [ 'viewer' , 'user' , 'profile' ] ,
56
+ data : { displayName : 'Steven Seagal' } ,
57
+ } ,
58
+ ] ,
44
59
} ;
45
60
const chunk3 = getMultiPartResponse ( chunk3Data , boundary ) ;
46
61
47
62
const chunk4Data = {
48
- data : false ,
49
- path : [ 'viewer' , 'user' , 'items' , 'edges' , 1 , 'node' , 'isFavorite' ] ,
63
+ incremental : [
64
+ {
65
+ data : false ,
66
+ path : [ 'viewer' , 'user' , 'items' , 'edges' , 1 , 'node' , 'isFavorite' ] ,
67
+ } ,
68
+ ] ,
50
69
} ;
51
70
const chunk4 = getMultiPartResponse ( chunk4Data , boundary ) ;
52
71
it ( 'should work on each chunk' , function ( ) {
@@ -61,19 +80,19 @@ describe('PathResolver', function () {
61
80
expect ( onResponse ) . not . toHaveBeenCalled ( ) ;
62
81
63
82
resolver . handleChunk ( chunk1 ) ;
64
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk1Data ] ) ;
83
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk1Data ] ) ;
65
84
66
85
onResponse . mockClear ( ) ;
67
86
resolver . handleChunk ( chunk2 ) ;
68
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk2Data ] ) ;
87
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk2Data ] ) ;
69
88
70
89
onResponse . mockClear ( ) ;
71
90
resolver . handleChunk ( chunk3 ) ;
72
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk3Data ] ) ;
91
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk3Data ] ) ;
73
92
74
93
onResponse . mockClear ( ) ;
75
94
resolver . handleChunk ( chunk4 ) ;
76
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk4Data ] ) ;
95
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk4Data ] ) ;
77
96
} ) ;
78
97
79
98
it ( 'should work when chunks are split' , function ( ) {
@@ -96,7 +115,7 @@ describe('PathResolver', function () {
96
115
resolver . handleChunk ( chunk1b ) ;
97
116
expect ( onResponse ) . not . toHaveBeenCalled ( ) ;
98
117
resolver . handleChunk ( chunk1c ) ;
99
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk1Data ] ) ;
118
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk1Data ] ) ;
100
119
onResponse . mockClear ( ) ;
101
120
102
121
const chunk2a = chunk2 . substr ( 0 , 35 ) ;
@@ -105,7 +124,7 @@ describe('PathResolver', function () {
105
124
resolver . handleChunk ( chunk2a ) ;
106
125
expect ( onResponse ) . not . toHaveBeenCalled ( ) ;
107
126
resolver . handleChunk ( chunk2b ) ;
108
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk2Data ] ) ;
127
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk2Data ] ) ;
109
128
onResponse . mockClear ( ) ;
110
129
111
130
const chunk3a = chunk3 . substr ( 0 , 10 ) ;
@@ -117,7 +136,7 @@ describe('PathResolver', function () {
117
136
resolver . handleChunk ( chunk3b ) ;
118
137
expect ( onResponse ) . not . toHaveBeenCalled ( ) ;
119
138
resolver . handleChunk ( chunk3c ) ;
120
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk3Data ] ) ;
139
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk3Data ] ) ;
121
140
} ) ;
122
141
123
142
it ( 'should work when chunks are combined' , function ( ) {
@@ -132,7 +151,7 @@ describe('PathResolver', function () {
132
151
expect ( onResponse ) . not . toHaveBeenCalled ( ) ;
133
152
134
153
resolver . handleChunk ( chunk1 + chunk2 ) ;
135
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk1Data , chunk2Data ] ) ;
154
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk1Data , chunk2Data ] ) ;
136
155
} ) ;
137
156
138
157
it ( 'should work when chunks are combined and split' , function ( ) {
@@ -159,13 +178,13 @@ describe('PathResolver', function () {
159
178
expect ( onResponse ) . not . toHaveBeenCalled ( ) ;
160
179
161
180
resolver . handleChunk ( chunk1 + chunk2 + chunk3a ) ;
162
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk1Data , chunk2Data ] ) ;
181
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk1Data , chunk2Data ] ) ;
163
182
onResponse . mockClear ( ) ;
164
183
165
184
resolver . handleChunk ( chunk3b ) ;
166
185
expect ( onResponse ) . not . toHaveBeenCalled ( ) ;
167
186
resolver . handleChunk ( chunk3c ) ;
168
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk3Data ] ) ;
187
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk3Data ] ) ;
169
188
} ) ;
170
189
171
190
it ( 'should work when chunks are combined across boundaries' , function ( ) {
@@ -183,10 +202,10 @@ describe('PathResolver', function () {
183
202
const chunk2b = chunk2 . substring ( 35 ) ;
184
203
185
204
resolver . handleChunk ( chunk1 + chunk2a ) ;
186
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk1Data ] ) ;
205
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk1Data ] ) ;
187
206
onResponse . mockClear ( ) ;
188
207
resolver . handleChunk ( chunk2b ) ;
189
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk2Data ] ) ;
208
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk2Data ] ) ;
190
209
} ) ;
191
210
it ( 'should work when final chunk ends with terminating boundary' , function ( ) {
192
211
const onResponse = jest . fn ( ) ;
@@ -200,20 +219,20 @@ describe('PathResolver', function () {
200
219
expect ( onResponse ) . not . toHaveBeenCalled ( ) ;
201
220
202
221
resolver . handleChunk ( chunk1 ) ;
203
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk1Data ] ) ;
222
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk1Data ] ) ;
204
223
205
224
onResponse . mockClear ( ) ;
206
225
resolver . handleChunk ( chunk2 ) ;
207
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk2Data ] ) ;
226
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk2Data ] ) ;
208
227
209
228
onResponse . mockClear ( ) ;
210
229
resolver . handleChunk ( chunk3 ) ;
211
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk3Data ] ) ;
230
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk3Data ] ) ;
212
231
213
232
onResponse . mockClear ( ) ;
214
233
const chunk4FinalBoundary = getMultiPartResponse ( chunk4Data , `${ boundary } --` ) ;
215
234
resolver . handleChunk ( chunk4FinalBoundary ) ;
216
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk4Data ] ) ;
235
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk4Data ] ) ;
217
236
} ) ;
218
237
219
238
it ( 'should work with preamble' , function ( ) {
@@ -229,20 +248,20 @@ describe('PathResolver', function () {
229
248
expect ( onResponse ) . not . toHaveBeenCalled ( ) ;
230
249
231
250
resolver . handleChunk ( chunk1 ) ;
232
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk1Data ] ) ;
251
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk1Data ] ) ;
233
252
234
253
onResponse . mockClear ( ) ;
235
254
resolver . handleChunk ( chunk2 ) ;
236
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk2Data ] ) ;
255
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk2Data ] ) ;
237
256
238
257
onResponse . mockClear ( ) ;
239
258
resolver . handleChunk ( chunk3 ) ;
240
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk3Data ] ) ;
259
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk3Data ] ) ;
241
260
242
261
onResponse . mockClear ( ) ;
243
262
const chunk4FinalBoundary = getMultiPartResponse ( chunk4Data , `${ boundary } --` ) ;
244
263
resolver . handleChunk ( chunk4FinalBoundary ) ;
245
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk4Data ] ) ;
264
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk4Data ] ) ;
246
265
} ) ;
247
266
it ( 'should work with epilogue' , function ( ) {
248
267
const onResponse = jest . fn ( ) ;
@@ -256,20 +275,20 @@ describe('PathResolver', function () {
256
275
expect ( onResponse ) . not . toHaveBeenCalled ( ) ;
257
276
258
277
resolver . handleChunk ( chunk1 ) ;
259
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk1Data ] ) ;
278
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk1Data ] ) ;
260
279
261
280
onResponse . mockClear ( ) ;
262
281
resolver . handleChunk ( chunk2 ) ;
263
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk2Data ] ) ;
282
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk2Data ] ) ;
264
283
265
284
onResponse . mockClear ( ) ;
266
285
resolver . handleChunk ( chunk3 ) ;
267
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk3Data ] ) ;
286
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk3Data ] ) ;
268
287
269
288
onResponse . mockClear ( ) ;
270
289
resolver . handleChunk ( chunk4 ) ;
271
290
resolver . handleChunk ( `This is some epilogue data that should be ignored\r\n` ) ;
272
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk4Data ] ) ;
291
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk4Data ] ) ;
273
292
} ) ;
274
293
it ( 'should work with epilogue after chunk with terminating boundary' , function ( ) {
275
294
const onResponse = jest . fn ( ) ;
@@ -283,21 +302,21 @@ describe('PathResolver', function () {
283
302
expect ( onResponse ) . not . toHaveBeenCalled ( ) ;
284
303
285
304
resolver . handleChunk ( chunk1 ) ;
286
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk1Data ] ) ;
305
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk1Data ] ) ;
287
306
288
307
onResponse . mockClear ( ) ;
289
308
resolver . handleChunk ( chunk2 ) ;
290
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk2Data ] ) ;
309
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk2Data ] ) ;
291
310
292
311
onResponse . mockClear ( ) ;
293
312
resolver . handleChunk ( chunk3 ) ;
294
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk3Data ] ) ;
313
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk3Data ] ) ;
295
314
296
315
onResponse . mockClear ( ) ;
297
316
const chunk4FinalBoundary = getMultiPartResponse ( chunk4Data , `${ boundary } --` ) ;
298
317
resolver . handleChunk ( chunk4FinalBoundary ) ;
299
318
resolver . handleChunk ( `This is some epilogue data that should be ignored\r\n` ) ;
300
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk4Data ] ) ;
319
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk4Data ] ) ;
301
320
} ) ;
302
321
} ) ;
303
322
}
0 commit comments