can-fixture
Advanced tools
Comparing version 3.0.5 to 3.1.0
First and foremost, thanks for contributing to can-fixture and CanJS! If you | ||
have any questions, reach out to us on the [gitter channel](https://gitter.im/canjs/canjs). | ||
have any questions, reach out to us on [Slack](https://www.bitovi.com/community/slack) ([#canjs channel](https://bitovi-community.slack.com/messages/CFC22NZ8A)). | ||
@@ -17,7 +17,7 @@ The following details how to make contributions such as: | ||
Report a bug as a new [github issue](https://github.com/canjs/can-fixture/issues/new). | ||
Report a bug as a new [GitHub issue](https://github.com/canjs/can-fixture/issues/new). | ||
When filing a bug, it is extremely helpful to include: | ||
- A small example with [JSBin](https://justinbmeyer.jsbin.com/zixumu/2/edit?html,js,output). | ||
- A small example with [JS Bin](https://justinbmeyer.jsbin.com/zixumu/2/edit?html,js,output). | ||
- Breaking tests (optional) | ||
@@ -24,0 +24,0 @@ - Proposed solutions (optional) |
16
core.js
@@ -216,5 +216,15 @@ "use strict"; | ||
if (result !== undefined) { | ||
// Resolve with fixture results | ||
response(200, result ); | ||
if (canReflect.isPromise(result)) { | ||
// If we have a promise, wait for it to resolve | ||
result.then(function (result) { | ||
if (result !== undefined) { | ||
// Resolve with fixture results | ||
response(200, result ); | ||
} | ||
}); | ||
} else { | ||
if (result !== undefined) { | ||
// Resolve with fixture results | ||
response(200, result ); | ||
} | ||
} | ||
@@ -221,0 +231,0 @@ }; |
@@ -14,2 +14,4 @@ @module {function} can-fixture | ||
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: | ||
@@ -38,4 +40,41 @@ | ||
When adding a fixture, it will remove any identical fixtures from the list of fixtures. The last fixture added will be the first matched. | ||
Return a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) (or use [async](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function) & [await](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await)) from `requestHandler` to asynchronously return results. This allows fixtures to depend on each other, introduce dynamic delays, and even depend on external resources. | ||
```js | ||
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"}] | ||
} ); | ||
``` | ||
@codepen | ||
@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}`. | ||
@@ -72,3 +111,3 @@ @param {can-fixture.requestHandler} requestHandler Handles the request and provides a response. | ||
@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 {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. | ||
@@ -188,3 +227,3 @@ | ||
} ); | ||
ajax( {type: "POST", url:"/tasks"} ).then( result => { | ||
@@ -256,4 +295,4 @@ console.log( result ); //-> {id: RandomNumber} | ||
Add fixtures that have been previously removed with another call to fixture. | ||
@param {Array<can-fixture/types/ajaxSettings>} An array of AJAX settings objects | ||
@return {Array<can-fixture/types/ajaxSettings>} Returns an array of any fixtures that are replaced by the fixtures that are added. | ||
@return {Array<can-fixture/types/ajaxSettings>} Returns an array of any fixtures that are replaced by the fixtures that are added. |
{ | ||
"name": "can-fixture", | ||
"version": "3.0.5", | ||
"version": "3.1.0", | ||
"description": "Intercept AJAX requests and simulate responses.", | ||
@@ -37,3 +37,3 @@ "main": "fixture.js", | ||
"steal-tools": "^1.0.0", | ||
"testee": "^0.8.0" | ||
"testee": "^0.9.0" | ||
}, | ||
@@ -40,0 +40,0 @@ "repository": { |
# can-fixture | ||
[![Join the chat at https://gitter.im/canjs/canjs](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/canjs/canjs?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) | ||
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://github.com/canjs/can-fixture/blob/master/LICENSE.md) | ||
[![Join our Slack](https://img.shields.io/badge/slack-join%20chat-611f69.svg)](https://www.bitovi.com/community/slack?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) | ||
[![Join our Discourse](https://img.shields.io/discourse/https/forums.bitovi.com/posts.svg)](https://forums.bitovi.com/?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) | ||
[![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/canjs/can-fixture/blob/master/LICENSE.md) | ||
[![npm version](https://badge.fury.io/js/can-fixture.svg)](https://www.npmjs.com/package/can-fixture) | ||
@@ -9,7 +10,7 @@ [![Travis build status](https://travis-ci.org/canjs/can-fixture.svg?branch=master)](https://travis-ci.org/canjs/can-fixture) | ||
the response with a file or function. Use `can-fixture` to: | ||
Intercept and simulate AJAX requests. Works without CanJS. | ||
## Documentation | ||
Read the [API docs on CanJS.com](https://canjs.com/doc/can-fixture.html). | ||
Read the [can-fixture API docs on CanJS.com](https://canjs.com/doc/can-fixture.html). | ||
@@ -27,2 +28,1 @@ ## Changelog | ||
[MIT](https://github.com/canjs/can-fixture/blob/master/LICENSE.md) | ||
@@ -138,7 +138,23 @@ require("./matches-test"); | ||
start(); | ||
}).catch(function(err){ | ||
debugger; | ||
}); | ||
}); | ||
test('dynamic fixtures return promises', function () { | ||
stop(); | ||
fixture.delay = 10; | ||
fixture('something', function () { | ||
return Promise.resolve([{ | ||
sweet: 'ness' | ||
}]); | ||
}); | ||
$.ajax({ | ||
url: 'something', | ||
dataType: 'json' | ||
}).then(function (data) { | ||
equal(data[0].sweet, 'ness', 'can.get works'); | ||
start(); | ||
}); | ||
}); | ||
if (__dirname !== '/') { | ||
@@ -1836,3 +1852,3 @@ test('fixture function', 3, function () { | ||
fixture("/services/thing", funcA); | ||
// in a test, remove default fixture and provide your own | ||
@@ -1839,0 +1855,0 @@ var oldFixtures = fixture("/services/thing", null); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
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
140996
3200
0