Skip to content

Commit

Permalink
Add yields and yieldsTo to fake
Browse files Browse the repository at this point in the history
  • Loading branch information
mroderick committed Jan 8, 2018
1 parent 8c1e221 commit 67d0bd8
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
23 changes: 23 additions & 0 deletions docs/release-source/release/fakes.md
Expand Up @@ -68,6 +68,29 @@ Creates a fake that returns a rejected `Promise` for the passed value.

If an `Error` is passed as the `value` argument, then that will be the value of the promise. If any other value is passed, then that will be used for the `message` property of the `Error` returned by the promise.

#### `sinon.fake.yields(callback[, value1, ..., valueN]);`

Creates a fake that calls the provided callback with the provided values.

```js
var fake = sinon.fake.yields(console.log, 'hello world');

fake();
// hello world
```

#### `sinon.fake.yieldsAsync(callback[, value1, ..., valueN]);`

Creates a fake that calls the provided callback asynchronously with the provided values.

```js
var fake = sinon.fake.yieldsAsync(console.log, 'hello world');

fake();
// hello world
```


### `sinon.fake(func);`

Wraps an existing `Function` to record all interactions, while leaving it up to the `func` to provide the behaviour.
Expand Down
37 changes: 37 additions & 0 deletions lib/sinon/fake.js
@@ -1,6 +1,7 @@
"use strict";

var spy = require("./spy");
var nextTick = require("./util/core/next-tick");

function getError(value) {
return value instanceof Error ? value : new Error(value);
Expand Down Expand Up @@ -73,4 +74,40 @@ fake.rejects = function rejects(value) {
return wrapFunc(f);
};

function yieldInternal(async, callback, values) {
function f() {
if (async) {
nextTick(function () {
callback.apply(null, values);
});
} else {
callback.apply(null, values);
}
}

return wrapFunc(f);
}

fake.yields = function yields() {
var callback = Array.prototype.slice.call(arguments, 0, 1)[0];
var values = Array.prototype.slice.call(arguments, 1);

if (typeof callback !== "function") {
throw new TypeError("Expected callback to be a Function");
}

return yieldInternal(false, callback, values);
};

fake.yieldsAsync = function yieldsAsync() {
var callback = Array.prototype.slice.call(arguments, 0, 1)[0];
var values = Array.prototype.slice.call(arguments, 1);

if (typeof callback !== "function") {
throw new TypeError("Expected callback to be a Function");
}

return yieldInternal(true, callback, values);
};

module.exports = fake;
35 changes: 35 additions & 0 deletions test/fake-test.js
Expand Up @@ -28,6 +28,7 @@ function verifyProxy(func, argument) {
});
}

function noop() {}

describe("fake", function () {
describe("module", function () {
Expand Down Expand Up @@ -151,4 +152,38 @@ describe("fake", function () {
});
});
});

describe(".yields", function () {
verifyProxy(fake.yields, noop, "42", "43");

it("should call the callback with the provided values", function () {
var callback = sinon.spy();
var myFake = fake.yields(callback, "one", "two", "three");

myFake();

sinon.assert.calledOnce(callback);
sinon.assert.calledWith(callback, "one", "two", "three");
});
});

describe(".yieldsAsync", function () {
verifyProxy(fake.yieldsAsync, noop, "42", "43");

it("should call the callback asynchronously with the provided values", function (done) {
var callback = sinon.spy();
var myFake = fake.yieldsAsync(callback, "one", "two", "three");

myFake();

sinon.assert.notCalled(callback);

setTimeout(function () {
sinon.assert.calledOnce(callback);
sinon.assert.calledWith(callback, "one", "two", "three");

done();
}, 0);
});
});
});

0 comments on commit 67d0bd8

Please sign in to comment.