can-fixture
Advanced tools
Comparing version 0.2.1 to 0.3.0
{ | ||
"name": "can-fixture", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "Intercept AJAX requests and simulate responses.", | ||
@@ -5,0 +5,0 @@ "main": "fixture.js", |
@@ -16,6 +16,7 @@ [![Build Status](https://api.travis-ci.org/canjs/can-fixture.svg?branch=master)](https://travis-ci.org/canjs/can-fixture) | ||
[Play around with can-fixture in this JSBin!](https://justinbmeyer.jsbin.com/zixumu/2/edit?html,js,output) | ||
- [Install](#install) | ||
- [Basic Use](#basic-use) | ||
- [API](#api) | ||
- <code>[fixture.on](#fixture.on)</code> | ||
- <code>[fixture(ajaxSettings, requestHandler(...))](#fixtureajaxsettings-requesthandler)</code> | ||
@@ -26,2 +27,3 @@ - <code>[requestHandler(request, response(...), requestHeaders, ajaxSettings)](#requesthandlerrequest-response-requestheaders-ajaxsettings)</code> | ||
- <code>[fixture(ajaxSettings, data)](#fixtureajaxsettings-data)</code> | ||
- <code>[fixture(ajaxSettings, delay)](#fixtureajaxsettings-delay)</code> | ||
- <code>[fixture(ajaxSettings, null)](#fixtureajaxsettings-null)</code> | ||
@@ -296,2 +298,12 @@ - <code>[fixture(methodAndUrl, url|data|requestHandler)](#fixturemethodandurl-urldatarequesthandler)</code> | ||
### `fixture(ajaxSettings, delay)` | ||
Delays the ajax request from being made for `delay` milliseconds. | ||
```js | ||
fixture({url: "/tasks"}, 2000); | ||
``` | ||
This doesn't simulate a response, but is useful for simulating slow connections. | ||
### `fixture(ajaxSettings, null)` | ||
@@ -298,0 +310,0 @@ |
@@ -1313,1 +1313,21 @@ var Qunit = require('steal-qunit'); | ||
}); | ||
asyncTest("slow mode works (#26)", function(){ | ||
var url = __dirname + '/fixtures/test.json'; | ||
fixture({url: url}, 1000); | ||
var xhr = new XMLHttpRequest(); | ||
var startTime = new Date(); | ||
xhr.addEventListener('load', function(){ | ||
ok(Math.abs(new Date() - startTime - 1000) < 300, "within 300 ms of 1 second delay"); | ||
fixture({url: url}, null); | ||
start(); | ||
}); | ||
xhr.open('GET', url); | ||
xhr.send(); | ||
}); |
23
xhr.js
@@ -185,7 +185,7 @@ // This overwrites the default XHR with a mock XHR object. | ||
var fixtureSettings = fixtureCore.get(xhrSettings); | ||
var mockXHR = this; | ||
// If a dynamic fixture is being used, we call the dynamic fixture function and then | ||
// copy the response back onto the `mockXHR` in the right places. | ||
if(fixtureSettings && typeof fixtureSettings.fixture === "function") { | ||
var mockXHR = this; | ||
return fixtureCore.callDynamicFixture(xhrSettings, fixtureSettings, function(status, body, headers, statusText){ | ||
@@ -246,4 +246,17 @@ body = typeof body === "string" ? body : JSON.stringify(body); | ||
// Make a realXHR object based around the settings of the mockXHR. | ||
var xhr = makeXHR(this); | ||
var xhr = makeXHR(this), | ||
makeRequest = function(){ | ||
mockXHR._xhr = xhr; | ||
xhr.open( xhr.type, xhr.url, xhr.async ); | ||
return xhr.send(data); | ||
}; | ||
if(fixtureSettings && typeof fixtureSettings.fixture === "number") { | ||
//!steal-remove-start | ||
fixtureCore.log(xhrSettings.url+" -> delay " + fixtureSettings.fixture+"ms"); | ||
//!steal-remove-end | ||
setTimeout(makeRequest, fixtureSettings.fixture); | ||
return; | ||
} | ||
// if we do have a fixture, update the real XHR object. | ||
@@ -258,6 +271,4 @@ if(fixtureSettings) { | ||
// Make the request. | ||
this._xhr = xhr; | ||
xhr.open( xhr.type, xhr.url, xhr.async ); | ||
return xhr.send(data); | ||
return makeRequest(); | ||
} | ||
}); |
89609
27
2245
589