can-fixture
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -217,2 +217,8 @@ var canSet = require("can-set"); | ||
}; | ||
this.__events = {}; | ||
// The way code detects if the browser supports onload is to check | ||
// if a new XHR object has the onload property, so setting it to null | ||
// passes that check. | ||
this.onload = null; | ||
this.onerror = null; | ||
}; | ||
@@ -231,2 +237,19 @@ | ||
XMLHttpRequest.prototype.addEventListener = function(ev, fn){ | ||
var evs = this.__events[ev] = this.__events[ev] || []; | ||
evs.push(fn); | ||
}; | ||
XMLHttpRequest.prototype.removeEventListener = function(ev, fn){ | ||
var evs = this.__events[ev] = this.__events[ev] || []; | ||
var idx = evs.indexOf(fn); | ||
if(idx >= 0) { | ||
evs.splice(idx, 1); | ||
} | ||
}; | ||
XMLHttpRequest.prototype.setDisableHeaderCheck = function(val){ | ||
this._disableHeaderCheck = !!val; | ||
}; | ||
XMLHttpRequest.prototype.send = function(data) { | ||
@@ -283,2 +306,3 @@ var settings = { | ||
self.onload && self.onload(); | ||
callEvents(self, "load"); | ||
} | ||
@@ -296,2 +320,3 @@ }, | ||
self.onload && self.onload(); | ||
callEvents(self, "load"); | ||
} | ||
@@ -322,12 +347,19 @@ }, fixture.delay); | ||
// values as the real xhr. | ||
copyProps(xhr, self, { onreadystatechange: true }); | ||
copyProps(xhr, self, { onreadystatechange: true, onload: true }); | ||
self.onreadystatechange && self.onreadystatechange(ev); | ||
if(self.onload && !xhr.__onloadCalled) { | ||
self.onload(); | ||
xhr.__onloadCalled = true; | ||
} | ||
} | ||
}; | ||
xhr.onload = function(){ | ||
callEvents(self, "load"); | ||
if(self.onload) { | ||
return self.onload.apply(this, arguments); | ||
} | ||
}; | ||
if(this._disableHeaderCheck && xhr.setDisableHeaderCheck) { | ||
xhr.setDisableHeaderCheck(true); | ||
} | ||
//helpers.extend(xhr, this); | ||
@@ -341,2 +373,13 @@ helpers.extend(xhr, settings); | ||
/** | ||
* Call all of an event for an XHR object | ||
*/ | ||
function callEvents(xhr, ev) { | ||
var evs = xhr.__events[ev] || [], fn; | ||
for(var i = 0, len = evs.length; i < len; i++) { | ||
fn = evs[i]; | ||
fn.call(xhr); | ||
} | ||
} | ||
// overwrite XHR | ||
@@ -343,0 +386,0 @@ |
{ | ||
"name": "can-fixture", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Intercept AJAX requests and simulate responses.", | ||
@@ -5,0 +5,0 @@ "main": "fixture.js", |
@@ -810,2 +810,73 @@ var Qunit = require('steal-qunit'); | ||
xhr.send(); | ||
}); | ||
}); | ||
QUnit.module("XHR Shim"); | ||
test("Supports onload", function(){ | ||
var xhr = new XMLHttpRequest(); | ||
QUnit.ok(("onload" in xhr), "shim passes onload detection"); | ||
}); | ||
asyncTest("supports addEventListener on XHR shim", function(){ | ||
var url = __dirname + '/fixtures/test.json'; | ||
var xhr = new XMLHttpRequest(); | ||
xhr.addEventListener('load', function(){ | ||
ok(true, "our shim supports addEventListener"); | ||
start(); | ||
}); | ||
xhr.open('GET', url); | ||
xhr.send(); | ||
}); | ||
asyncTest("supports removeEventListener on XHR shim", function(){ | ||
var url = __dirname + '/fixtures/test.json'; | ||
var xhr = new XMLHttpRequest(); | ||
var onload = function(){ | ||
ok(false, "this should not be called"); | ||
}; | ||
xhr.addEventListener('load', onload); | ||
xhr.removeEventListener("load", onload); | ||
xhr.onload = function(){ | ||
setTimeout(function(){ | ||
ok(true, 'didn\'t call the event listener'); | ||
start(); | ||
}); | ||
}; | ||
xhr.open('GET', url); | ||
xhr.send(); | ||
}); | ||
test("supports setDisableHeaderCheck", function(){ | ||
var url = __dirname + '/fixtures/test.json'; | ||
var xhr = new XMLHttpRequest(); | ||
try { | ||
xhr.setDisableHeaderCheck(true); | ||
ok(true, "did not throw"); | ||
} catch(e) { | ||
ok(false, "do not support setDisableHeaderCheck"); | ||
} | ||
}); | ||
asyncTest("supports setRequestHeader", function(){ | ||
var url = __dirname + '/fixtures/test.json'; | ||
var xhr = new XMLHttpRequest(); | ||
xhr.setRequestHeader("foo", "bar"); | ||
xhr.onreadystatechange = function(){ | ||
if(xhr.readyState === 4) { | ||
equal(xhr._headers.foo, "bar", "header was set"); | ||
start(); | ||
} | ||
}; | ||
xhr.open("GET", url); | ||
xhr.send(); | ||
}); |
50880
1616