@@ -44,7 +44,7 @@ const chunk3 = [
44
44
'' ,
45
45
'{"path":["viewer","user","profile"],"data":{"displayName":"Steven Seagal"}}\n' ,
46
46
'' ,
47
- '-----' ,
47
+ '-----\r\n ' ,
48
48
] . join ( '\r\n' ) ;
49
49
50
50
describe ( 'PathResolver' , function ( ) {
@@ -86,19 +86,24 @@ describe('PathResolver', function() {
86
86
onResponse,
87
87
} ) ;
88
88
89
+ console . log ( 'chunk1.length' , chunk1 . length ) ;
90
+
89
91
const chunk1a = chunk1 . substr ( 0 , 35 ) ;
90
- const chunk1b = chunk1 . substr ( 36 ) ;
92
+ const chunk1b = chunk1 . substr ( 35 , 80 ) ;
93
+ const chunk1c = chunk1 . substr ( 35 + 80 ) ;
91
94
92
95
resolver . handleChunk ( chunk1a ) ;
93
96
expect ( onResponse ) . not . toHaveBeenCalled ( ) ;
94
97
resolver . handleChunk ( chunk1b ) ;
98
+ expect ( onResponse ) . not . toHaveBeenCalled ( ) ;
99
+ resolver . handleChunk ( chunk1c ) ;
95
100
expect ( onResponse ) . toHaveBeenCalledWith ( {
96
101
data : { viewer : { currencies : null , user : { profile : null } } } ,
97
102
} ) ;
98
103
onResponse . mockClear ( ) ;
99
104
100
105
const chunk2a = chunk2 . substr ( 0 , 35 ) ;
101
- const chunk2b = chunk2 . substr ( 36 ) ;
106
+ const chunk2b = chunk2 . substr ( 35 ) ;
102
107
103
108
resolver . handleChunk ( chunk2a ) ;
104
109
expect ( onResponse ) . not . toHaveBeenCalled ( ) ;
@@ -115,7 +120,7 @@ describe('PathResolver', function() {
115
120
116
121
const chunk3a = chunk3 . substr ( 0 , 10 ) ;
117
122
const chunk3b = chunk3 . substr ( 11 , 20 ) ;
118
- const chunk3c = chunk3 . substr ( 21 ) ;
123
+ const chunk3c = chunk3 . substr ( 11 + 20 ) ;
119
124
120
125
resolver . handleChunk ( chunk3a ) ;
121
126
expect ( onResponse ) . not . toHaveBeenCalled ( ) ;
@@ -132,6 +137,89 @@ describe('PathResolver', function() {
132
137
} ) ;
133
138
} ) ;
134
139
140
+ it ( 'should work when chunks are combined' , function ( ) {
141
+ const onResponse = jest . fn ( ) ;
142
+ const resolver = new PatchResolver ( {
143
+ onResponse,
144
+ } ) ;
145
+
146
+ resolver . handleChunk ( chunk1 + chunk2 ) ;
147
+ expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( {
148
+ data : { viewer : { currencies : null , user : { profile : null } } } ,
149
+ } ) ;
150
+ expect ( onResponse . mock . calls [ 1 ] [ 0 ] ) . toEqual ( {
151
+ data : {
152
+ viewer : {
153
+ currencies : [ 'USD' , 'GBP' , 'EUR' , 'CAD' , 'AUD' , 'CHF' , 'MXN' ] ,
154
+ user : { profile : null } ,
155
+ } ,
156
+ } ,
157
+ } ) ;
158
+ } ) ;
159
+
160
+ it ( 'should work when chunks are combined and split' , function ( ) {
161
+ const onResponse = jest . fn ( ) ;
162
+ const resolver = new PatchResolver ( {
163
+ onResponse,
164
+ } ) ;
165
+
166
+ const chunk3a = chunk3 . substr ( 0 , 10 ) ;
167
+ const chunk3b = chunk3 . substr ( 11 , 20 ) ;
168
+ const chunk3c = chunk3 . substr ( 11 + 20 ) ;
169
+
170
+ resolver . handleChunk ( chunk1 + chunk2 + chunk3a ) ;
171
+ expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( {
172
+ data : { viewer : { currencies : null , user : { profile : null } } } ,
173
+ } ) ;
174
+ expect ( onResponse . mock . calls [ 1 ] [ 0 ] ) . toEqual ( {
175
+ data : {
176
+ viewer : {
177
+ currencies : [ 'USD' , 'GBP' , 'EUR' , 'CAD' , 'AUD' , 'CHF' , 'MXN' ] ,
178
+ user : { profile : null } ,
179
+ } ,
180
+ } ,
181
+ } ) ;
182
+ onResponse . mockClear ( ) ;
183
+
184
+ resolver . handleChunk ( chunk3b ) ;
185
+ expect ( onResponse ) . not . toHaveBeenCalled ( ) ;
186
+ resolver . handleChunk ( chunk3c ) ;
187
+ expect ( onResponse ) . toHaveBeenCalledWith ( {
188
+ data : {
189
+ viewer : {
190
+ currencies : [ 'USD' , 'GBP' , 'EUR' , 'CAD' , 'AUD' , 'CHF' , 'MXN' ] ,
191
+ user : { profile : { displayName : 'Steven Seagal' } } ,
192
+ } ,
193
+ } ,
194
+ } ) ;
195
+ } ) ;
196
+
197
+ it ( 'should work when chunks are combined across boundaries' , function ( ) {
198
+ const onResponse = jest . fn ( ) ;
199
+ const resolver = new PatchResolver ( {
200
+ onResponse,
201
+ } ) ;
202
+
203
+ const chunk2a = chunk2 . substring ( 0 , 35 ) ;
204
+ const chunk2b = chunk2 . substring ( 35 ) ;
205
+
206
+ resolver . handleChunk ( chunk1 + chunk2a ) ;
207
+ expect ( onResponse ) . toHaveBeenCalledWith ( {
208
+ data : { viewer : { currencies : null , user : { profile : null } } } ,
209
+ } ) ;
210
+ onResponse . mockClear ( ) ;
211
+ resolver . handleChunk ( chunk2b ) ;
212
+
213
+ expect ( onResponse ) . toHaveBeenCalledWith ( {
214
+ data : {
215
+ viewer : {
216
+ currencies : [ 'USD' , 'GBP' , 'EUR' , 'CAD' , 'AUD' , 'CHF' , 'MXN' ] ,
217
+ user : { profile : null } ,
218
+ } ,
219
+ } ,
220
+ } ) ;
221
+ } ) ;
222
+
135
223
it ( 'should merge errors' , function ( ) {
136
224
const onResponse = jest . fn ( ) ;
137
225
const resolver = new PatchResolver ( {
@@ -166,4 +254,5 @@ describe('PathResolver', function() {
166
254
errors : [ { message : 'Very Bad Error' } , { message : 'Not So Bad Error' } ] ,
167
255
} ) ;
168
256
} ) ;
257
+ it ( 'should work' , function ( ) { } ) ;
169
258
} ) ;
0 commit comments