@@ -39,9 +39,8 @@ describe('buildRequest', () => {
39
39
Accept : 'application/vnd.live-component+html' ,
40
40
'X-CSRF-TOKEN' : '_the_csrf_token' ,
41
41
} ) ;
42
- const body = fetchOptions . body ;
42
+ const body = < FormData > fetchOptions . body ;
43
43
expect ( body ) . toBeInstanceOf ( FormData ) ;
44
- // @ts -ignore body is already asserted to be FormData
45
44
expect ( body . get ( 'data' ) ) . toEqual ( JSON . stringify ( {
46
45
props : { firstName : 'Ryan' } ,
47
46
updated : { firstName : 'Kevin' } ,
@@ -69,9 +68,8 @@ describe('buildRequest', () => {
69
68
70
69
expect ( url ) . toEqual ( '/_components/_batch' ) ;
71
70
expect ( fetchOptions . method ) . toEqual ( 'POST' ) ;
72
- const body = fetchOptions . body ;
71
+ const body = < FormData > fetchOptions . body ;
73
72
expect ( body ) . toBeInstanceOf ( FormData ) ;
74
- // @ts -ignore body is already asserted to be FormData
75
73
expect ( body . get ( 'data' ) ) . toEqual ( JSON . stringify ( {
76
74
props : { firstName : 'Ryan' } ,
77
75
updated : { firstName : 'Kevin' } ,
@@ -103,9 +101,8 @@ describe('buildRequest', () => {
103
101
// no token
104
102
Accept : 'application/vnd.live-component+html' ,
105
103
} ) ;
106
- const body = fetchOptions . body ;
104
+ const body = < FormData > fetchOptions . body ;
107
105
expect ( body ) . toBeInstanceOf ( FormData ) ;
108
- // @ts -ignore body is already asserted to be FormData
109
106
expect ( body . get ( 'data' ) ) . toEqual ( JSON . stringify ( {
110
107
props : { firstName : 'Ryan' . repeat ( 1000 ) } ,
111
108
updated : { firstName : 'Kevin' . repeat ( 1000 ) } ,
@@ -138,14 +135,81 @@ describe('buildRequest', () => {
138
135
{ }
139
136
) ;
140
137
141
- const body = fetchOptions . body ;
138
+ const body = < FormData > fetchOptions . body ;
142
139
expect ( body ) . toBeInstanceOf ( FormData ) ;
143
- // @ts -ignore body is already asserted to be FormData
144
140
expect ( body . get ( 'data' ) ) . toEqual ( JSON . stringify ( {
145
141
props : { firstName : 'Ryan' } ,
146
142
updated : { firstName : 'Kevin' } ,
147
143
propsFromParent : { count : 5 } ,
148
144
args : { sendNotification : '1' } ,
149
145
} ) ) ;
150
146
} ) ;
147
+
148
+ // Helper method for FileList mocking
149
+ const getFileList = ( length = 1 ) => {
150
+ const blob = new Blob ( [ '' ] , { type : 'text/html' } ) ;
151
+ // @ts -ignore This is a mock and those are needed to mock a File object
152
+ blob [ 'lastModifiedDate' ] = '' ;
153
+ // @ts -ignore This is a mock and those are needed to mock a File object
154
+ blob [ 'name' ] = 'filename' ;
155
+ const file = < File > blob ;
156
+ const fileList : FileList = {
157
+ length : length ,
158
+ item : ( ) => file
159
+ } ;
160
+ for ( let i = 0 ; i < length ; ++ i ) {
161
+ fileList [ i ] = file ;
162
+ }
163
+ return fileList ;
164
+ } ;
165
+
166
+ it ( 'Sends file with request' , ( ) => {
167
+ const builder = new RequestBuilder ( '/_components' , '_the_csrf_token' ) ;
168
+
169
+ const { url, fetchOptions } = builder . buildRequest (
170
+ { firstName : 'Ryan' } ,
171
+ [ ] ,
172
+ { } ,
173
+ { } ,
174
+ { } ,
175
+ { 'file' : getFileList ( ) }
176
+ ) ;
177
+
178
+ expect ( url ) . toEqual ( '/_components' ) ;
179
+ expect ( fetchOptions . method ) . toEqual ( 'POST' ) ;
180
+ expect ( fetchOptions . headers ) . toEqual ( {
181
+ Accept : 'application/vnd.live-component+html' ,
182
+ 'X-CSRF-TOKEN' : '_the_csrf_token' ,
183
+ } ) ;
184
+ const body = < FormData > fetchOptions . body ;
185
+ expect ( body ) . toBeInstanceOf ( FormData ) ;
186
+ expect ( body . get ( 'file' ) ) . toBeInstanceOf ( File ) ;
187
+ expect ( body . getAll ( 'file' ) . length ) . toEqual ( 1 ) ;
188
+ } ) ;
189
+
190
+ it ( 'Sends multiple files with request' , ( ) => {
191
+ const builder = new RequestBuilder ( '/_components' , '_the_csrf_token' ) ;
192
+
193
+ const { url, fetchOptions } = builder . buildRequest (
194
+ { firstName : 'Ryan' } ,
195
+ [ ] ,
196
+ { } ,
197
+ { } ,
198
+ { } ,
199
+ { 'file[]' : getFileList ( 3 ) , 'otherFile' : getFileList ( ) }
200
+ ) ;
201
+
202
+ expect ( url ) . toEqual ( '/_components' ) ;
203
+ expect ( fetchOptions . method ) . toEqual ( 'POST' ) ;
204
+ expect ( fetchOptions . headers ) . toEqual ( {
205
+ Accept : 'application/vnd.live-component+html' ,
206
+ 'X-CSRF-TOKEN' : '_the_csrf_token' ,
207
+ } ) ;
208
+ const body = < FormData > fetchOptions . body ;
209
+ expect ( body ) . toBeInstanceOf ( FormData ) ;
210
+ expect ( body . get ( 'file[]' ) ) . toBeInstanceOf ( File ) ;
211
+ expect ( body . getAll ( 'file[]' ) . length ) . toEqual ( 3 ) ;
212
+ expect ( body . get ( 'otherFile' ) ) . toBeInstanceOf ( File ) ;
213
+ expect ( body . getAll ( 'otherFile' ) . length ) . toEqual ( 1 ) ;
214
+ } ) ;
151
215
} ) ;
0 commit comments