Skip to content

Commit

Permalink
mock the behavior of IE
Browse files Browse the repository at this point in the history
unit test for IE
  • Loading branch information
danmarshall committed Aug 26, 2016
1 parent 3669375 commit f66c83b
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 13 deletions.
59 changes: 48 additions & 11 deletions spec/helpers/ajax-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,17 @@ export class MockXMLHttpRequest {

private previousRequest: MockXMLHttpRequest;

private responseType: string = '';
protected responseType: string = '';
private eventHandlers: Array<any> = [];
private readyState: number = 0;

private user: any;
private password: any;

private responseHeaders: any;
private status: any;
private responseText: string;
private response: any;
protected status: any;
protected responseText: string;
protected response: any;

url: any;
method: any;
Expand Down Expand Up @@ -176,6 +176,18 @@ export class MockXMLHttpRequest {
this.triggerEvent('error');
}

protected jsonResponseValue(response: any) {
try {
this.response = JSON.parse(response.responseText);
} catch (err) {
throw new Error('unable to JSON.parse: \n' + response.responseText);
}
}

protected defaultResponseValue() {
throw new Error('unhandled type "' + this.responseType + '"');
}

respondWith(response: any): void {
this.readyState = 4;
this.responseHeaders = {
Expand All @@ -186,17 +198,13 @@ export class MockXMLHttpRequest {
if (!('response' in response)) {
switch (this.responseType) {
case 'json':
try {
this.response = JSON.parse(response.responseText);
} catch (err) {
throw new Error('unable to JSON.parse: \n' + response.responseText);
}
this.jsonResponseValue(response);
break;
case 'text':
this.response = response.responseText;
break;
default:
throw new Error('unhandled type "' + this.responseType + '"');
this.defaultResponseValue();
}
}
// TODO: pass better event to onload.
Expand All @@ -218,4 +226,33 @@ export class MockXMLHttpRequest {
}
});
}
}
}

export class MockXMLHttpRequest_InternetExplorer extends MockXMLHttpRequest {
constructor() {
super();
}

private mockHttp204() {
this.responseType = '';
this.responseText = '';
this.response = '';
}

protected jsonResponseValue(response: any) {
if (this.status == 204) {
this.mockHttp204();
return;
}
return super.jsonResponseValue(response);
}

protected defaultResponseValue() {
if (this.status == 204) {
this.mockHttp204()
return;
}
return super.defaultResponseValue();
}

}
39 changes: 37 additions & 2 deletions spec/observables/dom/ajax-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {expect} from 'chai';
import * as sinon from 'sinon';
import * as Rx from '../../../dist/cjs/Rx';
import {root} from '../../../dist/cjs/util/root';
import {MockXMLHttpRequest} from '../../helpers/ajax-helper';
import {MockXMLHttpRequest, MockXMLHttpRequest_InternetExplorer} from '../../helpers/ajax-helper';

declare const global: any;

Expand Down Expand Up @@ -555,10 +555,45 @@ describe('Observable.ajax', () => {
request.respondWith({
'status': 204,
'contentType': 'application/json',
'responseType': 'json',
'responseText': expected
});

expect(result.response).to.deep.equal(expected);
expect(result.response).to.equal(expected);
expect(complete).to.be.true;
});

it('should succeed in IE on 204 No Content', () => {
const expected = null;
let result: Rx.AjaxResponse;
let complete = false;

root.XMLHttpRequest = MockXMLHttpRequest_InternetExplorer;

Rx.Observable
.ajax.post('/flibbertyJibbet', expected)
.subscribe(x => {
result = x;
}, null, () => {
complete = true;
});

const request = MockXMLHttpRequest.mostRecent;

expect(request.method).to.equal('POST');
expect(request.url).to.equal('/flibbertyJibbet');
expect(request.requestHeaders).to.deep.equal({
'X-Requested-With': 'XMLHttpRequest',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
});

//IE behavior: IE does not provide the a responseText property, so also exercise the code which handles this.
request.respondWith({
'status': 204,
'contentType': 'application/json'
});

expect(result.response).to.equal(expected);
expect(complete).to.be.true;
});

Expand Down

0 comments on commit f66c83b

Please sign in to comment.