@@ -4,61 +4,49 @@ import { TextEncoder, TextDecoder } from 'util';
4
4
global . TextEncoder = TextEncoder ;
5
5
global . TextDecoder = TextDecoder ;
6
6
7
- const chunk1 = [
8
- '' ,
9
- '---' ,
10
- 'Content-Type: application/json' ,
11
- 'Content-Length: 142' ,
12
- '' ,
13
- '{"data":{"viewer":{"currencies":null,"user":{"profile":null,"items":{"edges":[{"node":{"isFavorite":null}},{"node":{"isFavorite":null}}]}}}}}\n' ,
14
- ] . join ( '\r\n' ) ;
15
-
16
- const chunk1error = [
17
- '' ,
18
- '---' ,
19
- 'Content-Type: application/json' ,
20
- 'Content-Length: 104' ,
21
- '' ,
22
- '{"data":{"viewer":{"currencies":null,"user":{"profile":null}}},"errors":[{"message":"Very Bad Error"}]}\n' ,
23
- ] . join ( '\r\n' ) ;
24
-
25
- const chunk2 = [
26
- '' ,
27
- '---' ,
28
- 'Content-Type: application/json' ,
29
- 'Content-Length: 85' ,
30
- '' ,
31
- '{"path":["viewer","currencies"],"data":["USD","GBP","EUR","CAD","AUD","CHF","😂"]}\n' , // test unicode
32
- ] . join ( '\r\n' ) ;
33
-
34
- const chunk2error = [
35
- '' ,
36
- '---' ,
37
- 'Content-Type: application/json' ,
38
- 'Content-Length: 127' ,
39
- '' ,
40
- '{"path":["viewer","currencies"],"data":["USD","GBP","EUR","CAD","AUD","CHF","😂"],"errors":[{"message":"Not So Bad Error"}]}\n' ,
41
- ] . join ( '\r\n' ) ;
42
-
43
- const chunk3 = [
44
- '' ,
45
- '---' ,
46
- 'Content-Type: application/json' ,
47
- 'Content-Length: 76' ,
48
- '' ,
49
- '{"path":["viewer","user","profile"],"data":{"displayName":"Steven Seagal"}}\n' ,
50
- ] . join ( '\r\n' ) ;
51
-
52
- const chunk4 = [
53
- '' ,
54
- '---' ,
55
- 'Content-Type: application/json' ,
56
- 'Content-Length: 78' ,
57
- '' ,
58
- '{"data":false,"path":["viewer","user","items","edges",1,"node","isFavorite"]}\n' ,
59
- '' ,
60
- '-----\r\n' ,
61
- ] . join ( '\r\n' ) ;
7
+ function getMultiPartResponse ( data ) {
8
+ const json = JSON . stringify ( data ) ;
9
+ const chunk = Buffer . from ( json , 'utf8' ) ;
10
+
11
+ return [
12
+ '' ,
13
+ '---' ,
14
+ 'Content-Type: application/json' ,
15
+ `Content-Length: ${ String ( chunk . length ) } ` ,
16
+ '' ,
17
+ json ,
18
+ '' ,
19
+ ] . join ( '\r\n' ) ;
20
+ }
21
+
22
+ const chunk1Data = {
23
+ data : {
24
+ viewer : {
25
+ currencies : null ,
26
+ user : {
27
+ profile : null ,
28
+ items : { edges : [ { node : { isFavorite : null } } , { node : { isFavorite : null } } ] } ,
29
+ } ,
30
+ } ,
31
+ } ,
32
+ } ;
33
+ const chunk1 = getMultiPartResponse ( chunk1Data ) ;
34
+
35
+ const chunk2Data = {
36
+ path : [ 'viewer' , 'currencies' ] ,
37
+ data : [ 'USD' , 'GBP' , 'EUR' , 'CAD' , 'AUD' , 'CHF' , '😂' ] , // test unicode
38
+ errors : [ { message : 'Not So Bad Error' } ] ,
39
+ } ;
40
+ const chunk2 = getMultiPartResponse ( chunk2Data ) ;
41
+
42
+ const chunk3Data = { path : [ 'viewer' , 'user' , 'profile' ] , data : { displayName : 'Steven Seagal' } } ;
43
+ const chunk3 = getMultiPartResponse ( chunk3Data ) ;
44
+
45
+ const chunk4Data = {
46
+ data : false ,
47
+ path : [ 'viewer' , 'user' , 'items' , 'edges' , 1 , 'node' , 'isFavorite' ] ,
48
+ } ;
49
+ const chunk4 = getMultiPartResponse ( chunk4Data ) ;
62
50
63
51
describe ( 'PathResolver' , function ( ) {
64
52
it ( 'should work on each chunk' , function ( ) {
@@ -68,19 +56,19 @@ describe('PathResolver', function() {
68
56
} ) ;
69
57
70
58
resolver . handleChunk ( chunk1 ) ;
71
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toMatchSnapshot ( ) ;
59
+ expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk1Data ] ) ;
72
60
73
61
onResponse . mockClear ( ) ;
74
62
resolver . handleChunk ( chunk2 ) ;
75
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toMatchSnapshot ( ) ;
63
+ expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk2Data ] ) ;
76
64
77
65
onResponse . mockClear ( ) ;
78
66
resolver . handleChunk ( chunk3 ) ;
79
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toMatchSnapshot ( ) ;
67
+ expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk3Data ] ) ;
80
68
81
69
onResponse . mockClear ( ) ;
82
70
resolver . handleChunk ( chunk4 ) ;
83
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toMatchSnapshot ( ) ;
71
+ expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk4Data ] ) ;
84
72
} ) ;
85
73
86
74
it ( 'should work when chunks are split' , function ( ) {
@@ -98,7 +86,7 @@ describe('PathResolver', function() {
98
86
resolver . handleChunk ( chunk1b ) ;
99
87
expect ( onResponse ) . not . toHaveBeenCalled ( ) ;
100
88
resolver . handleChunk ( chunk1c ) ;
101
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toMatchSnapshot ( ) ;
89
+ expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk1Data ] ) ;
102
90
onResponse . mockClear ( ) ;
103
91
104
92
const chunk2a = chunk2 . substr ( 0 , 35 ) ;
@@ -107,7 +95,7 @@ describe('PathResolver', function() {
107
95
resolver . handleChunk ( chunk2a ) ;
108
96
expect ( onResponse ) . not . toHaveBeenCalled ( ) ;
109
97
resolver . handleChunk ( chunk2b ) ;
110
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toMatchSnapshot ( ) ;
98
+ expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk2Data ] ) ;
111
99
onResponse . mockClear ( ) ;
112
100
113
101
const chunk3a = chunk3 . substr ( 0 , 10 ) ;
@@ -119,7 +107,7 @@ describe('PathResolver', function() {
119
107
resolver . handleChunk ( chunk3b ) ;
120
108
expect ( onResponse ) . not . toHaveBeenCalled ( ) ;
121
109
resolver . handleChunk ( chunk3c ) ;
122
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toMatchSnapshot ( ) ;
110
+ expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk3Data ] ) ;
123
111
} ) ;
124
112
125
113
it ( 'should work when chunks are combined' , function ( ) {
@@ -129,7 +117,7 @@ describe('PathResolver', function() {
129
117
} ) ;
130
118
131
119
resolver . handleChunk ( chunk1 + chunk2 ) ;
132
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toMatchSnapshot ( ) ;
120
+ expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk1Data , chunk2Data ] ) ;
133
121
} ) ;
134
122
135
123
it ( 'should work when chunks are combined and split' , function ( ) {
@@ -143,13 +131,13 @@ describe('PathResolver', function() {
143
131
const chunk3c = chunk3 . substr ( 11 + 20 ) ;
144
132
145
133
resolver . handleChunk ( chunk1 + chunk2 + chunk3a ) ;
146
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toMatchSnapshot ( ) ;
134
+ expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk1Data , chunk2Data ] ) ;
147
135
onResponse . mockClear ( ) ;
148
136
149
137
resolver . handleChunk ( chunk3b ) ;
150
138
expect ( onResponse ) . not . toHaveBeenCalled ( ) ;
151
139
resolver . handleChunk ( chunk3c ) ;
152
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toMatchSnapshot ( ) ;
140
+ expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk3Data ] ) ;
153
141
} ) ;
154
142
155
143
it ( 'should work when chunks are combined across boundaries' , function ( ) {
@@ -162,42 +150,9 @@ describe('PathResolver', function() {
162
150
const chunk2b = chunk2 . substring ( 35 ) ;
163
151
164
152
resolver . handleChunk ( chunk1 + chunk2a ) ;
165
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toMatchSnapshot ( ) ;
153
+ expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk1Data ] ) ;
166
154
onResponse . mockClear ( ) ;
167
155
resolver . handleChunk ( chunk2b ) ;
168
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toMatchSnapshot ( ) ;
169
- } ) ;
170
-
171
- it ( 'should merge errors' , function ( ) {
172
- const onResponse = jest . fn ( ) ;
173
- const resolver = new PatchResolver ( {
174
- onResponse,
175
- } ) ;
176
-
177
- resolver . handleChunk ( chunk1error ) ;
178
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toMatchSnapshot ( ) ;
179
- onResponse . mockClear ( ) ;
180
- resolver . handleChunk ( chunk2error ) ;
181
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toMatchSnapshot ( ) ;
182
- onResponse . mockClear ( ) ;
183
- resolver . handleChunk ( chunk3 ) ;
184
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toMatchSnapshot ( ) ;
185
- } ) ;
186
-
187
- it ( 'should work when not applying to previous' , function ( ) {
188
- const onResponse = jest . fn ( ) ;
189
- const resolver = new PatchResolver ( {
190
- onResponse,
191
- applyToPrevious : false ,
192
- } ) ;
193
-
194
- const chunk2a = chunk2 . substring ( 0 , 35 ) ;
195
- const chunk2b = chunk2 . substring ( 35 ) ;
196
-
197
- resolver . handleChunk ( chunk1 + chunk2a ) ;
198
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toMatchSnapshot ( ) ;
199
- onResponse . mockClear ( ) ;
200
- resolver . handleChunk ( chunk2b ) ;
201
- expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toMatchSnapshot ( ) ;
156
+ expect ( onResponse . mock . calls [ 0 ] [ 0 ] ) . toEqual ( [ chunk2Data ] ) ;
202
157
} ) ;
203
158
} ) ;
0 commit comments