@@ -23,11 +23,19 @@ describe("alchemy-file-upload", () => {
23
23
24
24
const testFile = new File ( [ "a" . repeat ( 1100 ) ] , "foo.txt" )
25
25
26
- const mockXMLHttpRequest = ( status = 200 , response = { } ) => {
26
+ const mockXMLHttpRequest = (
27
+ status = 200 ,
28
+ response = { } ,
29
+ reason = "Created"
30
+ ) => {
31
+ const body =
32
+ typeof response === "string" ? response : JSON . stringify ( response )
33
+
27
34
mock . setup ( )
28
35
mock . post ( "/admin/pictures" , {
29
36
status,
30
- body : JSON . stringify ( response )
37
+ reason,
38
+ body
31
39
} )
32
40
33
41
let request = new XMLHttpRequest ( )
@@ -203,26 +211,54 @@ describe("alchemy-file-upload", () => {
203
211
} )
204
212
205
213
describe ( "failed server response" , ( ) => {
206
- beforeEach ( ( ) => {
207
- const xhrMock = mockXMLHttpRequest ( 400 , {
208
- message : "Error: Foo Bar"
214
+ describe ( "with a JSON response" , ( ) => {
215
+ beforeEach ( ( ) => {
216
+ const xhrMock = mockXMLHttpRequest ( 400 , {
217
+ message : "Error: Foo Bar"
218
+ } )
219
+ renderComponent ( testFile , xhrMock )
220
+ component . request . open ( "post" , "/admin/pictures" )
221
+ component . request . send ( )
209
222
} )
210
- renderComponent ( testFile , xhrMock )
211
- component . request . open ( "post" , "/admin/pictures" )
212
- component . request . send ( )
213
- } )
214
223
215
- it ( "should call the growl method" , ( ) => {
216
- expect ( growl ) . toHaveBeenCalledWith ( "Error: Foo Bar" , "error" )
217
- expect ( growl ) . toHaveBeenCalledTimes ( 1 )
218
- } )
224
+ it ( "should call the growl method" , ( ) => {
225
+ expect ( growl ) . toHaveBeenCalledWith ( "Error: Foo Bar" , "error" )
226
+ expect ( growl ) . toHaveBeenCalledTimes ( 1 )
227
+ } )
219
228
220
- it ( "should mark as failed" , ( ) => {
221
- expect ( component . className ) . toEqual ( "failed" )
229
+ it ( "should mark as failed" , ( ) => {
230
+ expect ( component . className ) . toEqual ( "failed" )
231
+ } )
232
+
233
+ it ( "should have an error message" , ( ) => {
234
+ expect ( component . errorMessage ) . toEqual ( "Error: Foo Bar" )
235
+ } )
222
236
} )
223
237
224
- it ( "should have an error message" , ( ) => {
225
- expect ( component . errorMessage ) . toEqual ( "Error: Foo Bar" )
238
+ describe ( "without a JSON response" , ( ) => {
239
+ beforeEach ( ( ) => {
240
+ const xhrMock = mockXMLHttpRequest (
241
+ 502 ,
242
+ "<h1>Error</h1><p>Foo Bar</p>" ,
243
+ "Bad Gateway"
244
+ )
245
+ renderComponent ( testFile , xhrMock )
246
+ component . request . open ( "post" , "/admin/pictures" )
247
+ component . request . send ( )
248
+ } )
249
+
250
+ it ( "should call the growl method" , ( ) => {
251
+ expect ( growl ) . toHaveBeenCalledWith ( "502: Bad Gateway" , "error" )
252
+ expect ( growl ) . toHaveBeenCalledTimes ( 1 )
253
+ } )
254
+
255
+ it ( "should mark as failed" , ( ) => {
256
+ expect ( component . className ) . toEqual ( "failed" )
257
+ } )
258
+
259
+ it ( "should have an error message" , ( ) => {
260
+ expect ( component . errorMessage ) . toEqual ( "502: Bad Gateway" )
261
+ } )
226
262
} )
227
263
} )
228
264
} )
0 commit comments