can-fixture
Advanced tools
Comparing version 3.0.1 to 3.0.2
@@ -8,147 +8,233 @@ @module {function} can-fixture | ||
@description can-fixture intercepts an AJAX request and simulates the response with a file or function. | ||
@description Intercept AJAX requests and simulate the response. | ||
@signature `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. | ||
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: | ||
The following traps requests to GET /todos and responds with an array of data: | ||
```js | ||
fixture( { method: "get", url: "/todos" }, | ||
function( request, response, headers, ajaxSettings ) { | ||
return { | ||
data: [ | ||
{ id: 1, name: "dishes" }, | ||
{ id: 2, name: "mow" } | ||
] | ||
}; | ||
} ); | ||
``` | ||
```js | ||
import {fixture, ajax} from "can"; | ||
When adding a fixture, it will remove any identical fixtures from the list of fixtures. The last fixture added will be the first matched. | ||
fixture( { method: "get", url: "/todos" }, | ||
( request, response, headers, ajaxSettings ) => { | ||
return { | ||
data: [ | ||
{ id: 1, name: "dishes" }, | ||
{ id: 2, name: "mow" } | ||
] | ||
}; | ||
} | ||
); | ||
@param {can-fixture/types/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}. | ||
@param {can-fixture.requestHandler} requestHandler Handles the request and provides a response. The next section details this function's use. | ||
ajax( {url: "/todos"} ).then( result => { | ||
console.log( result.data ); //-> [{id: 1, name: "dishes"}, {id:2, name: "mow"}] | ||
} ); | ||
@signature `fixture(ajaxSettings, url)` | ||
``` | ||
@codepen | ||
Redirects the request to another url. This can be useful for simulating a response with a file. | ||
When adding a fixture, it will remove any identical fixtures from the list of fixtures. The last fixture added will be the first matched. | ||
```js | ||
fixture( { url: "/tasks" }, "fixtures/tasks.json" ); | ||
``` | ||
@param {can-fixture/types/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}`. | ||
@param {can-fixture.requestHandler} requestHandler Handles the request and provides a response. | ||
Placeholders available in the `ajaxSettings` url will be available in the redirect url: | ||
@signature `fixture( ajaxSettings, url )` | ||
```js | ||
fixture( { url: "/tasks/{id}" }, "fixtures/tasks/{id}.json" ); | ||
``` | ||
Redirects the request to another url. This can be useful for simulating a response with a file. | ||
@signature `fixture(ajaxSettings, data)` | ||
```js | ||
import {fixture, ajax} from "can"; | ||
Responds with the `JSON.stringify` result of `data`. | ||
fixture( { url: "/tasks" }, "/fixtures/tasks.json" ); | ||
```js | ||
fixture( { url: "/tasks" }, { tasks: [ { id: 1, complete: false } ] } ); | ||
``` | ||
ajax( {url: "/tasks"} ); // "can-fixture: /tasks => /fixtures/tasks.json" | ||
``` | ||
@codepen | ||
@highlight 3 | ||
Placeholders available in the `ajaxSettings` url will be available in the redirect url: | ||
```js | ||
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" | ||
``` | ||
@codepen | ||
@highlight 3 | ||
@param {can-fixture/types/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}`. | ||
@param {String} url The pathname of requests that will be trapped. | ||
@signature `fixture( ajaxSettings, data )` | ||
Responds with the result of `data`. | ||
```js | ||
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}]} | ||
} ); | ||
``` | ||
@codepen | ||
@highlight 3 | ||
@param {can-fixture/types/ajaxSettings} ajaxSettings An object that is used to match values on an XHR object, namely the url and method. | ||
@param {Object} data A representation of records in the store. | ||
@signature `fixture(ajaxSettings, delay)` | ||
Delays the ajax request from being made for `delay` milliseconds. | ||
Delays the ajax request from being made for `delay` milliseconds. See [can-fixture.delay delay] for more information. | ||
```js | ||
fixture( { url: "/tasks" }, 2000 ); | ||
``` | ||
```js | ||
import {fixture, ajax} from "can"; | ||
This doesn't simulate a response, but is useful for simulating slow connections. | ||
fixture( { url: "/tasks" }, 2000 ); | ||
ajax( {url: "/tasks"} ); // "can-fixture: /tasks => delay 2000ms" | ||
``` | ||
@codepen | ||
@highlight 3 | ||
This doesn't simulate a response, but is useful for simulating slow connections. | ||
@param {can-fixture/types/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}`. | ||
@param {Number} delay A numeric representation of milliseconds that the response should wait. | ||
@signature `fixture(ajaxSettings, null)` | ||
Removes the matching fixture from the list of fixtures. | ||
Removes the matching fixture from the list of fixtures. See [can-fixture.on on] for related. | ||
```js | ||
fixture( { url: "/tasks" }, "fixtures/tasks.json" ); | ||
```js | ||
import {fixture, ajax} from "can"; | ||
$.get( "/tasks" ); // requests fixtures/tasks.json | ||
fixture( {url: "/tasks"}, "fixtures/tasks.json" ); | ||
fixture( { url: "/tasks" }, null ); | ||
ajax( {url: "/tasks"} ); // requests fixtures/tasks.json | ||
$.get( "/tasks" ); // requests /tasks | ||
``` | ||
fixture( {url: "/tasks"}, null ); | ||
@signature `fixture(methodAndUrl, url|data|requestHandler)` | ||
// Made a request to "/tasks", but we catch a 404. | ||
ajax( {url: "/tasks"} ).catch( error => { | ||
console.log("error"); //-> "error" | ||
}); | ||
A short hand for creating an [can-fixture/types/ajaxSettings] with a `method` and `url`. | ||
``` | ||
@codepen | ||
```js | ||
fixture( "GET /tasks", requestHandler ); | ||
@param {can-fixture/types/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}`. | ||
@param {object} null A representation of the intentional absence of any object value. [null](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null) is a JavaScript primitive value. | ||
// is the same as | ||
@signature `fixture( methodAndUrl, url|data|requestHandler )` | ||
fixture( { method: "get", url: "/tasks" }, requestHandler ); | ||
``` | ||
A short hand for creating an [can-fixture/types/ajaxSettings] with a `method` and `url`. | ||
The format is `METHOD URL`. | ||
```js | ||
fixture( "GET /tasks", requestHandler ); | ||
@signature `fixture(url, url|data|requestHandler)` | ||
// is the same as | ||
A short hand for creating an [can-fixture/types/ajaxSettings] with just a `url`. | ||
fixture( { method: "get", url: "/tasks" }, requestHandler ); | ||
``` | ||
```js | ||
fixture( "/tasks", requestHandler ); | ||
@param {Object} methodAndUrl A string formatted like is `METHOD URL`. | ||
@param {String|Object|can-fixture.requestHandler} 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. | ||
// is the same as | ||
@signature `fixture( url, url|data|requestHandler )` | ||
fixture( { url: "/tasks" }, requestHandler ); | ||
``` | ||
A short hand for creating an [can-fixture/types/ajaxSettings] with just a `url`. | ||
@signature `fixture(fixtures)` | ||
```js | ||
fixture( "/tasks", requestHandler ); | ||
Create multiple fixtures at once. | ||
// is the same as | ||
```js | ||
fixture( { | ||
"POST /tasks": function() { | ||
return { id: Math.random() }; | ||
}, | ||
"GET /tasks": { data: [ { id: 1, name: "mow lawn" } ] }, | ||
"/people": "fixtures/people.json" | ||
} ); | ||
``` | ||
fixture( { url: "/tasks" }, requestHandler ); | ||
``` | ||
@param {String} url The pathname of requests that will be trapped. | ||
@param {String|Object|can-fixture.requestHandler} 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. | ||
@param {Object<methodAndUrl,String|Object|can-fixture.requestHandler|can-fixture/StoreType>} fixtures A mapping of methodAndUrl to | ||
some response argument type. | ||
@signature `fixture( fixtures )` | ||
Create multiple fixtures at once. | ||
```js | ||
import {fixture, ajax} from "can"; | ||
@signature `fixture(restfulUrl, store)` | ||
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} | ||
} ); | ||
Wire up a restful API scheme to a store. | ||
ajax( {url: "/tasks"} ).then( result => { | ||
console.log( result.data ); //-> [ {id: 1, name: "mow lawn"} ] | ||
} ); | ||
``` | ||
@codepen | ||
```js | ||
const todoQueryLogic = new QueryLogic( | ||
{identity: ["id"]} | ||
); | ||
const todoStore = fixture.store( [ | ||
{ id: 1, name: "Do the dishes" }, | ||
{ id: 2, name: "Walk the dog" } | ||
], todoQueryLogic ); | ||
@param {Object<methodAndUrl,String|Object|can-fixture.requestHandler|can-fixture/StoreType>} fixtures A mapping of methodAndUrl to some response argument type. | ||
fixture( "/api/todos/{id}", todoStore ); // can also be written fixture("/api/todos", todoStore); | ||
``` | ||
@signature `fixture( restfulUrl, store )` | ||
This is a shorthand for wiring up the `todoStore` as follows: | ||
Wire up a restful API scheme to a store. | ||
```js | ||
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 | ||
} ); | ||
``` | ||
```js | ||
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'}" | ||
} ); | ||
``` | ||
@codepen | ||
This is a shorthand for wiring up the `todoStore` as follows: | ||
```js | ||
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'}" | ||
} ); | ||
``` | ||
@codepen | ||
@highlight 8-14,only | ||
@param {String} restfulUrl The url that may include a template for the place of the ID prop. The `list` url is assumed to be `restfulUrl` with the `/{ID_PROP}` part removed, if provided; otherwise the `item` url is assumed to have the `/{ID_PROP}` part appended to the end. | ||
@param {can-fixture/StoreType} store A store produced by [can-fixture.store]. |
@property {Number} can-fixture.delay delay | ||
@parent can-fixture.properties | ||
@description Sets the delay until a response is fired. | ||
@signature `fixture.delay` | ||
Sets the delay until a response is fired in milliseconds. | ||
Sets the delay until a response is fired in milliseconds. | ||
```js | ||
fixture.delay = 1000; // 1 second delay | ||
``` | ||
```js | ||
import {QueryLogic, fixture, ajax} from "can"; | ||
const todoQueryLogic = new QueryLogic( | ||
{identity: ["id"]} | ||
); | ||
const todoStore = fixture.store( [ | ||
{ id: 1, name: "Do the dishes" }, | ||
{ id: 2, name: "Walk the dog" } | ||
], todoQueryLogic ); | ||
fixture( "/api/todos/{id}", todoStore ); // can also be written fixture("/api/todos", todoStore); | ||
fixture.delay = 3000; | ||
ajax( {url: "/api/todos/1"} ).then(result => { | ||
clearInterval(timer); | ||
console.log(result); //-> { id: 1, name: "Do the dishes" }, | ||
}); | ||
// logs seconds passed | ||
let i = 0; | ||
const timer = setInterval(() => { | ||
console.log(++i + " second(s) passed"); | ||
}, 1000); | ||
``` | ||
@codepen | ||
@highlight 14,only |
@property {Array} can-fixture.fixtures fixtures | ||
@parent can-fixture.properties | ||
@hide | ||
@@ -6,2 +7,16 @@ | ||
The list of currently active fixtures. | ||
The list of currently active fixtures. | ||
```js | ||
import {fixture} from "can"; | ||
fixture( "GET /todos", {} ); | ||
console.log( fixture.fixtures ); //-> [{ | ||
// fixture() { return data }; | ||
// type: "GET", | ||
// url: "/todos", | ||
// }] | ||
``` | ||
@codepen | ||
@property {Boolean} can-fixture.on on | ||
@parent can-fixture.properties | ||
@description Turns all fixtures on or off. Defaults to `true` for on. | ||
@signature `fixture.on` | ||
Turns the fixtures on or off. Defaults to `true` for on. | ||
Turns all fixtures on or off. Defaults to `true` for on. | ||
```js | ||
import {fixture, ajax} from "can"; | ||
fixture( "GET /todos", () => { | ||
return "success"; | ||
} ); | ||
fixture.on = false; //-> AJAX requests will not be trapped | ||
ajax( {url: "/todos"} ).catch( error => { | ||
console.log("Couldn't connect."); | ||
} ); | ||
``` | ||
@codepen | ||
@highlight 7 | ||
@body | ||
## Alternatives | ||
To remove a fixture you can also use `fixture(ajaxSetting, null)`. This method is elaborated further in [can-fixture]. | ||
```js | ||
fixture.on = false; //-> AJAX requests will not be trapped | ||
import {fixture, ajax} from "can"; | ||
fixture( "GET /todos", () => { | ||
return "success"; | ||
} ); | ||
fixture( "GET /todos", null ); | ||
ajax( {url:"/todos"} ).catch( error => { | ||
console.log("Couldn't connect."); | ||
} ); | ||
``` | ||
To remove a fixture you can also use `fixture(ajaxSetting, null)`. | ||
@codepen | ||
@highlight 7 |
@function can-fixture.rand rand | ||
@parent can-fixture.properties | ||
@description Returns a random integer. | ||
@signature `fixture.rand(min, max)` | ||
Returns a random integer in the range [min, max]. If only one argument is provided, | ||
returns a random integer from [0, max]. | ||
Returns a random integer in the range [`min`, `max`]. If only one argument is provided, | ||
returns a random integer from [`0`, `max`]. | ||
```js | ||
fixture.rand( 1, 10 ); //-> Random number between 1 and 10 inclusive. | ||
fixture.rand( 10 ); //-> Random number between 0 and 10 inclusive. | ||
``` | ||
```js | ||
import {fixture} from "can"; | ||
console.log( fixture.rand( 1, 10 ) ); //-> Random number between 1 and 10 inclusive. | ||
console.log( fixture.rand( 10 ) ); //-> Random number between 0 and 10 inclusive. | ||
``` | ||
@codepen | ||
@param {Number} [min] The lower limit of values that will be returned. | ||
@param {Number} max The upper limit of values that will be returned. `max` is valid return value. | ||
@return {Number} A number inclusive between `min` and `max`. | ||
@signature `fixture.rand(choices, min, max)` | ||
An array of between min and max random items from choices. If only `min` is | ||
provided, `max` will equal `min`. If both `max` and `min` are not provided, | ||
`min` will be 1 and `max` will be `choices.length`. | ||
An array of between min and max random items from choices. If only `min` is | ||
provided, `max` will equal `min`. If both `max` and `min` are not provided, | ||
`min` will be 1 and `max` will be `choices.length`. | ||
```js | ||
// pick a random number of items from an array | ||
fixture.rand( [ "a", "b", "c" ] ); //-> ["c"] | ||
fixture.rand( [ "a", "b", "c" ] ); //-> ["b","a"] | ||
```js | ||
import {fixture} from "can"; | ||
// pick one item from an array | ||
fixture.rand( [ "a", "b", "c" ], 1 ); //-> ["c"] | ||
// pick a random number of items from an array | ||
console.log( fixture.rand( [ "a", "b", "c" ] ) ); //-> ["c"] | ||
// get one item from an array | ||
fixture.rand( [ "a", "b", "c" ], 1 )[ 0 ]; //-> "b" | ||
// pick one item from an array | ||
console.log( fixture.rand( [ "a", "b", "c" ], 1 ) ); //-> ["c"] | ||
// get 2 or 3 items from the array | ||
fixture.rand( [ "a", "b", "c" ], 2, 3 ); //-> ["c","a","b"] | ||
``` | ||
// get one item from an array | ||
console.log( fixture.rand( [ "a", "b", "c" ], 1 )[ 0 ] ); //-> "b" | ||
// get 2 or 3 items from the array | ||
console.log( fixture.rand( [ "a", "b", "c" ], 2, 3 ) ); //-> ["c","a","b"] | ||
``` | ||
@codepen | ||
@param {Array} choices An array of values to chose from. The returned array will only include a value once. | ||
@param {Number} [min] The minimum number of items to be in the returned array. | ||
@param {Number} [max] The maximum number of items in the returned array. | ||
@return {Array} an array between `min` and `max` random items from the choices array. |
@function can-fixture.store store | ||
@parent can-fixture.properties | ||
@description Creates a store. | ||
@signature `fixture.store(baseItems, queryLogic)` | ||
Create a store that starts with `baseItems` for a service layer | ||
described by `queryLogic`. | ||
Create a store that starts with `baseItems` for a service layer | ||
described by `queryLogic`. | ||
```js | ||
const Todo = DefineMap.extend({ | ||
id: {identity: true, type: "number"}, | ||
completed: "boolean" | ||
}) | ||
```js | ||
import {DefineMap, QueryLogic, fixture, ajax} from "can"; | ||
import {Todo} from "https://unpkg.com/can-demo-models@5"; | ||
// Describe the services parameters: | ||
const todoQueryLogic = new QueryLogic(Todo); | ||
// Describe the services parameters: | ||
const todoQueryLogic = new QueryLogic(Todo); | ||
// Create a store with initial data. | ||
// Pass [] if you want it to be empty. | ||
const todoStore = fixture.store( [ | ||
{ | ||
_id: 1, | ||
name: "Do the dishes", | ||
complete: true | ||
}, { | ||
_id: 2, | ||
name: "Walk the dog", | ||
complete: false | ||
} ], | ||
todoQueryLogic ); | ||
// Create a store with initial data. | ||
// Pass an empty Array (ex: []) if you want it to be empty. | ||
const todoStore = fixture.store( [ | ||
{ | ||
id: 1, | ||
name: "Do the dishes", | ||
complete: true | ||
}, { | ||
id: 2, | ||
name: "Walk the dog", | ||
complete: false | ||
} | ||
], todoQueryLogic ); | ||
// Hookup urls to the store: | ||
fixture( "/todos/{_id}", todoStore ); | ||
``` | ||
// Hookup urls to the store: | ||
fixture( "/todos/{id}", todoStore ); | ||
ajax( {url: "/todos/1"} ).then( result => { | ||
console.log( result ); | ||
} ); | ||
``` | ||
@codepen | ||
@highlight 9-19 | ||
@param {Array} baseItems An array of items that will populate the store. | ||
@@ -41,29 +48,38 @@ @param {can-query-logic} QueryLogic A description of the service layer's parameters. | ||
@signature `fixture.store(count, makeItems, queryLogic)` | ||
Similar to `fixture.store(baseItems, queryLogic)`, except that | ||
it uses `makeItems` to create `count` entries in the store. | ||
Similar to `fixture.store(baseItems, queryLogic)`, except that | ||
it uses `makeItems` to create `count` entries in the store. | ||
```js | ||
// Describe the services parameters: | ||
const todoQueryLogic = new QueryLogic( /* ... */ ); | ||
```js | ||
import {DefineMap, QueryLogic, fixture, ajax} from "can"; | ||
import {Todo} from "https://unpkg.com/can-demo-models@5"; | ||
import "//unpkg.com/jquery@3.3.1/dist/jquery.js"; | ||
// Create a store with initial data. | ||
// Pass [] if you want it to be empty. | ||
const todoStore = fixture.store( | ||
1000, | ||
function( i ) { | ||
return { | ||
_id: i + 1, | ||
name: "Todo " + i, | ||
complete: fixture.rand( [ true, false ], 1 )[ 0 ] | ||
}; | ||
}, | ||
todoQueryLogic ); | ||
// Describe the services parameters: | ||
const todoQueryLogic = new QueryLogic(Todo); | ||
// Hookup urls to the store: | ||
fixture( "/todos/{_id}", todoStore ); | ||
``` | ||
@param {Number} count TODO describe | ||
// Create a store with initial data. | ||
const todoStore = fixture.store( | ||
1000, | ||
( i ) => ( { | ||
id: i + 1, | ||
name: "Todo " + i, | ||
complete: fixture.rand( [ true, false ], 1 )[ 0 ] | ||
} ), | ||
todoQueryLogic | ||
); | ||
// Hookup urls to the store: | ||
fixture( "/todos/{id}", todoStore ); | ||
ajax( {url: "/todos/3"} ).then( result => { | ||
console.log( result ); //-> "{'_id':3,'name':'Todo 2','complete':true||false}" | ||
} ); | ||
``` | ||
@codepen | ||
@highlight 9-17 | ||
@param {Number} count The number of `baseItems` to create. | ||
@param {function} makeItems A function that will generate `baseItems` | ||
@@ -70,0 +86,0 @@ @param {can-query-logic} queryLogic A description of the service layer's parameters. |
@typedef {Object} can-fixture/types/ajaxSettings ajaxSettings | ||
@parent can-fixture.types | ||
An object used to match incoming [can-fixture/types/request] objects. | ||
@description An object used to match incoming [can-fixture/types/request] objects. | ||
@type {Object} | ||
This object is used to match values on [can-fixture/types/request] objects. | ||
If there's a match, the fixture handler provided with the | ||
[can-fixture/types/ajaxSettings] will be invoked. | ||
This object is used to match values on [can-fixture/types/request] objects. | ||
If there's a match, the fixture handler provided with the | ||
[can-fixture/types/ajaxSettings] will be invoked. | ||
If a property on an `ajaxSettings` is not provided, all request values | ||
will be matched for that property. | ||
If a property on an `ajaxSettings` is not provided, all request values | ||
will be matched for that property. | ||
For example, | ||
you can match all `GET` requests, no matter what `url` is passed like: | ||
For example, | ||
you can match all `GET` requests, no matter what `url` is passed like: | ||
``` | ||
fixture({method: "GET"}, function(){ ... }); | ||
``` | ||
```js | ||
import {fixture, ajax} from "can"; | ||
fixture({method: "GET"}, () => { | ||
return "success"; | ||
}); | ||
@option {String} url The requested url with anything after the querystring taken off in `GET` and `DESTROY` method requests. For example, you can't match: | ||
// randomly selects '/example', '/canJS', or '/random'. | ||
const url = fixture.rand(["/example", "/canJS", "/random"], 1); | ||
ajax( {url} ).then(result => { | ||
console.log( result ); //-> "success" | ||
}); | ||
``` | ||
@codepen | ||
@highlight 3-5 | ||
@option {String} url The requested url with anything after the querystring taken off in `GET` and `DESTROY` method requests. For example, you can't match: | ||
```js | ||
fixture({method: "GET", url: "/things?name=Justin"}); | ||
@@ -31,11 +43,35 @@ ``` | ||
```js | ||
import {fixture, ajax} from "can"; | ||
fixture({method: "GET", url: "/things", data: {name: "Justin"}}, () => { | ||
return "success"; | ||
} ); | ||
// can also be: `$.get("/things", {name: "Justin"})` | ||
// attempting to just get from the endpoint "/things" won't work. | ||
ajax( {url: "/things?name=Justin"} ).then( result => { | ||
console.log( result ); //-> "success" | ||
} ); | ||
``` | ||
fixture({method: "GET", url: "/things", data: {name: "Justin"}}); | ||
``` | ||
@codepen | ||
@highlight 3-5 | ||
The `url` can have templates like: | ||
```js | ||
import {fixture, ajax} from "can"; | ||
import "//unpkg.com/jquery@3.3.1/dist/jquery.js"; | ||
fixture({method: "GET", url: "/things/{id}"}, () => { | ||
return "success"; | ||
} ); | ||
// attempting to just get from the endpoint "/things" won't work. | ||
ajax( {url: "/things/1"} ).then( result => { | ||
console.log( result ); //-> "success" | ||
} ); | ||
``` | ||
fixture({method: "GET", url: "/things/{id}"}) | ||
``` | ||
@codepen | ||
@highlight 4-6 | ||
@@ -42,0 +78,0 @@ The templated values get added to the [can-fixture/types/request] object's `data`. |
@typedef {Object} can-fixture/types/request request | ||
@parent can-fixture.types | ||
An object with easily digestible values derived from the mock XHR | ||
object. | ||
@description An object with easily digestible values derived from the mock XHR object. | ||
@type {Object} | ||
This object is passed to a [can-fixture.requestHandler] | ||
and can be used to determine the response. | ||
This object is passed to a [can-fixture.requestHandler] | ||
and can be used to determine the response. | ||
```js | ||
fixture( "GET /todos/{id}", function( request, response ) { | ||
request.url; //-> "todos/5" | ||
request.method; //-> "get" | ||
request.data; //-> {id: "5", include: ["owner"]} | ||
request.headers; //-> {} | ||
request.async; //-> false | ||
} ); | ||
```js | ||
import {fixture, ajax} from "can"; | ||
$.get( "/todos/5?include[]=owner" ); | ||
``` | ||
fixture( "GET /todos/{id}", ( request, response ) => { | ||
console.log( request.url ); //-> "todos/5" | ||
console.log( request.method ); //-> "get" | ||
console.log( request.data ); //-> {id: "5", include: ["owner"]} | ||
console.log( request.headers ); //-> {Accept: "*/*", X-Requested-With: "XMLHttpRequest"} | ||
console.log( request.async ); //-> false | ||
return; | ||
} ); | ||
ajax( {url: "/todos/5?include[]=owner"} ); | ||
``` | ||
@codepen | ||
@option {String} url The requested url with anything after the querystring taken off in `GET` and `DESTROY` method requests. | ||
@@ -25,0 +28,0 @@ @option {String} method The method of the request. Ex: `GET`, `PUT`, `POST`, etc. |
@typedef {function(can-fixture/types/request,can-fixture.response,Object,Object)} can-fixture.requestHandler(request,response,requestHeaders,ajaxSettings) requestHandler | ||
@parent can-fixture.types | ||
@description Defines the XHR response for a given trapped request. | ||
@signature `requestHandler(request, response(...), requestHeaders, ajaxSettings)` | ||
Defines the XHR response for a given trapped request. | ||
Defines the XHR response for a given trapped request. | ||
```js | ||
fixture( { method: "get", url: "/todos" }, | ||
function( request, response, headers, ajaxSettings ) { | ||
request; //-> { | ||
// method: "get", | ||
// url: "/todos", | ||
// data: {complete: true} | ||
// } | ||
```js | ||
import {fixture, ajax} from "can"; | ||
} | ||
); | ||
fixture( { method: "get", url: "/todos" }, | ||
( request, response, headers, ajaxSettings ) => { | ||
console.log( request.method ); //-> "get" | ||
console.log( request.url ); //-> "/todos" | ||
console.log( request.data ); //-> {complete: "true"} | ||
} | ||
); | ||
$.ajax( { method: "get", url: "/todos?complete=true" } ); | ||
``` | ||
ajax( {url: "/todos?complete=true"} ); | ||
``` | ||
@codepen | ||
Templated `url` data will be added to the `requestHandler`'s `request` argument's `data` property: | ||
Templated `url` data will be added to the `requestHandler`'s `request` argument's `data` property: | ||
```js | ||
fixture( { url: "/todos/{action}" }, | ||
function( request, response, headers, ajaxSettings ) { | ||
request; //-> { | ||
// method: "post", | ||
// url: "/todos", | ||
// data: {action: delete} | ||
// } | ||
} | ||
); | ||
```js | ||
import {fixture, ajax} from "can"; | ||
$.post( "/todos/delete" ); | ||
``` | ||
fixture( { url: "/todos/{action}" }, | ||
( request, response, headers, ajaxSettings ) => { | ||
console.log( request.method ); //-> "post" | ||
console.log( request.url ); //-> "/todos/delete" | ||
console.log( request.data ); //-> {action: "delete"} | ||
} | ||
); | ||
ajax( {type: "POST", url:"/todos/delete"} ); | ||
``` | ||
@codepen | ||
@param {can-fixture/types/request} request Information about the request. The request's data property will contain data from the request's querystring or request body. Also | ||
@@ -39,0 +43,0 @@ any templated values in the [can-fixture/types/ajaxSettings]'s `url` will be added. |
@typedef {function} can-fixture.response response | ||
@parent can-fixture.types | ||
@signature `response(status, body, headers, statusText)` | ||
@description Used to detail a response. | ||
Used to detail a response. | ||
@signature `response( [status], body, [headers], [statusText] )` | ||
Example: | ||
Using the response function to return an unauthorized error. | ||
```js | ||
fixture( { url: "/todos/{action}" }, | ||
function( request, response, headers, ajaxSettings ) { | ||
response( | ||
401, | ||
{ message: "Unauthorized" }, | ||
{ "WWW-Authenticate": "Basic realm=\"myRealm\"" }, | ||
"unauthorized" ); | ||
} | ||
); | ||
```js | ||
import {fixture, ajax} from "can"; | ||
$.post( "/todos/delete" ); | ||
``` | ||
fixture( {url: "/todos/{action}"}, | ||
( request, response, headers, ajaxSettings ) => { | ||
response( | ||
401, | ||
{ message: "Unauthorized" }, | ||
{ "WWW-Authenticate": "Basic realm=\"myRealm\"" }, | ||
"unauthorized" | ||
); | ||
} | ||
); | ||
You don't have to provide every argument to `response`. It can be called like: | ||
// fixture response log { | ||
// headers: {WWW-Authenticate: "Basic realm='myRealm'"}, | ||
// responseBody: {message: "Unauthorized"}, | ||
// statusCode: 401, | ||
// statusText: "unauthorized" | ||
// } | ||
```js | ||
// Just body | ||
response( { message: "Hello World" } ); | ||
ajax( {type:"POST", url: "/todos/delete"} ).catch( error => { | ||
console.log(error); //-> {message: "Unauthorized"} | ||
} ); | ||
``` | ||
@codepen | ||
// status and body | ||
response( 401, { message: "Unauthorized" } ); | ||
The default `statusText` will be `"ok"` for statuses between `200` and `300` including `200`, and `304`. It will `"error"` for everything else. | ||
// body and headers | ||
response( "{\"message\":\"Unauthorized\"}", { "WWW-Authenticate": "Basic realm=\"myRealm\"" } ); | ||
@param {Number} status The [HTTP response code](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html). Ex: `200`. | ||
@param {Object|String} body A JavaScript object, or a string that will be serialized and set as the responseText of the XHR object, or the raw string text that will be set as the responseText of the XHR object. | ||
@param {Object} headers An object of [HTTP response headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers) and values. | ||
@param {String} statusText The status text of the response. Ex: ``"ok"`` for 200. | ||
// status, body statusText | ||
response( 401, "{\"message\":\"Unauthorized\"}", "unauthorized" ); | ||
``` | ||
@signature `response( body )` | ||
The default `statusText` will be `ok` for `200 <= status < 300, status === 304` and `error` | ||
for everything else. | ||
Using the body object to return a single message. | ||
The status will be `200` and statusText will be `"ok"`. | ||
```js | ||
import {fixture, ajax} from "can"; | ||
fixture( {url: "/todos"}, ( request, response ) => { | ||
// Just body | ||
response( { message: "Hello World" } ); | ||
}); | ||
ajax( {url: "/todos"} ).then( result => { | ||
console.log( result ); //-> {message: "Hello World"} | ||
} ); | ||
``` | ||
@codepen | ||
@highlight 5 | ||
@param {Object|String} body A JavaScript object, or a string that will be serialized and set as the responseText of the XHR object, or the raw string text that will be set as the responseText of the XHR object. | ||
@signature `response( [status], body )` | ||
Response will always return a `401` status. The statusText will be `"error"`. | ||
```js | ||
import {fixture, ajax} from "can"; | ||
fixture( {url: "/todos"}, ( request, response ) => { | ||
// status and body | ||
response( 401, { message: "Unauthorized" } ); | ||
}); | ||
ajax( {url:"/todos"} ).catch( error => { | ||
console.log( error ); //-> 401, {message: "Unauthorized"} | ||
}); | ||
``` | ||
@codepen | ||
@highlight 5 | ||
@param {Number} status The [HTTP response code](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html). Ex: `200`. | ||
@param {Object} body A JS object that will be serialized and set as the responseText of the XHR object, or | ||
the raw string text that will be set as the responseText of the XHR object. | ||
@param {Object} headers An object of HTTP response headers and values. | ||
@param {Object|String} body A JavaScript object, or a string that will be serialized and set as the responseText of the XHR object, or the raw string text that will be set as the responseText of the XHR object. | ||
@signature `response( body, [headers] )` | ||
Response will return the responseBody with custom headers. | ||
Because it's not set the `status` will default to `200`, and the `statusText` to `"ok"`. | ||
```js | ||
import {fixture, ajax} from "can"; | ||
fixture( {url: "/todos"}, ( request, response ) => { | ||
// body and headers | ||
response( | ||
"{\"message\":\"Unauthorized\"}", | ||
{ "WWW-Authenticate": "Basic realm=\"myRealm\"" } | ||
); | ||
}); | ||
ajax( {url: "/todos"} ).then( result => { | ||
console.log( result ); //-> {message: "Unauthorized"} | ||
} ); | ||
``` | ||
@codepen | ||
@highlight 5-8 | ||
@param {Object|String} body A JavaScript object, or a string that will be serialized and set as the responseText of the XHR object, or the raw string text that will be set as the responseText of the XHR object. | ||
@param {Object} headers An object of [HTTP response headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers) and values. | ||
@signature `response( [status], body, [statusText] )` | ||
Response will return with the given `status` and `statusText` along with the `responseBody`. | ||
```js | ||
import {fixture, ajax} from "can"; | ||
fixture( {url: "/todos"}, ( request, response ) => { | ||
// status, body, statusText | ||
response( 401, "{\"message\":\"Unauthorized\"}", "unauthorized" ); | ||
}); | ||
ajax( {url: "/todos"} ).catch( error => { | ||
console.log(error); //-> {message: "Unauthorized"} | ||
} ); | ||
``` | ||
@codepen | ||
@highlight 5 | ||
@param {Number} status The [HTTP response code](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html). Ex: `200`. | ||
@param {Object|String} body A JavaScript object, or a string that will be serialized and set as the responseText of the XHR object, or the raw string text that will be set as the responseText of the XHR object. | ||
@param {String} statusText The status text of the response. Ex: ``"ok"`` for 200. |
@function can-fixture/StoreType.prototype.createData createData | ||
@parent can-fixture/StoreType.prototype | ||
@signature `Store.prototype.createData(request, response)` | ||
@description Creates records in the store. It can serve as a [can-fixture.requestHandler requestHandler]. | ||
A `requestHandler` that creates an item in the store. | ||
@signature `Store.createData(request, response)` | ||
```js | ||
fixture( "POST /api/todos", todoStore.createData ); | ||
``` | ||
Creates records in the store. It can serve as a `requestHandler`. The example will store the the object in the data parameter in the store. | ||
```js | ||
import {QueryLogic, fixture, ajax} from "can"; | ||
const todoStore = fixture.store( | ||
[], | ||
new QueryLogic({ identity: ["id"] }) | ||
); | ||
fixture( "POST /todos", (req, res) => { | ||
todoStore.createData(req, res); | ||
} ); | ||
const ajaxSettings = { | ||
url: "/todos", | ||
type: "POST", | ||
data: {name:"Write examples!"} | ||
}; | ||
ajax(ajaxSettings).then(result => { | ||
console.log(result); //-> {id: 1, name: "Write examples!"} | ||
}); | ||
``` | ||
@codepen | ||
@param {object} request An HTTP Request object | ||
@param {object} response An HTTP response object. | ||
@function can-fixture/StoreType.prototype.createInstance createInstance | ||
@parent can-fixture/StoreType.prototype | ||
@signature `Store.prototype.createInstance(record)` | ||
@description Create an instance in the fixture store programmatically. | ||
Create an instance in the fixture store programatically. This is usually | ||
used to make sure a record exists when simulating real-time services. | ||
@signature `Store.createInstance( record )` | ||
```js | ||
var store = fixture.store([ | ||
Create an instance in the fixture store programmatically. This is usually | ||
used to make sure a record exists when simulating real-time services. | ||
```js | ||
import {fixture, QueryLogic} from "can"; | ||
const store = fixture.store([ | ||
{id: 0, name: "foo"} | ||
], new QueryLogic({identity: ["id"]})); | ||
], new QueryLogic({identity: ["id"]})); | ||
// In a test, make sure the store has the same data you are going | ||
// to "push" to the client: | ||
store.createInstance({name: "lawn"}).then(function(record){ | ||
connection.createInstance(record) | ||
}); | ||
``` | ||
// In a test, make sure the store has the same data you are going | ||
// to "push" to the client: | ||
store.createInstance( {name: "lawn"} ).then( (record) => { | ||
console.log(record); //-> {name: "lawn", id: 1} | ||
} ); | ||
``` | ||
@codepen | ||
@param {Object} record The record being added to the store. |
@function can-fixture/StoreType.prototype.destroyData destroyData | ||
@parent can-fixture/StoreType.prototype | ||
@signature `Store.prototype.destroyData(request, response)` | ||
@description A [can-fixture.requestHandler requestHandler] that removes a record from the store. | ||
A `requestHandler` that removes an item from the store. | ||
@signature `Store.destroyData(request, response)` | ||
```js | ||
fixture( "DELETE /api/todos/{_id}", todoStore.destroyData ); | ||
``` | ||
A `requestHandler` that removes a record from the store. | ||
```js | ||
import {QueryLogic, fixture, ajax} from "//unpkg.com/can@5/core.mjs"; | ||
import {Todo} from "https://unpkg.com/can-demo-models@5"; | ||
const todoStore = fixture.store( [ | ||
{id: 1, name: "Do the dishes"}, | ||
{id: 2, name: "Walk the dog"} | ||
], new QueryLogic(Todo) ); | ||
fixture("/todos", todoStore) | ||
fixture( "DELETE /todos/{id}", (req, res) => { | ||
todoStore.destroyData(req, res); | ||
} ); | ||
ajax( {url: "/todos/1", type: "DELETE"} ).then( result => { | ||
console.log(result); //-> {id: 1, name: "Do the dishes"} | ||
} ); | ||
ajax( {type: "GET", url: "/todos"} ).then( value => { | ||
console.log( value.data ); //-> [ {id: 2, name: "Walk the dog"} ] | ||
} ); | ||
``` | ||
@codepen | ||
@param {object} request An HTTP Request object | ||
@param {object} response An HTTP response object. |
@function can-fixture/StoreType.prototype.destroyInstance destroyInstance | ||
@parent can-fixture/StoreType.prototype | ||
@signature `Store.prototype.destroyInstance(request, response)` | ||
@description Destroy an instance in the fixture store programmatically. | ||
Destroy an instance in the fixture store programatically. This is usually | ||
used to make sure a record exists in the store when simulating real-time services. | ||
@signature `Store.destroyInstance( param )` | ||
```js | ||
var store = fixture.store([ | ||
Destroy an instance in the fixture store programmatically. This is usually | ||
used to make sure a record exists in the store when simulating real-time services. | ||
```js | ||
import {QueryLogic, fixture} from "can"; | ||
const store = fixture.store([ | ||
{id: 0, name: "foo"} | ||
], new QueryLogic({identity: ["id"]})); | ||
], new QueryLogic({identity: ["id"]})); | ||
// In a test, make sure the store has destroyed the same data that | ||
// the client is being told has been destroyed. | ||
store.destroyInstance({id: 0}).then(function(record){ | ||
connection.destroyInstance(record) | ||
}); | ||
``` | ||
// In a test, make sure the store has destroyed the same data that | ||
// the client is being told has been destroyed. | ||
store.destroyInstance( {id: 0} ).then( (record) => { | ||
console.log(record) //-> {id: 0, name: "foo"} | ||
} ); | ||
``` | ||
@codepen | ||
@param {object} param An object containing a [can-query-logic QueryLogic] schema identity of the store. |
@function can-fixture/StoreType.prototype.get get | ||
@parent can-fixture/StoreType.prototype | ||
@signature `Store.prototype.get(params)` | ||
@description Returns a single record's data from the store. | ||
Returns a single item's data from the store. | ||
@signature `Store.get( params )` | ||
```js | ||
todoStore.get( { id: 1 } ); //-> {id: 1, name: "dishes"} | ||
``` | ||
Returns a single record's data from the store. | ||
```js | ||
import {QueryLogic, fixture} from "can"; | ||
const todoStore = fixture.store( [ | ||
{id: 1, name: "Do the dishes"}, | ||
{id: 2, name: "Walk the dog"} | ||
], new QueryLogic({identity: ["id"]}) ); | ||
const result = todoStore.get( {id: 1} ); | ||
console.log( result ); //-> {id: 1, name: "Do the dishes"} | ||
``` | ||
@codepen | ||
@param {Object} params An object containing a [can-query-logic QueryLogic] schema identity of the store. | ||
@return {Object} The first record that matches the params. |
@function can-fixture/StoreType.prototype.getData getData | ||
@parent can-fixture/StoreType.prototype | ||
@signature `Store.prototype.getData(request, response)` | ||
@description A [can-fixture.requestHandler requestHandler] that gets a single record from the store. | ||
A `requestHandler` that gets a single item from the store. | ||
@signature `Store.getData(request, response)` | ||
```js | ||
fixture( "GET /api/todos/{_id}", todoStore.getData ); | ||
``` | ||
A `requestHandler` that gets a single record from the store. | ||
```js | ||
import {QueryLogic, fixture, ajax} from "can"; | ||
import {Todo} from "//unpkg.com/can-demo-models@5"; | ||
const todoStore = fixture.store( [ | ||
{id: 1, name: "Do the dishes"}, | ||
{id: 2, name: "Walk the dog"} | ||
], new QueryLogic(Todo) ); | ||
fixture( "GET /todos/{id}", (req, res) => { | ||
todoStore.getData(req, res); | ||
} ); | ||
ajax( {url: "/todos/1", type: "GET"} ).then( value => { | ||
console.log( value ); //-> {id:1, name:"Do the dishes"} | ||
}); | ||
``` | ||
@codepen | ||
@param {object} request An HTTP Request object | ||
@param {object} response An HTTP response object. |
@function can-fixture/StoreType.prototype.getList getList | ||
@parent can-fixture/StoreType.prototype | ||
@signature `Store.prototype.getList(set)` | ||
@description Returns the matching items from the store | ||
Returns the matching items from the store like: `{data: [...]}`. | ||
@signature `Store.getList( set )` | ||
```js | ||
todoStore.get( { name: "dishes" } ); //-> {data: [{id: 1, name: "dishes"}]} | ||
``` | ||
Returns the matching items from the store like: `{filter: [...]}`. | ||
```js | ||
import {QueryLogic, fixture} from "can"; | ||
import {Todo} from "//unpkg.com/can-demo-models@5"; | ||
const todoQueryLogic = new QueryLogic( Todo ); | ||
const todoStore = fixture.store( [ | ||
{id: 1, name: "Do the dishes", complete: true}, | ||
{id: 2, name: "Walk the dog", complete: false}, | ||
{id: 3, name: "dry the dishes", complete: false}, | ||
], todoQueryLogic ); | ||
const result = todoStore.getList( {filter: {complete: false}} ); | ||
console.log( result.data ); //-> [ | ||
// {id: 2, name: "Walk the dog", complete: false}, | ||
// {id: 3, name: "dry the dishes", complete: false} | ||
// ] | ||
``` | ||
@codepen | ||
@param {Object} set A [can-query-logic/query Query]. | ||
@return {Object} A serialized object. The `data` parameter contains a list of records from the store. |
@function can-fixture/StoreType.prototype.getListData getListData | ||
@parent can-fixture/StoreType.prototype | ||
@signature `Store.prototype.getListData(request, response)` | ||
@description A `requestHandler` that gets multiple items from the store. | ||
A `requestHandler` that gets multiple items from the store. | ||
@signature `Store.getListData(request, response)` | ||
```js | ||
fixture( "GET /api/todos", todoStore.getListData ); | ||
``` | ||
A `requestHandler` that gets multiple items from the store. | ||
```js | ||
import {QueryLogic, fixture, ajax} from "can"; | ||
const todoStore = fixture.store( [ | ||
{id: 1, name: "Do the dishes", complete: true}, | ||
{id: 2, name: "Walk the dog", complete: true}, | ||
{id: 3, name: "Write docs", complete: false} | ||
], new QueryLogic() ); | ||
fixture( "GET /todos", (req, res) => { | ||
todoStore.getListData(req, res); | ||
} ); | ||
const ajaxOptions = { | ||
url: "/todos", | ||
data: { filter: {complete: true} } | ||
} | ||
ajax( ajaxOptions ).then( value => { | ||
console.log( value.data ); //-> [ | ||
// {id:1, name:"Do the dishes", complete: true}, | ||
// {id:2, name:"Walk the dog", complete: true} | ||
// ] | ||
}); | ||
``` | ||
@codepen | ||
@param {object} request An HTTP Request object | ||
@param {object} response An HTTP response object. |
@function can-fixture/StoreType.prototype.reset reset | ||
@parent can-fixture/StoreType.prototype | ||
@signature `Store.prototype.reset([baseItems])` | ||
@description Sets the items in the store to their original state. | ||
Sets the items in the store to their original state or to `baseItems` if it's passed as an argument. | ||
@signature `Store.reset( [baseItems] )` | ||
```js | ||
// Creates a store with one item. | ||
const todoStore = fixture.store( | ||
[ { id: 1, name: "dishes" } ], | ||
new QueryLogic({identity: ["id"]}) ); | ||
fixture( "/todos/{id}", todoStore ); | ||
todoStore.getList( {} ).length; //-> 1 | ||
Sets the items in the store to their original state or to `baseItems` if it's passed as an argument. | ||
// delete that item | ||
$.ajax( { url: "todos/1", method: "delete" } ).then( function() { | ||
return todoStore.getList( {} ).length; //-> 0 | ||
} ).then( function() { | ||
```js | ||
import {QueryLogic, fixture, ajax} from "can"; | ||
// calling reset adds it back | ||
todoStore.reset(); | ||
todoStore.getList( {} ).length; //-> 1 | ||
} ); | ||
``` | ||
// Creates a store with one item. | ||
const todoStore = fixture.store( | ||
[ {id: 1, name: "dishes"} ], | ||
new QueryLogic({identity: ["id"]}) | ||
); | ||
fixture( "/todos/{id}", todoStore ); | ||
console.log( todoStore.getList( {} ).count ); //-> 1 | ||
// delete that item | ||
ajax( { url: "/todos/1", type: "DELETE" } ) | ||
.then( () => { | ||
console.log( todoStore.getList( {} ).count ); //-> 0 | ||
} ) | ||
.then( () => { | ||
// calling reset adds it back | ||
todoStore.reset(); | ||
console.log( todoStore.getList( {} ).count ); //-> 1 | ||
} ); | ||
``` | ||
@codepen | ||
@param {Array} baseItems If provided, adds these items to the store. | ||
This can be useful for setting up particular testing scenarios. |
@function can-fixture/StoreType.prototype.updateData updateData | ||
@parent can-fixture/StoreType.prototype | ||
@signature `Store.prototype.updateData(request, response)` | ||
@description A [can-fixture.requestHandler requestHandler] that updates a record in the store. | ||
A `requestHandler` that updates an item in the store. | ||
@signature `Store.updateData(request, response)` | ||
```js | ||
fixture( "PUT /api/todos/{_id}", todoStore.updateData ); | ||
``` | ||
A `requestHandler` that updates an item in the store. | ||
```js | ||
import {QueryLogic, fixture, ajax} from "can"; | ||
import {Todo} from "https://unpkg.com/can-demo-models@5"; | ||
const todoStore = fixture.store( [ | ||
{id: 1, name: "Do the dishes"}, | ||
{id: 2, name: "Walk the dog"} | ||
], new QueryLogic(Todo) ); | ||
fixture( "/todos/{id}", todoStore); | ||
fixture( "PUT /todos/{id}", (req, res) => { | ||
todoStore.updateData(req, res); | ||
} ); | ||
const ajaxSettings = { | ||
url: "/todos/1", | ||
type: "PUT", | ||
data: {name: "test"}, | ||
}; | ||
ajax(ajaxSettings).then( () => { | ||
ajax( {url: "/todos/1", type: "GET"} ).then( value => { | ||
console.log(value); //-> {name:"test", id:1} | ||
}); | ||
}); | ||
``` | ||
@codepen | ||
@param {object} request An HTTP Request object | ||
@param {object} response An HTTP response object. |
@function can-fixture/StoreType.prototype.updateInstance updateInstance | ||
@parent can-fixture/StoreType.prototype | ||
@signature `Store.prototype.updateInstance(request, response)` | ||
@description Destroy an instance in the fixture store programmatically. | ||
Destroy an instance in the fixture store programatically. This is usually | ||
used to make sure a record exists in the store when simulating real-time services. | ||
@signature `Store.updateInstance( params )` | ||
```js | ||
var store = fixture.store([ | ||
Update an instance in the fixture store programmatically. | ||
```js | ||
import {QueryLogic, fixture} from "can"; | ||
const store = fixture.store([ | ||
{id: 0, name: "dishes"} | ||
], new QueryLogic({identity: ["id"]})); | ||
], new QueryLogic({identity: ["id"]})); | ||
// In a test, make sure the store has updated the same data that | ||
// the client is being told has been updated. | ||
store.updateInstance({id: 0, name: "do the dishes"}).then(function(record){ | ||
connection.updateInstance(record) | ||
}); | ||
``` | ||
// In a test, make sure the store has updated the same data that | ||
// the client is being told has been updated. | ||
store.updateInstance( {id: 0, name: "do the dishes"} ).then(record => { | ||
console.log( record ); //-> {id: 0, name: "do the dishes"} | ||
}); | ||
``` | ||
@codepen | ||
@param {Object} params A matching identity and properties to be updated. |
@@ -7,2 +7,3 @@ @function can-fixture/StoreType Store | ||
The following documents the methods on a store object returned by [can-fixture.store]. | ||
`Store` is a placeholder for the methods on a store object returned by [can-fixture.store]. It is for documentation purposes only. | ||
{ | ||
"name": "can-fixture", | ||
"version": "3.0.1", | ||
"version": "3.0.2", | ||
"description": "Intercept AJAX requests and simulate responses.", | ||
@@ -5,0 +5,0 @@ "main": "fixture.js", |
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
135844
0