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