can-fixture
Advanced tools
Comparing version 0.2.0 to 0.2.1
29
build.js
var stealTools = require("steal-tools"); | ||
var globalJS = require("steal-tools/lib/build/helpers/global").js; | ||
var baseHelpers = require("steal-tools/lib/build/helpers/base"); | ||
var baseNormalize = globalJS.normalize(); | ||
stealTools.export({ | ||
@@ -7,5 +12,27 @@ system: { | ||
}, | ||
options: { | ||
//verbose: true | ||
}, | ||
outputs: { | ||
"+amd": {}, | ||
"+global-js": {} | ||
"global": { | ||
modules: globalJS.modules(), | ||
format: "global", | ||
dest: globalJS.dest(), | ||
useNormalizedDependencies: globalJS.useNormalizedDependencies(), | ||
// makes sure can-set and can-connect are namespaced so there's no collision | ||
normalize: function(depName, depLoad){ | ||
var parsed = baseHelpers.parseModuleName(depLoad.name); | ||
var res; | ||
if(parsed.packageName !== "can-connect" && parsed.packageName !== "can-set") { | ||
res = baseNormalize.apply(this, arguments); | ||
} else { | ||
res = "fixture-"+parsed.packageName+"/"+parsed.modulePath | ||
} | ||
return res; | ||
}, | ||
ignore: function(name, load){ | ||
return false | ||
} | ||
} | ||
} | ||
@@ -12,0 +39,0 @@ }).catch(function(e){ |
17
core.js
// Adds | ||
var canSet = require("can-set"); | ||
var helpers = canSet.helpers; | ||
var helpers = require("./helpers/helpers"); | ||
var sub = require("./helpers/sub"); | ||
var Store = require("./store"); | ||
require("./store"); | ||
@@ -16,3 +16,3 @@ | ||
// `fixture("/things/{id}", store)` | ||
if(fixture && fixture instanceof Store) { | ||
if(fixture && (fixture.getData || fixture.getListData)) { | ||
var root = settings, | ||
@@ -102,4 +102,3 @@ store = fixture, | ||
}; | ||
setTimeout(function () { | ||
var callFixture = function () { | ||
// fall the fixture | ||
@@ -112,3 +111,9 @@ var result = fixtureSettings.fixture(xhrSettings, response, xhrSettings.headers, fixtureSettings); | ||
} | ||
}, $fixture.delay); | ||
}; | ||
if(!xhrSettings.async) { | ||
callFixture(); | ||
} else { | ||
setTimeout(callFixture, $fixture.delay); | ||
} | ||
}; | ||
@@ -115,0 +120,0 @@ |
var core = require("./core"); | ||
var fixture = core.add; | ||
var helpers = require("can-set").helpers; | ||
var helpers = require("./helpers/helpers"); | ||
var Store = require("./store"); | ||
@@ -67,2 +67,7 @@ require("./xhr"); | ||
if(typeof window !== "undefined" && !require.resolve) { | ||
window.fixture = fixture; | ||
} | ||
module.exports = fixture; |
@@ -1,2 +0,2 @@ | ||
var helpers = require("can-set").helpers; | ||
var helpers = require("./helpers"); | ||
@@ -3,0 +3,0 @@ var digitTest = /^\d+$/, |
@@ -1,2 +0,2 @@ | ||
var helpers = require("can-set").helpers; | ||
var helpers = require("./helpers"); | ||
var getId = require("./getid"); | ||
@@ -3,0 +3,0 @@ var canSet = require("can-set"); |
{ | ||
"name": "can-fixture", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Intercept AJAX requests and simulate responses.", | ||
@@ -26,3 +26,3 @@ "main": "fixture.js", | ||
"testee": "^0.2.5", | ||
"steal-tools": "^0.15.5", | ||
"steal-tools": "^0.15.7", | ||
"jquery": "^1.11.0", | ||
@@ -29,0 +29,0 @@ "jshint": "^2.7.0" |
@@ -16,2 +16,34 @@ [![Build Status](https://api.travis-ci.org/canjs/can-fixture.svg?branch=master)](https://travis-ci.org/canjs/can-fixture) | ||
- [Install](#install) | ||
- [Basic Use](#basic-use) | ||
- [API](#api) | ||
- <code>[fixture.on](#fixture.on)</code> | ||
- <code>[fixture(ajaxSettings, requestHandler(...))](#fixtureajaxsettings-requesthandler)</code> | ||
- <code>[requestHandler(request, response(...), requestHeaders, ajaxSettings)](#requesthandlerrequest-response-requestheaders-ajaxsettings)</code> | ||
- <code>[response(status, body, headers, statusText)](#responsestatus-body-headers-statustext)</code> | ||
- <code>[fixture(ajaxSettings, url)](#fixtureajaxsettings-url)</code> | ||
- <code>[fixture(ajaxSettings, data)](#fixtureajaxsettings-data)</code> | ||
- <code>[fixture(ajaxSettings, null)](#fixtureajaxsettings-null)</code> | ||
- <code>[fixture(methodAndUrl, url|data|requestHandler)](#fixturemethodandurl-urldatarequesthandler)</code> | ||
- <code>[fixture(url, url|data|requestHandler)](#fixtureurl-urldatarequesthandler)</code> | ||
- <code>[fixture(fixtures)](#fixturefixtures)</code> | ||
- <code>[fixture(restfulUrl, store)](#fixturerestfulurl-store)</code> | ||
- <code>[fixture.store(baseItems, algebra)](#fixturestorebaseitems-algebra)</code> | ||
- <code>[fixture.store(count, makeItems, algebra)](#fixturestorecount-makeitems-algebra)</code> | ||
- <code>[Store](#store)</code> | ||
- <code>[Store.prototype.getListData(request, response)](#storeprototypegetlistdatarequest-response)</code> | ||
- <code>[Store.prototype.getData(request, response)](#storeprototypegetdatarequest-response)</code> | ||
- <code>[Store.prototype.createData(request, response)](#storeprototypecreatedatarequest-response)</code> | ||
- <code>[Store.prototype.updateData(request, response)](#storeprototypeupdatedatarequest-response)</code> | ||
- <code>[Store.prototype.destroyData(request, response)](#storeprototypedestroydatarequest-response)</code> | ||
- <code>[Store.prototype.reset([baseItems])](#storeprototyperesetbaseitems)</code> | ||
- <code>[Store.prototype.get(params)](#storeprototypegetparams)</code> | ||
- <code>[Store.prototype.getList(set)](#storeprototypegetlistset)</code> | ||
- <code>[fixture.rand(min, max)](#fixturerandmin-max)</code> | ||
- <code>[fixture.rand(choices, min, max)](#fixturerandchoices-min-max)</code> | ||
- <code>[fixture.delay](#fixturedelay)</code> | ||
- <code>[fixture.on](#fixtureon)</code> | ||
- <code>[fixture.fixtures](#fixturefixtures-1)</code> | ||
## Install | ||
@@ -121,3 +153,3 @@ | ||
## APIs | ||
## API | ||
@@ -282,3 +314,3 @@ The `fixture` function has multiple signatures, most based on convenience. However, | ||
``` | ||
```js | ||
fixture("GET /tasks", requestHandler ); | ||
@@ -297,3 +329,3 @@ | ||
``` | ||
```js | ||
fixture("/tasks", requestHandler); | ||
@@ -482,3 +514,3 @@ | ||
``` | ||
```js | ||
todoStore.get({id: 1}) //-> {id: 1, name: "dishes"} | ||
@@ -491,3 +523,3 @@ ``` | ||
``` | ||
```js | ||
todoStore.get({name: "dishes"}) //-> {data: [{id: 1, name: "dishes"}]} | ||
@@ -531,3 +563,3 @@ ``` | ||
``` | ||
```js | ||
fixture.delay = 1000; // 1 second delay | ||
@@ -538,5 +570,7 @@ ``` | ||
<a id="fixture.on"/> | ||
Turns the fixtures on or off. Defaults to `true` for on. | ||
``` | ||
```js | ||
fixture.on = false; //-> AJAX requests will not be trapped | ||
@@ -543,0 +577,0 @@ ``` |
var canSet = require("can-set"); | ||
var helpers = canSet.helpers; | ||
var helpers = require("./helpers/helpers"); | ||
var connect = require("can-connect"); | ||
@@ -4,0 +4,0 @@ var legacyStore = require("./helpers/legacyStore"); |
@@ -1259,1 +1259,55 @@ var Qunit = require('steal-qunit'); | ||
}); | ||
asyncTest("supports addEventListener on shim using fixture", function(){ | ||
fixture("/addEventListener", function(){ | ||
return {}; | ||
}); | ||
var xhr = new XMLHttpRequest(); | ||
xhr.addEventListener('load', function(){ | ||
ok(true, "our shim supports addEventListener"); | ||
start(); | ||
}); | ||
xhr.open('GET', "/addEventListener"); | ||
xhr.send(); | ||
}); | ||
test("supports sync on XHR shim (#23)", function(){ | ||
var url = __dirname + '/fixtures/test.json'; | ||
var xhr = new XMLHttpRequest(); | ||
xhr.addEventListener('load', function(){ | ||
ok(true, "our shim supports addEventListener"); | ||
}); | ||
xhr.open('GET', url, false); | ||
xhr.send(); | ||
}); | ||
test("supports sync fixtures (#23)", function(){ | ||
fixture("/sync", function(){ | ||
return {}; | ||
}); | ||
var xhr = new XMLHttpRequest(); | ||
xhr.addEventListener('load', function(){ | ||
ok(true, "our shim supports sync"); | ||
}); | ||
xhr.open('GET', "/sync", false); | ||
xhr.send(); | ||
}); | ||
test("supports sync redirect fixtures (#23)", function(){ | ||
fixture("/sync_redirect", __dirname+'/fixtures/test.json'); | ||
var xhr = new XMLHttpRequest(); | ||
xhr.addEventListener('load', function(){ | ||
ok(true, "our shim supports sync redirect"); | ||
}); | ||
xhr.open('GET', "/sync_redirect", false); | ||
xhr.send(); | ||
}); |
81
xhr.js
@@ -6,6 +6,6 @@ // This overwrites the default XHR with a mock XHR object. | ||
var fixtureCore = require("./core"); | ||
var canSet = require("can-set"); | ||
var helpers = canSet.helpers; | ||
var helpers = require("./helpers/helpers"); | ||
var deparam = require("./helpers/deparam"); | ||
// Save the real XHR object as XHR | ||
@@ -16,2 +16,15 @@ var XHR = XMLHttpRequest, | ||
// Figure out events on XHR object | ||
// but start with some default events. | ||
var events = ["abort","error","load","loadend","loadstart","progress"]; | ||
(function(){ | ||
var x = new XHR(); | ||
for(var prop in x) { | ||
if(prop.indexOf("on") === 0 && | ||
events.indexOf(prop.substr(2)) === -1 && | ||
prop !== "onreadystatechange") { | ||
events.push(prop.substr(2)); | ||
} | ||
} | ||
})(); | ||
// DEFINE HELPERS | ||
@@ -40,2 +53,7 @@ | ||
var propsToIgnore = { onreadystatechange: true, onload: true, __events: true }; | ||
helpers.each(events, function(prop){ | ||
propsToIgnore["on"+prop] = true; | ||
}); | ||
// Helper that given the mockXHR, creates a real XHR that will call the mockXHR's | ||
@@ -47,3 +65,3 @@ // callbacks when the real XHR's request completes. | ||
// Copy everything on mock to it. | ||
assign(xhr, mockXHR); | ||
assign(xhr, mockXHR, propsToIgnore); | ||
@@ -57,3 +75,3 @@ // When the real XHR is called back, update all properties | ||
// values as the real xhr. | ||
assign(mockXHR, xhr,{ onreadystatechange: true, onload: true }); | ||
assign(mockXHR, xhr,propsToIgnore); | ||
if(mockXHR.onreadystatechange) { | ||
@@ -64,9 +82,13 @@ mockXHR.onreadystatechange(ev); | ||
xhr.onload = function(){ | ||
callEvents(mockXHR, "load"); | ||
if(mockXHR.onload) { | ||
return mockXHR.onload.apply(mockXHR, arguments); | ||
} | ||
}; | ||
// wire up events to forward to mock object | ||
helpers.each(events, function(eventName){ | ||
xhr["on"+eventName] = function(){ | ||
callEvents(mockXHR, eventName); | ||
if(mockXHR["on"+eventName]) { | ||
return mockXHR["on"+eventName].apply(mockXHR, arguments); | ||
} | ||
}; | ||
}); | ||
// wire up the mockXHR's getResponseHeader to call the real | ||
@@ -108,5 +130,6 @@ // XHR's getResponseHeader. | ||
}, | ||
open: function(type, url){ | ||
open: function(type, url, async){ | ||
this.type = type; | ||
this.url = url; | ||
this.async = async === false ? false : true; | ||
}, | ||
@@ -148,3 +171,4 @@ getAllResponseHeaders: function(){ | ||
headers: this._headers, | ||
type: this.type.toLowerCase() || 'get' | ||
type: this.type.toLowerCase() || 'get', | ||
async: this.async | ||
}; | ||
@@ -181,3 +205,5 @@ // if get or delete, the url should not include the querystring. | ||
}); | ||
if ( (status >= 200 && status < 300 || status === 304) ) { | ||
var success = (status >= 200 && status < 300 || status === 304); | ||
if ( success ) { | ||
helpers.extend(mockXHR,{ | ||
@@ -193,8 +219,31 @@ statusText: statusText || "OK", | ||
} | ||
if(mockXHR.onreadystatechange) { | ||
mockXHR.onreadystatechange({ target: mockXHR }); | ||
} | ||
if(mockXHR.onload) { | ||
mockXHR.onload(); | ||
// fire progress events | ||
callEvents(mockXHR, "progress"); | ||
if(mockXHR.onprogress) { | ||
mockXHR.onprogress(); | ||
} | ||
if(success) { | ||
callEvents(mockXHR, "load"); | ||
if(mockXHR.onload) { | ||
mockXHR.onload(); | ||
} | ||
} else { | ||
callEvents(mockXHR, "error"); | ||
if(mockXHR.onerror) { | ||
mockXHR.onerror(); | ||
} | ||
} | ||
callEvents(mockXHR, "loadend"); | ||
if(mockXHR.onloadend) { | ||
mockXHR.onloadend(); | ||
} | ||
}); | ||
@@ -218,5 +267,5 @@ } | ||
this._xhr = xhr; | ||
xhr.open(xhr.type, xhr.url); | ||
xhr.open( xhr.type, xhr.url, xhr.async ); | ||
return xhr.send(data); | ||
} | ||
}); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
85696
26
2222
577
0