can-fixture
Intercept AJAX requests and simulate the response.
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.
When adding a fixture, it will remove any identical fixtures from the list of fixtures. The last fixture added will be the first matched.
The following traps requests to GET /todos and responds with an array of data:
import {fixture, ajax} from "can";
fixture( { method: "get", url: "/todos" },
( request, response, headers, ajaxSettings ) => {
return {
data: [
{ id: 1, name: "dishes" },
{ id: 2, name: "mow" }
]
};
}
);
ajax( {url: "/todos"} ).then( result => {
console.log( result.data ); //-> [{id: 1, name: "dishes"}, {id:2, name: "mow"}]
} );
Return a Promise (or use async & await) from requestHandler
to asynchronously return results. This allows fixtures to depend on each other, introduce dynamic delays, and even depend on external resources.
import {fixture, ajax} from "can";
fixture( { method: "get", url: "/todos" },
( request, response, headers, ajaxSettings ) => {
return new Promise((resolve) => {
setTimeout(() => resolve({
data: [
{ id: 1, name: "dishes" },
{ id: 2, name: "mow" }
]
}), 1000);
});
}
);
// or
fixture( { method: "get", url: "/todos" },
async ( request, response, headers, ajaxSettings ) => {
await delay(1000);
return {
data: [
{ id: 1, name: "dishes" },
{ id: 2, name: "mow" }
]
};
});
);
ajax( {url: "/todos"} ).then( result => {
console.log( result.data ); //-> [{id: 1, name: "dishes"}, {id:2, name: "mow"}]
} );
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.
Returns
{Array<ajaxSettings>}
:
Returns an array of any fixtures that are replaced by the fixture that is added.
fixture( ajaxSettings, url )
Redirects the request to another url. This can be useful for simulating a response with a file.
import {fixture, ajax} from "can";
fixture( { url: "/tasks" }, "/fixtures/tasks.json" );
ajax( {url: "/tasks"} ); // "can-fixture: /tasks => /fixtures/tasks.json"
Placeholders available in the ajaxSettings
url will be available in the redirect url:
import {fixture, ajax} from "can";
fixture( {url: "/tasks/{id}"}, "fixtures/tasks/{id}.json" );
ajax( {url: "/tasks/1"} ); // "can-fixture: /tasks/1 => /fixtures/tasks/1.json"
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
/tasks/{_id}
. - url
{String}
:The pathname of requests that will be trapped.
fixture( ajaxSettings, data )
Responds with the result of data
.
import {fixture, ajax} from "can";
fixture( {url: "/tasks"}, {tasks: [ {id: 1, complete: false} ]} );
ajax( {url: "/tasks"} ).then( result => {
console.log( result ); //-> {tasks:[{id:1, complete:false}]}
} );
Parameters
- ajaxSettings
{ajaxSettings}
:An object that is used to match values on an XHR object, namely the url and method.
- data
{Object}
:A representation of records in the store.
Returns
{Array<ajaxSettings>}
:
Returns an array of any fixtures that are replaced by the fixture that is added.
fixture(ajaxSettings, delay)
Delays the ajax request from being made for delay
milliseconds. See delay for more information.
import {fixture, ajax} from "can";
fixture( { url: "/tasks" }, 2000 );
ajax( {url: "/tasks"} ); // "can-fixture: /tasks => delay 2000ms"
This doesn't simulate a response, but is useful for simulating slow connections.
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}
. - delay
{Number}
:A numeric representation of milliseconds that the response should wait.
Returns
{Array<ajaxSettings>}
:
Returns an array of any fixtures that are replaced by the fixture that is added.
fixture(ajaxSettings, null)
Removes the matching fixture from the list of fixtures. See on for related.
import {fixture, ajax} from "can";
fixture( {url: "/tasks"}, "fixtures/tasks.json" );
ajax( {url: "/tasks"} ); // requests fixtures/tasks.json
fixture( {url: "/tasks"}, null );
// Made a request to "/tasks", but we catch a 404.
ajax( {url: "/tasks"} ).catch( error => {
console.log("error"); //-> "error"
});
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}
. - null
{object}
:A representation of the intentional absence of any object value. null is a JavaScript primitive value.
Returns
{Array<ajaxSettings>}
:
Returns an array of any fixtures that are replaced by the fixture that is added.
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 );
Parameters
- methodAndUrl
{Object}
:A string formatted like is
METHOD URL
. - url
{String|Object|requestHandler(request, response, requestHeaders, ajaxSettings)}
:url|data|requestHandler The URL that will be queried. A representation of records in the store. A definition for XHR response for a given trapped request.
Returns
{Array<ajaxSettings>}
:
Returns an array of any fixtures that are replaced by the fixture that is added.
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 );
Parameters
- url
{String}
:The pathname of requests that will be trapped.
- url
{String|Object|requestHandler(request, response, requestHeaders, ajaxSettings)}
:url|data|requestHandler The URL that will be queried. A representation of records in the store. A definition for XHR response for a given trapped request.
Returns
{Array<ajaxSettings>}
:
Returns an array of any fixtures that are replaced by the fixture that is added.
fixture( fixtures )
Create multiple fixtures at once.
import {fixture, ajax} from "can";
fixture( {
"POST /tasks": () => {
return { id: parseInt(Math.random() * 10, 10) };
},
"GET /tasks": { data: [ {id: 1, name: "mow lawn"} ] },
"/people": "fixtures/people.json"
} );
ajax( {type: "POST", url:"/tasks"} ).then( result => {
console.log( result ); //-> {id: RandomNumber}
} );
ajax( {url: "/tasks"} ).then( result => {
console.log( result.data ); //-> [ {id: 1, name: "mow lawn"} ]
} );
Parameters
- fixtures
{Object<methodAndUrl,String|Object|requestHandler(request, response, requestHeaders, ajaxSettings)|Store()>}
:A mapping of methodAndUrl to some response argument type.
Returns
{Array<ajaxSettings>}
:
Returns an array of any fixtures that are replaced by the fixture that is added.
fixture( restfulUrl, store )
Wire up a restful API scheme to a store.
import {QueryLogic, fixture, ajax} from "can";
const todoStore = fixture.store( [
{ id: 1, name: "Do the dishes" },
{ id: 2, name: "Walk the dog" }
], new QueryLogic( {identity: ["id"]} ) );
// can also be written fixture("/api/todos", todoStore);
fixture( "/api/todos/{id}", todoStore );
ajax( {url:"/api/todos/1"} ).then( result => {
console.log( result ); //-> "{'id':1,'name':'Do the dishes'}"
} );
This is a shorthand for wiring up the todoStore
as follows:
import {QueryLogic, fixture, ajax} from "can";
const todoStore = fixture.store( [
{ id: 1, name: "Do the dishes" },
{ id: 2, name: "Walk the dog" }
], new QueryLogic( {identity: ["id"]} ) );
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
} );
ajax( {url: "/api/todos/1"} ).then( result => {
console.log( result ); //-> "{'id':1,'name':'Do the dishes'}"
} );
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.
Returns
{Array<ajaxSettings>}
:
Returns an array of any fixtures that are replaced by the fixture that is added.
fixture(ajaxSettingsArray)
Add fixtures that have been previously removed with another call to fixture.
Parameters
- An
{Array<ajaxSettings>}
:array of AJAX settings objects
Returns
{Array<ajaxSettings>}
:
Returns an array of any fixtures that are replaced by the fixtures that are added.