Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support returning promises from dynamic fixtures #164

Merged
merged 5 commits into from
Apr 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions core.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,19 @@ exports.callDynamicFixture = function(xhrSettings, fixtureSettings, cb){
// fall the fixture
var result = fixtureSettings.fixture(xhrSettings, response, xhrSettings.headers, fixtureSettings);

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 );
}
}
};

Expand Down
49 changes: 44 additions & 5 deletions docs/can-fixture.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

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:

```js
Expand All @@ -34,7 +36,44 @@
```
@codepen

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}`.
@param {can-fixture.requestHandler} requestHandler Handles the request and provides a response.
Expand Down Expand Up @@ -67,7 +106,7 @@
@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 {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 )`
Expand Down Expand Up @@ -182,7 +221,7 @@
"GET /tasks": { data: [ {id: 1, name: "mow lawn"} ] },
"/people": "fixtures/people.json"
} );

ajax( {type: "POST", url:"/tasks"} ).then( result => {
console.log( result ); //-> {id: RandomNumber}
} );
Expand Down Expand Up @@ -249,6 +288,6 @@
@signature `fixture(ajaxSettingsArray)`

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.
22 changes: 19 additions & 3 deletions test/fixture_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,27 @@ test('dynamic fixtures', function () {
.done(function (data) {
equal(data[0].sweet, 'ness', 'can.get works');
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 !== '/') {
test('fixture function', 3, function () {
stop();
Expand Down Expand Up @@ -1832,7 +1848,7 @@ test('fixture returns the old fixture callback when fixtures are removed (#34)',
return "foo";
};
fixture("/services/thing", funcA);

// in a test, remove default fixture and provide your own
var oldFixtures = fixture("/services/thing", null);
QUnit.deepEqual(oldFixtures, [{fixture: funcA, url: '/services/thing'}]);
Expand Down