Skip to content

Commit

Permalink
feat(ajax-spec): support upload progress mock
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Dec 19, 2016
1 parent dc06e01 commit 88f56fb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
26 changes: 24 additions & 2 deletions spec/helpers/ajax-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export class MockXMLHttpRequest {
onerror: (e: ErrorEvent) => any;
onprogress: (e: ProgressEvent) => any;
ontimeout: (e: ProgressEvent) => any;
upload: XMLHttpRequestUpload;
upload: XMLHttpRequestUpload = Object.create(null);

constructor() {
this.previousRequest = MockXMLHttpRequest.recentRequest;
Expand All @@ -158,6 +158,14 @@ export class MockXMLHttpRequest {
this.password = password;
this.readyState = 1;
this.triggerEvent('readyStateChange');
// https://developer.mozilla.org/en/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest
// You need to add the event listeners before calling open() on the request. Otherwise the progress events will not fire.
const originalProgressHandler = this.upload.onprogress;
Object.defineProperty(this.upload, 'progress', {
get() {
return originalProgressHandler;
}
});
}

setRequestHeader(key: any, value: any): void {
Expand Down Expand Up @@ -194,7 +202,12 @@ export class MockXMLHttpRequest {
throw new Error('unhandled type "' + this.responseType + '"');
}

respondWith(response: any): void {
respondWith(response: any, progressTimes?: number): void {
if (progressTimes) {
for (let i = 1; i <= progressTimes; ++ i) {
this.triggerUploadEvent('progress', { type: 'ProgressEvent', total: progressTimes, loaded: i });
}
}
this.readyState = 4;
this.responseHeaders = {
'Content-Type': response.contentType || 'text/plain'
Expand Down Expand Up @@ -232,6 +245,15 @@ export class MockXMLHttpRequest {
}
});
}

triggerUploadEvent(name: any, eventObj?: any): void {
// TODO: create a better default event
const e: any = eventObj || {};

if (this.upload['on' + name]) {
this.upload['on' + name](e);
}
}
}

export class MockXMLHttpRequestInternetExplorer extends MockXMLHttpRequest {
Expand Down
24 changes: 1 addition & 23 deletions spec/observables/dom/ajax-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -734,16 +734,6 @@ describe('Observable.ajax', () => {
configurable: true
});

Object.defineProperty(root.XMLHttpRequest.prototype, 'upload', {
get() {
return true;
},
configurable: true
});

// mock for onprogress
root.XDomainRequest = true;

Rx.Observable.ajax({
url: '/flibbertyJibbet',
progressSubscriber: (<any>{
Expand All @@ -763,12 +753,11 @@ describe('Observable.ajax', () => {
const request = MockXMLHttpRequest.mostRecent;

expect(() => {
request.onprogress((<any>'onprogress'));
request.upload.onprogress((<any>'onprogress'));
}).not.throw();

delete root.XMLHttpRequest.prototype.onprogress;
delete root.XMLHttpRequest.prototype.upload;
delete root.XDomainRequest;
});

it('should work fine when XMLHttpRequest onerror property is monkey patched', function() {
Expand All @@ -788,16 +777,6 @@ describe('Observable.ajax', () => {
configurable: true
});

Object.defineProperty(root.XMLHttpRequest.prototype, 'upload', {
get() {
return true;
},
configurable: true
});

// mock for onprogress
root.XDomainRequest = true;

Rx.Observable.ajax({
url: '/flibbertyJibbet'
})
Expand All @@ -813,6 +792,5 @@ describe('Observable.ajax', () => {

delete root.XMLHttpRequest.prototype.onerror;
delete root.XMLHttpRequest.prototype.upload;
delete root.XDomainRequest;
});
});

0 comments on commit 88f56fb

Please sign in to comment.