jasmine: Mocking XMLHttpRequest

Viewed 15

I have an upload function that contains some EventListeners , do you know how can i mock it readystatechange and error events?

thanks

  private uploadAttachmentProgressing(uploadInfo: AttachmentUploadInformation, attachment: File): Observable<string> {
    const isCompleted = new Subject<string>();

   
    const xhr = new XMLHttpRequest();
    xhr.upload.addEventListener('progress', (progressEvent: ProgressEvent) => {
      /* upload methods */
    });

    xhr.addEventListener('readystatechange', () => {
 
      if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
        isCompleted.next(uploadInfo.key);
      }
    });

    xhr.addEventListener('error', () => {
      isCompleted.next(null);
    });

    xhr.open('PUT', uploadInfo.url, true);

    xhr.send(attachment);

    return isCompleted;
  }
0 Answers
Related