import { UploadxAjax } from './ajax' ; const data = { key0 : '0' , key1 : '1' } ; const mockXHR = { DONE : 0 , HEADERS_RECEIVED : 0 , LOADING : 0 , OPENED : 0 , UNSENT : 0 , getResponseHeader ( name ) { return null ; } , onabort ( ) { } , onerror ( ) { } , onload ( ) { } , onloadend ( ) { } , onprogress ( ) { } , onreadystatechange ( ) { } , ontimeout ( ) { } , readyState : 0 , responseText : JSON . stringify ( data ) , responseType : '' , responseURL : '' , setRequestHeader ( name , value ) { } , status : 200 , statusText : 'OK' , timeout : 0 , withCredentials : false , upload : { onprogress : ( evt ) => { } } , abort ( ) { } , open ( ) { } , send : ( ) => { setTimeout ( ( ) => { mockXHR . onload && mockXHR . onload ( { } as ProgressEvent ) ; } ) ; } , getAllResponseHeaders : ( ) => `key0: 0\r\nkey1: 1\r\n` , response : JSON . stringify ( data ) } as unknown as XMLHttpRequest ; const mockedXHR = { ... mockXHR } ; describe ( 'Ajax' , ( ) => { let ajax ; beforeEach ( ( ) => { ajax = new UploadxAjax ( ( ) => mockXHR ) ; } ) ; it ( 'getResponseHeaders' , ( ) => { const headers = ajax . getResponseHeaders ( mockedXHR ) ; expect ( headers ) . toEqual ( data ) ; } ) ; it ( 'getResponseBody' , ( ) => { const body = ajax . getResponseBody < string > ( mockedXHR ) ; expect ( body ) . toEqual ( JSON . stringify ( data ) ) ; } ) ; it ( 'request' , async ( ) => { const res = await ajax . request ( { url : '/upload' , responseType : 'json' } ) ; expect ( res ) . toEqual ( jasmine . objectContaining ( { status : 200 , headers : data , data } ) ) ; } ) ; } ) ;