@@ -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 . flatMap ( ( 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,23 +39,48 @@ 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 ) ;
71
+ const chunk5Data = {
72
+ incremental : [
73
+ {
74
+ data : true ,
75
+ path : [ 'viewer' , 'user' , 'items' , 'edges' , 2 , 'node' , 'isFavorite' ] ,
76
+ } ,
77
+ {
78
+ data : false ,
79
+ path : [ 'viewer' , 'user' , 'items' , 'edges' , 3 , 'node' , 'isFavorite' ] ,
80
+ } ,
81
+ ] ,
82
+ } ;
83
+ const chunk5 = getMultiPartResponse ( chunk5Data , boundary ) ;
52
84
it ( 'should work on each chunk' , function ( ) {
53
85
const onResponse = jest . fn ( ) ;
54
86
const resolver = new PatchResolver ( {
@@ -61,19 +93,23 @@ describe('PathResolver', function () {
61
93
expect ( onResponse ) . not . toHaveBeenCalled ( ) ;
62
94
63
95
resolver . handleChunk ( chunk1 ) ;
64
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk1Data ] ) ;
96
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk1Data ] ) ;
65
97
66
98
onResponse . mockClear ( ) ;
67
99
resolver . handleChunk ( chunk2 ) ;
68
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk2Data ] ) ;
100
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk2Data ] ) ;
69
101
70
102
onResponse . mockClear ( ) ;
71
103
resolver . handleChunk ( chunk3 ) ;
72
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk3Data ] ) ;
104
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk3Data ] ) ;
73
105
74
106
onResponse . mockClear ( ) ;
75
107
resolver . handleChunk ( chunk4 ) ;
76
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk4Data ] ) ;
108
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk4Data ] ) ;
109
+
110
+ onResponse . mockClear ( ) ;
111
+ resolver . handleChunk ( chunk5 ) ;
112
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk5Data ] ) ;
77
113
} ) ;
78
114
79
115
it ( 'should work when chunks are split' , function ( ) {
@@ -96,7 +132,7 @@ describe('PathResolver', function () {
96
132
resolver . handleChunk ( chunk1b ) ;
97
133
expect ( onResponse ) . not . toHaveBeenCalled ( ) ;
98
134
resolver . handleChunk ( chunk1c ) ;
99
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk1Data ] ) ;
135
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk1Data ] ) ;
100
136
onResponse . mockClear ( ) ;
101
137
102
138
const chunk2a = chunk2 . substr ( 0 , 35 ) ;
@@ -105,7 +141,7 @@ describe('PathResolver', function () {
105
141
resolver . handleChunk ( chunk2a ) ;
106
142
expect ( onResponse ) . not . toHaveBeenCalled ( ) ;
107
143
resolver . handleChunk ( chunk2b ) ;
108
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk2Data ] ) ;
144
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk2Data ] ) ;
109
145
onResponse . mockClear ( ) ;
110
146
111
147
const chunk3a = chunk3 . substr ( 0 , 10 ) ;
@@ -117,7 +153,7 @@ describe('PathResolver', function () {
117
153
resolver . handleChunk ( chunk3b ) ;
118
154
expect ( onResponse ) . not . toHaveBeenCalled ( ) ;
119
155
resolver . handleChunk ( chunk3c ) ;
120
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk3Data ] ) ;
156
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk3Data ] ) ;
121
157
} ) ;
122
158
123
159
it ( 'should work when chunks are combined' , function ( ) {
@@ -132,7 +168,7 @@ describe('PathResolver', function () {
132
168
expect ( onResponse ) . not . toHaveBeenCalled ( ) ;
133
169
134
170
resolver . handleChunk ( chunk1 + chunk2 ) ;
135
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk1Data , chunk2Data ] ) ;
171
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk1Data , chunk2Data ] ) ;
136
172
} ) ;
137
173
138
174
it ( 'should work when chunks are combined and split' , function ( ) {
@@ -159,13 +195,13 @@ describe('PathResolver', function () {
159
195
expect ( onResponse ) . not . toHaveBeenCalled ( ) ;
160
196
161
197
resolver . handleChunk ( chunk1 + chunk2 + chunk3a ) ;
162
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk1Data , chunk2Data ] ) ;
198
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk1Data , chunk2Data ] ) ;
163
199
onResponse . mockClear ( ) ;
164
200
165
201
resolver . handleChunk ( chunk3b ) ;
166
202
expect ( onResponse ) . not . toHaveBeenCalled ( ) ;
167
203
resolver . handleChunk ( chunk3c ) ;
168
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk3Data ] ) ;
204
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk3Data ] ) ;
169
205
} ) ;
170
206
171
207
it ( 'should work when chunks are combined across boundaries' , function ( ) {
@@ -183,10 +219,10 @@ describe('PathResolver', function () {
183
219
const chunk2b = chunk2 . substring ( 35 ) ;
184
220
185
221
resolver . handleChunk ( chunk1 + chunk2a ) ;
186
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk1Data ] ) ;
222
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk1Data ] ) ;
187
223
onResponse . mockClear ( ) ;
188
224
resolver . handleChunk ( chunk2b ) ;
189
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk2Data ] ) ;
225
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk2Data ] ) ;
190
226
} ) ;
191
227
it ( 'should work when final chunk ends with terminating boundary' , function ( ) {
192
228
const onResponse = jest . fn ( ) ;
@@ -200,20 +236,20 @@ describe('PathResolver', function () {
200
236
expect ( onResponse ) . not . toHaveBeenCalled ( ) ;
201
237
202
238
resolver . handleChunk ( chunk1 ) ;
203
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk1Data ] ) ;
239
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk1Data ] ) ;
204
240
205
241
onResponse . mockClear ( ) ;
206
242
resolver . handleChunk ( chunk2 ) ;
207
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk2Data ] ) ;
243
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk2Data ] ) ;
208
244
209
245
onResponse . mockClear ( ) ;
210
246
resolver . handleChunk ( chunk3 ) ;
211
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk3Data ] ) ;
247
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk3Data ] ) ;
212
248
213
249
onResponse . mockClear ( ) ;
214
250
const chunk4FinalBoundary = getMultiPartResponse ( chunk4Data , `${ boundary } --` ) ;
215
251
resolver . handleChunk ( chunk4FinalBoundary ) ;
216
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk4Data ] ) ;
252
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk4Data ] ) ;
217
253
} ) ;
218
254
219
255
it ( 'should work with preamble' , function ( ) {
@@ -229,20 +265,20 @@ describe('PathResolver', function () {
229
265
expect ( onResponse ) . not . toHaveBeenCalled ( ) ;
230
266
231
267
resolver . handleChunk ( chunk1 ) ;
232
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk1Data ] ) ;
268
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk1Data ] ) ;
233
269
234
270
onResponse . mockClear ( ) ;
235
271
resolver . handleChunk ( chunk2 ) ;
236
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk2Data ] ) ;
272
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk2Data ] ) ;
237
273
238
274
onResponse . mockClear ( ) ;
239
275
resolver . handleChunk ( chunk3 ) ;
240
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk3Data ] ) ;
276
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk3Data ] ) ;
241
277
242
278
onResponse . mockClear ( ) ;
243
279
const chunk4FinalBoundary = getMultiPartResponse ( chunk4Data , `${ boundary } --` ) ;
244
280
resolver . handleChunk ( chunk4FinalBoundary ) ;
245
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk4Data ] ) ;
281
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk4Data ] ) ;
246
282
} ) ;
247
283
it ( 'should work with epilogue' , function ( ) {
248
284
const onResponse = jest . fn ( ) ;
@@ -256,20 +292,20 @@ describe('PathResolver', function () {
256
292
expect ( onResponse ) . not . toHaveBeenCalled ( ) ;
257
293
258
294
resolver . handleChunk ( chunk1 ) ;
259
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk1Data ] ) ;
295
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk1Data ] ) ;
260
296
261
297
onResponse . mockClear ( ) ;
262
298
resolver . handleChunk ( chunk2 ) ;
263
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk2Data ] ) ;
299
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk2Data ] ) ;
264
300
265
301
onResponse . mockClear ( ) ;
266
302
resolver . handleChunk ( chunk3 ) ;
267
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk3Data ] ) ;
303
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk3Data ] ) ;
268
304
269
305
onResponse . mockClear ( ) ;
270
306
resolver . handleChunk ( chunk4 ) ;
271
307
resolver . handleChunk ( `This is some epilogue data that should be ignored\r\n` ) ;
272
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk4Data ] ) ;
308
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk4Data ] ) ;
273
309
} ) ;
274
310
it ( 'should work with epilogue after chunk with terminating boundary' , function ( ) {
275
311
const onResponse = jest . fn ( ) ;
@@ -283,21 +319,21 @@ describe('PathResolver', function () {
283
319
expect ( onResponse ) . not . toHaveBeenCalled ( ) ;
284
320
285
321
resolver . handleChunk ( chunk1 ) ;
286
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk1Data ] ) ;
322
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk1Data ] ) ;
287
323
288
324
onResponse . mockClear ( ) ;
289
325
resolver . handleChunk ( chunk2 ) ;
290
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk2Data ] ) ;
326
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk2Data ] ) ;
291
327
292
328
onResponse . mockClear ( ) ;
293
329
resolver . handleChunk ( chunk3 ) ;
294
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk3Data ] ) ;
330
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk3Data ] ) ;
295
331
296
332
onResponse . mockClear ( ) ;
297
333
const chunk4FinalBoundary = getMultiPartResponse ( chunk4Data , `${ boundary } --` ) ;
298
334
resolver . handleChunk ( chunk4FinalBoundary ) ;
299
335
resolver . handleChunk ( `This is some epilogue data that should be ignored\r\n` ) ;
300
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk4Data ] ) ;
336
+ assertChunksRecieved ( onResponse . mock . calls [ 0 ] [ 0 ] , [ chunk4Data ] ) ;
301
337
} ) ;
302
338
} ) ;
303
339
}
0 commit comments