Skip to content

Commit

Permalink
Fixes for IE 8 and IE 9
Browse files Browse the repository at this point in the history
- Object.keys isn't available in IE 8, so using a local objectKeys helper method
that accomplishes the same effect for spec
- The DONE property isn't supported in IE 8, so falling back to the
numerical 4 in spec when necessary
- Extending from an XMLHttpRequest can't extend properties that are
filled in asynchronously (status, responseText, etc.) on IE 8 and IE 9
so we now skip these properties when extending
  • Loading branch information
sheelc committed Jun 1, 2014
1 parent d103af2 commit 85be9be
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 17 deletions.
19 changes: 16 additions & 3 deletions lib/mock-ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,25 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

(function() {
function extend(destination, source) {
function extend(destination, source, propertiesToSkip) {
propertiesToSkip = propertiesToSkip || [];
for (var property in source) {
destination[property] = source[property];
if (!arrayContains(propertiesToSkip, property)) {
destination[property] = source[property];
}
}
return destination;
}

function arrayContains(arr, item) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] === item) {
return true;
}
}
return false;
}

function MockAjax(global) {
var requestTracker = new RequestTracker(),
stubTracker = new StubTracker(),
Expand Down Expand Up @@ -101,7 +113,8 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
this.requestHeaders = {};
}

extend(FakeXMLHttpRequest.prototype, new window.XMLHttpRequest());
var iePropertiesThatCannotBeCopied = ['responseBody', 'responseText', 'responseXML', 'status', 'statusText', 'responseTimeout'];
extend(FakeXMLHttpRequest.prototype, new window.XMLHttpRequest(), iePropertiesThatCannotBeCopied);
extend(FakeXMLHttpRequest.prototype, {
open: function() {
this.method = arguments[0];
Expand Down
25 changes: 14 additions & 11 deletions spec/javascripts/fake-xml-http-request-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ describe("FakeXMLHttpRequest", function() {
xhr2 = new fakeGlobal.XMLHttpRequest();
});

function objectKeys(obj) {
keys = [];
for (key in obj) {
if (obj.hasOwnProperty(key)) {
keys.push(key);
}
}

return keys;
}

it("should have an initial readyState of 0 (uninitialized)", function() {
expect(xhr.readyState).toEqual(0);
});
Expand All @@ -21,7 +32,7 @@ describe("FakeXMLHttpRequest", function() {
});

it("should make the request headers available", function() {
expect(Object.keys(xhr.requestHeaders).length).toEqual(1);
expect(objectKeys(xhr.requestHeaders).length).toEqual(1);
expect(xhr.requestHeaders['X-Header-1']).toEqual('one');
});

Expand All @@ -31,12 +42,12 @@ describe("FakeXMLHttpRequest", function() {
});

it("should make the only its request headers available", function() {
expect(Object.keys(xhr2.requestHeaders).length).toEqual(1);
expect(objectKeys(xhr2.requestHeaders).length).toEqual(1);
expect(xhr2.requestHeaders['X-Header-2']).toEqual('two');
});

it("should not modify any other xhr objects", function() {
expect(Object.keys(xhr.requestHeaders).length).toEqual(1);
expect(objectKeys(xhr.requestHeaders).length).toEqual(1);
expect(xhr.requestHeaders['X-Header-1']).toEqual('one');
});
});
Expand Down Expand Up @@ -97,14 +108,6 @@ describe("FakeXMLHttpRequest", function() {
});
});

it("can be extended", function(){
pending("why do we want to do this?");
FakeXMLHttpRequest.prototype.foo = function(){
return "foo";
};
expect(new FakeXMLHttpRequest().foo()).toEqual("foo");
});

describe("data", function() {
beforeEach(function() {
xhr.open("POST", "http://example.com?this=that");
Expand Down
2 changes: 1 addition & 1 deletion spec/javascripts/mock-ajax-toplevel-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe("Jasmine Mock Ajax (for toplevel)", function() {
complete = jasmine.createSpy("onComplete");

onreadystatechange = function() {
if (this.readyState == this.DONE) {
if (this.readyState == (this.DONE || 4)) { // IE 8 doesn't support DONE
if (this.status == 200) {
if (this.responseHeaders['Content-type'] === 'application/json') {
this.response = JSON.parse(this.responseText);
Expand Down
4 changes: 2 additions & 2 deletions spec/javascripts/webmock-style-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe("Webmock style mocking", function() {
url = url || "http://example.com/someApi"
var xhr = new fakeGlobal.XMLHttpRequest();
xhr.onreadystatechange = function(arguments) {
if (this.readyState == this.DONE) {
if (this.readyState == (this.DONE || 4)) { // IE 8 doesn't support DONE
response = this;
successSpy();
}
Expand Down Expand Up @@ -85,7 +85,7 @@ describe("Webmock style mocking", function() {
var postRequest = function(data) {
var xhr = new fakeGlobal.XMLHttpRequest();
xhr.onreadystatechange = function(arguments) {
if (this.readyState == this.DONE) {
if (this.readyState == (this.DONE || 4)) { // IE 8 doesn't support DONE
response = this;
successSpy();
}
Expand Down

0 comments on commit 85be9be

Please sign in to comment.