can-fixture
Advanced tools
Comparing version 0.4.0-pre.8 to 0.4.0-pre.9
25
core.js
@@ -116,4 +116,5 @@ // Adds | ||
callFixture(); | ||
return null; | ||
} else { | ||
setTimeout(callFixture, $fixture.delay); | ||
return setTimeout(callFixture, $fixture.delay); | ||
} | ||
@@ -193,2 +194,11 @@ }; | ||
}; | ||
var isEmptyOrSubset = function(a, b) { | ||
if( a == null && isEmptyObject(b) ) { | ||
return true; | ||
} else if( b == null && isEmptyObject(a) ) { | ||
return true; | ||
} else { | ||
return canSet.subset(a, b); | ||
} | ||
}; | ||
@@ -203,5 +213,11 @@ // Comparator object used to find a similar fixture. | ||
}, | ||
xhr: function(){ | ||
return true; | ||
}, | ||
type: function(a,b){ | ||
return b && a ? a.toLowerCase() === b.toLowerCase() : b === a; | ||
}, | ||
method: function(a,b){ | ||
return b && a ? a.toLowerCase() === b.toLowerCase() : b === a; | ||
}, | ||
helpers: function(){ | ||
@@ -211,3 +227,3 @@ return true; | ||
headers: isEmptyOrNull, | ||
data: isEmptyOrNull | ||
data: isEmptyOrSubset | ||
}; | ||
@@ -220,2 +236,7 @@ | ||
exports.dataFromUrl = function (fixtureUrl, url) { | ||
if(!fixtureUrl) { | ||
// if there's no url, it's a match | ||
return {}; | ||
} | ||
var order = [], | ||
@@ -222,0 +243,0 @@ // Sanitizes fixture URL |
{ | ||
"name": "can-fixture", | ||
"version": "0.4.0-pre.8", | ||
"version": "0.4.0-pre.9", | ||
"description": "Intercept AJAX requests and simulate responses.", | ||
@@ -25,8 +25,9 @@ "main": "fixture.js", | ||
"devDependencies": { | ||
"bit-docs": "0.0.7", | ||
"jquery": "^1.11.0", | ||
"jshint": "^2.7.0", | ||
"steal": "^0.16.21", | ||
"steal-qunit": "0.1.1", | ||
"testee": "^0.2.5", | ||
"steal-tools": "^0.16.5", | ||
"jquery": "^1.11.0", | ||
"jshint": "^2.7.0" | ||
"testee": "^0.2.5" | ||
}, | ||
@@ -48,3 +49,19 @@ "repository": { | ||
"npmAlgorithm": "flat" | ||
}, | ||
"bit-docs": { | ||
"dependencies": { | ||
"bit-docs-glob-finder": "^0.0.5", | ||
"bit-docs-dev": "^0.0.3", | ||
"bit-docs-js": "^0.0.4", | ||
"bit-docs-generate-readme": "^0.0.10" | ||
}, | ||
"glob": { | ||
"pattern": "**/*.{js,md}", | ||
"ignore": "node_modules/**/*" | ||
}, | ||
"parent": "can-fixture", | ||
"readme": { | ||
"apis": "./docs/apis.json" | ||
} | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
var Qunit = require('steal-qunit'); | ||
var QUnit = require('steal-qunit'); | ||
var fixture = require("can-fixture"); | ||
@@ -10,3 +10,3 @@ var core = require("../core"); | ||
var errorCallback = function(xhr, status, error){ | ||
ok(false); | ||
ok(false, error); | ||
start(); | ||
@@ -205,3 +205,3 @@ }; | ||
// every item can be first | ||
for(var i = 0; i < 100; i++) { | ||
for(i = 0; i < 100; i++) { | ||
result = rand(choices); | ||
@@ -629,2 +629,3 @@ matched[result.length] = true; | ||
year: function(a, b){ | ||
/* jshint eqeqeq:false */ | ||
return a == b; | ||
@@ -634,2 +635,3 @@ | ||
modelId: function(a, b){ | ||
/* jshint eqeqeq:false */ | ||
return a == b; | ||
@@ -732,3 +734,3 @@ } | ||
ok(false, ""+e); | ||
start() | ||
start(); | ||
}); | ||
@@ -897,3 +899,2 @@ | ||
test("supports setDisableHeaderCheck", function(){ | ||
var url = __dirname + '/fixtures/test.json'; | ||
var xhr = new XMLHttpRequest(); | ||
@@ -988,3 +989,3 @@ | ||
equal(val, "bar"); | ||
} | ||
}; | ||
} | ||
@@ -994,3 +995,3 @@ if(originalXhr.readyState === 4) { | ||
} | ||
} | ||
}; | ||
xhr.send(); | ||
@@ -1065,3 +1066,3 @@ }); | ||
}).then(function(car){ | ||
equal(car.name, "2013 Altima", "get a single car works") | ||
equal(car.name, "2013 Altima", "get a single car works"); | ||
start(); | ||
@@ -1137,3 +1138,3 @@ }); | ||
}).then(function(car){ | ||
equal(car.name, "2013 Altima", "get a single car works") | ||
equal(car.name, "2013 Altima", "get a single car works"); | ||
start(); | ||
@@ -1249,3 +1250,3 @@ }); | ||
fixture('/cars/{_id}', store) | ||
fixture('/cars/{_id}', store); | ||
@@ -1288,3 +1289,3 @@ var findAll = function(){ | ||
}).then(function(car){ | ||
equal(car.name, "2013 Altima", "get a single car works") | ||
equal(car.name, "2013 Altima", "get a single car works"); | ||
start(); | ||
@@ -1409,3 +1410,3 @@ }); | ||
start(); | ||
}) | ||
}); | ||
@@ -1425,3 +1426,3 @@ xhr.open('GET', '/onload'); | ||
start(); | ||
} | ||
}; | ||
@@ -1442,2 +1443,78 @@ var xhr = new XMLHttpRequest(); | ||
asyncTest('fixture with timeout aborts if xhr timeout less than delay', function() { | ||
fixture('/onload', 1000); | ||
var xhr = new XMLHttpRequest(); | ||
xhr.open('GET', '/onload'); | ||
setTimeout(function() { | ||
xhr.abort(); | ||
}, 50); | ||
xhr.send(); | ||
xhr.addEventListener('error', function() { | ||
fixture('/onload', null); | ||
ok(true, 'Got to the error handler'); | ||
equal(xhr.statusText, "aborted"); | ||
equal(xhr.status, "0"); | ||
start(); | ||
}); | ||
xhr.addEventListener('load', function() { | ||
fixture('/onload', null); | ||
ok(false, 'timed out xhr did not abort'); | ||
start(); | ||
}); | ||
}); | ||
asyncTest('dynamic fixture with timeout does not run if xhr timeout less than delay', function() { | ||
var delay = fixture.delay; | ||
fixture.delay = 1000; | ||
fixture('/onload', function() { | ||
fixture('/onload', null); | ||
ok(false, 'timed out xhr did not abort'); | ||
start(); | ||
}); | ||
var xhr = new XMLHttpRequest(); | ||
xhr.open('GET', '/onload'); | ||
setTimeout(function() { | ||
xhr.abort(); | ||
}, 50); | ||
xhr.send(); | ||
xhr.addEventListener('error', function() { | ||
fixture('/onload', null); | ||
ok(true, 'Got to the error handler'); | ||
equal(xhr.statusText, "aborted"); | ||
equal(xhr.status, "0"); | ||
start(); | ||
}); | ||
fixture.delay = delay; | ||
}); | ||
asyncTest('fixture with timeout does not run if $.ajax timeout less than delay', function() { | ||
var delay = fixture.delay; | ||
fixture.delay = 1000; | ||
fixture('/onload', function() { | ||
fixture('/onload', null); | ||
ok(false, 'timed out xhr did not abort'); | ||
start(); | ||
}); | ||
$.ajax({ | ||
url: '/onload', | ||
timeout: 50, | ||
error: function(xhr) { | ||
fixture('/onload', null); | ||
ok(true, 'Got to the error handler'); | ||
equal(xhr.statusText, "timeout"); | ||
equal(xhr.status, "0"); | ||
start(); | ||
} | ||
}); | ||
fixture.delay = delay; | ||
}); | ||
asyncTest("response headers are set", function(){ | ||
@@ -1459,1 +1536,37 @@ fixture("GET /todos", function(request, response){ | ||
}); | ||
asyncTest("match values in get data", function(){ | ||
fixture({ | ||
method: "GET", | ||
url: "/data-value", | ||
data: {name: "justin"} | ||
}, function(request, response){ | ||
QUnit.ok(true, "got it"); | ||
return {}; | ||
}); | ||
var xhr = new XMLHttpRequest(); | ||
xhr.addEventListener('load', function(){ | ||
QUnit.start(); | ||
}); | ||
xhr.open('GET', "/data-value?name=justin&age=22"); | ||
xhr.send(); | ||
}); | ||
asyncTest("universal match (#2000)", function(){ | ||
fixture({},function(){ | ||
ok(true, "got hit"); | ||
return {}; | ||
}); | ||
var xhr = new XMLHttpRequest(); | ||
xhr.addEventListener('load', function(){ | ||
QUnit.start(); | ||
fixture.fixtures.splice(0, fixture.fixtures.length); | ||
}); | ||
xhr.open('GET', "/something-totally-unexpected-62"); | ||
xhr.send(); | ||
}); |
58
xhr.js
@@ -0,1 +1,4 @@ | ||
/* global require, window, global */ | ||
/* global setTimeout, clearTimeout, XMLHttpRequest */ | ||
// This overwrites the default XHR with a mock XHR object. | ||
@@ -8,4 +11,15 @@ // The mock XHR object's `.send` method is able to | ||
var each = require("can-util/js/each/each"); | ||
var assign = require("can-util/js/assign/assign"); | ||
// Copy props from source to dest, except those on the XHR prototype and | ||
// listed as excluding. | ||
var assign = function(dest, source, excluding){ | ||
excluding = excluding || {}; | ||
// copy everything on this to the xhr object that is not on `this`'s prototype | ||
for(var prop in source){ | ||
if(!( prop in XMLHttpRequest.prototype) && !excluding[prop] ) { | ||
dest[prop] = source[prop]; | ||
} | ||
} | ||
}; | ||
// Save the real XHR object as XHR | ||
@@ -39,15 +53,3 @@ var XHR = XMLHttpRequest, | ||
} | ||
// Copy props from source to dest, except those on the XHR prototype and | ||
// listed as excluding. | ||
var assign = function(dest, source, excluding){ | ||
excluding = excluding || {}; | ||
// copy everything on this to the xhr object that is not on `this`'s prototype | ||
for(var prop in source){ | ||
if(!( prop in XMLHttpRequest.prototype) && !excluding[prop] ) { | ||
dest[prop] = source[prop]; | ||
} | ||
} | ||
}; | ||
var propsToIgnore = { onreadystatechange: true, onload: true, __events: true }; | ||
@@ -161,2 +163,21 @@ each(events, function(prop){ | ||
}, | ||
abort: function() { | ||
assign(this,{ | ||
readyState: 4, | ||
status: 0, | ||
statusText: "aborted" | ||
}); | ||
clearTimeout(this.timeoutId); | ||
if(this.onreadystatechange) { | ||
this.onreadystatechange({ target: this }); | ||
} | ||
callEvents(this, "error"); | ||
if(this.onerror) { | ||
this.onerror(); | ||
} | ||
callEvents(this, "loadend"); | ||
if(this.onloadend) { | ||
this.onloadend(); | ||
} | ||
}, | ||
// This needs to compile the information necessary to see if | ||
@@ -180,3 +201,4 @@ // there is a corresponding fixture. | ||
method: type, | ||
async: this.async | ||
async: this.async, | ||
xhr: this | ||
}; | ||
@@ -202,2 +224,4 @@ // if get or delete, the url should not include the querystring. | ||
var mockXHR = this; | ||
var timeoutId; | ||
// If a dynamic fixture is being used, we call the dynamic fixture function and then | ||
@@ -207,3 +231,3 @@ // copy the response back onto the `mockXHR` in the right places. | ||
return fixtureCore.callDynamicFixture(xhrSettings, fixtureSettings, function(status, body, headers, statusText){ | ||
this.timeoutId = fixtureCore.callDynamicFixture(xhrSettings, fixtureSettings, function(status, body, headers, statusText){ | ||
body = typeof body === "string" ? body : JSON.stringify(body); | ||
@@ -256,2 +280,4 @@ | ||
}); | ||
return timeoutId; | ||
} | ||
@@ -278,3 +304,3 @@ // At this point there is either not a fixture or a redirect fixture. | ||
//!steal-remove-end | ||
setTimeout(makeRequest, fixtureSettings.fixture); | ||
this.timeoutId = setTimeout(makeRequest, fixtureSettings.fixture); | ||
return; | ||
@@ -281,0 +307,0 @@ } |
Sorry, the diff of this file is not supported yet
128312
48
2467
7