can-fixture
can-fixture intercepts an AJAX request and simulates the response with a file or function.
fixture(ajaxSettings, requestHandler(...))
If an XHR request matches ajaxSettings, calls requestHandler with the XHR requests data. Makes the XHR request respond with the return value of requestHandler or the result of calling its response argument.
The following traps requests to GET /todos and responds with an array of data:
fixture({method: "get", url: "/todos"},
function(request, response, headers, ajaxSettings){
return {
data: [
{id: 1, name: "dishes"},
{id: 2, name: "mow"}
]
};
})
When adding a fixture, it will remove any identical fixtures from the list of fixtures. The last fixture added will be the first matched.
Parameters
- ajaxSettings
{ajaxSettings}
:An object that is used to match values on an XHR object, namely the url and method. url can be templated like /todos/{_id}.
- requestHandler
{requestHandler(request, response, requestHeaders, ajaxSettings)}
:Handles the request and provides a response. The next section details this function's use.
fixture(ajaxSettings, url)
Redirects the request to another url. This can be useful for simulating a response with a file.
fixture({url: "/tasks"}, "fixtures/tasks.json");
Placeholders available in the ajaxSettings
url will be available in the redirect url:
fixture({url: "/tasks/{id}"}, "fixtures/tasks/{id}.json");
fixture(ajaxSettings, data)
Responds with the JSON.stringify
result of data
.
fixture({url: "/tasks"}, {tasks: [{id: 1, complete: false}]});
fixture(ajaxSettings, delay)
Delays the ajax request from being made for delay
milliseconds.
fixture({url: "/tasks"}, 2000);
This doesn't simulate a response, but is useful for simulating slow connections.
fixture(ajaxSettings, null)
Removes the matching fixture from the list of fixtures.
fixture({url: "/tasks"}, "fixtures/tasks.json");
$.get("/tasks") // requests fixtures/tasks.json
fixture({url: "/tasks"}, null);
$.get("/tasks") // requests /tasks
fixture(methodAndUrl, url|data|requestHandler)
A short hand for creating an ajaxSettings with a method
and url
.
fixture("GET /tasks", requestHandler );
// is the same as
fixture({method: "get", url: "/tasks"}, requestHandler );
The format is METHOD URL
.
fixture(url, url|data|requestHandler)
A short hand for creating an ajaxSettings with just a url
.
fixture("/tasks", requestHandler);
// is the same as
fixture({url: "/tasks"}, requestHandler);
fixture(fixtures)
Create multiple fixtures at once.
fixture({
"POST /tasks": function(){
return {id: Math.random()}
},
"GET /tasks": {data: [{id: 1, name: "mow lawn"}]},
"/people": "fixtures/people.json"
});
Parameters
- fixtures
{Object<methodAndUrl,String|Object|requestHandler(request, response, requestHeaders, ajaxSettings)|Store()>}
:A mapping of methodAndUrl to some response argument type.
fixture(restfulUrl, store)
Wire up a restful API scheme to a store.
var todoAlgebra = new set.Algebra(
set.props.id("id")
);
var todoStore = fixture.store([
{ id: 1, name: 'Do the dishes'},
{ id: 2, name: 'Walk the dog'}
], todoAlgebra);
fixture("/api/todos/{id}", todoStore); // can also be written fixture("/api/todos", todoStore);
This is a shorthand for wiring up the todoStore
as follows:
fixture({
"GET /api/todos": todoStore.getListData,
"GET /api/todos/{id}": todoStore.getData,
"POST /api/todos": todoStore.createData,
"PUT /api/todos/{id}": todoStore.updateData,
"DELETE /api/todos/{id}": todoStore.destroyData
});
Parameters
- restfulUrl
{String}
:The url that may include a template for the place of the ID prop. The
list
url is assumed to berestfulUrl
with the/{ID_PROP}
part removed, if provided; otherwise theitem
url is assumed to have the/{ID_PROP}
part appended to the end. - store
{Store()}
:A store produced by store.