Skip to content

Commit 3b3c562

Browse files
committed
Add release documentation for v8.0.0
1 parent bbc5f18 commit 3b3c562

36 files changed

+3538
-113
lines changed

docs/_releases/latest.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
---
22
layout: page
33
title: API documentation - Sinon.JS
4-
release_id: v7.5.0
4+
skip_ad: true
5+
release_id: v8.0.0
56
---
67

78
# {{page.title}} - `{{page.release_id}}`
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require("@fatso83/mini-mocha").install();
2+
var sinon = require("sinon");
3+
var PubSub = require("pubsub-js");
4+
var referee = require("@sinonjs/referee");
5+
var assertTrue = referee.assert;
6+
7+
describe('PubSub', function() {
8+
it("should call subscribers on publish", function () {
9+
var callback = sinon.spy();
10+
11+
PubSub.subscribe("message", callback);
12+
PubSub.publishSync("message");
13+
14+
assertTrue(callback.called);
15+
})
16+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require("@fatso83/mini-mocha").install();
2+
var sinon = require("sinon");
3+
var referee = require("@sinonjs/referee");
4+
var assert = referee.assert;
5+
var jsdom = require('jsdom');
6+
var JSDOM = jsdom.JSDOM;
7+
var window = (new JSDOM()).window;
8+
var document = (new JSDOM('')).window;
9+
var jQuery = require('jquery')(window);
10+
global.document = document;
11+
12+
describe('Wrap all object method', function() {
13+
var sandbox = sinon.createSandbox();
14+
15+
beforeEach(function() {
16+
sandbox.spy(jQuery);
17+
});
18+
19+
afterEach(function() {
20+
sandbox.restore();
21+
});
22+
23+
it("should inspect jQuery.getJSON's usage of jQuery.ajax", function () {
24+
var url = "https://jsonplaceholder.typicode.com/todos/1";
25+
jQuery.getJSON(url);
26+
27+
assert(jQuery.ajax.calledOnce);
28+
assert.equals(url, jQuery.ajax.getCall(0).args[0].url);
29+
assert.equals("json", jQuery.ajax.getCall(0).args[0].dataType);
30+
})
31+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require("@fatso83/mini-mocha").install();
2+
var sinon = require("sinon");
3+
var referee = require("@sinonjs/referee");
4+
var assert = referee.assert;
5+
var jsdom = require('jsdom');
6+
var JSDOM = jsdom.JSDOM;
7+
var window = (new JSDOM()).window;
8+
var document = (new JSDOM('')).window;
9+
var jQuery = require('jquery')(window);
10+
global.document = document;
11+
12+
describe('Wrap existing method', function() {
13+
var sandbox = sinon.createSandbox();
14+
15+
beforeEach(function() {
16+
sandbox.spy(jQuery, "ajax");
17+
});
18+
19+
afterEach(function() {
20+
sandbox.restore();
21+
});
22+
23+
it("should inspect jQuery.getJSON's usage of jQuery.ajax", function () {
24+
var url = "https://jsonplaceholder.typicode.com/todos/1";
25+
jQuery.getJSON(url);
26+
27+
assert(jQuery.ajax.calledOnce);
28+
assert.equals(url, jQuery.ajax.getCall(0).args[0].url);
29+
assert.equals("json", jQuery.ajax.getCall(0).args[0].dataType);
30+
})
31+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require("@fatso83/mini-mocha").install();
2+
var sinon = require("sinon");
3+
var PubSub = require("pubsub-js");
4+
var referee = require("@sinonjs/referee");
5+
var assert = referee.assert;
6+
7+
describe('PubSub', function() {
8+
it("should call subscribers with message as first argument", function () {
9+
var message = 'an example message';
10+
var spy = sinon.spy();
11+
12+
PubSub.subscribe(message, spy);
13+
PubSub.publishSync(message, "some payload");
14+
15+
assert(spy.calledWith(message));
16+
})
17+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require("@fatso83/mini-mocha").install();
2+
var sinon = require("sinon");
3+
var PubSub = require("pubsub-js");
4+
var referee = require("@sinonjs/referee");
5+
var assert = referee.assert;
6+
7+
describe('PubSub', function() {
8+
it("should call subscribers with message as first argument", function () {
9+
var message = 'an example message';
10+
var spy = sinon.spy();
11+
12+
PubSub.subscribe(message, spy);
13+
PubSub.publishSync(message, "some payload");
14+
15+
assert.equals(message, spy.args[0][0]);
16+
})
17+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require("@fatso83/mini-mocha").install();
2+
var sinon = require("sinon");
3+
var PubSub = require("pubsub-js");
4+
var referee = require("@sinonjs/referee");
5+
var assert = referee.assert;
6+
7+
describe('PubSub', function() {
8+
it("should call subscribers with message as first argument", function () {
9+
var message = 'an example message';
10+
var spy = sinon.spy();
11+
12+
PubSub.subscribe(message, spy);
13+
PubSub.publishSync(message, "some payload");
14+
15+
assert.equals(message, spy.getCall(0).args[0]);
16+
})
17+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require("@fatso83/mini-mocha").install();
2+
var sinon = require("sinon");
3+
var referee = require("@sinonjs/referee");
4+
var assert = referee.assert;
5+
6+
describe('withArgs', function() {
7+
it("should call method once with each argument", function () {
8+
var object = { method: function () {} };
9+
var spy = sinon.spy(object, "method");
10+
11+
object.method(42);
12+
object.method(1);
13+
14+
assert(spy.withArgs(42).calledOnce);
15+
assert(spy.withArgs(1).calledOnce);
16+
})
17+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require("@fatso83/mini-mocha").install();
2+
var sinon = require("sinon");
3+
var referee = require("@sinonjs/referee");
4+
var assert = referee.assert;
5+
var jsdom = require('jsdom');
6+
var JSDOM = jsdom.JSDOM;
7+
var window = (new JSDOM()).window;
8+
var document = (new JSDOM('')).window;
9+
var jQuery = require('jquery')(window);
10+
global.document = document;
11+
12+
describe('Return nth call', function() {
13+
var sandbox = sinon.createSandbox();
14+
15+
beforeEach(function() {
16+
sandbox.spy(jQuery, "ajax");
17+
});
18+
19+
afterEach(function() {
20+
sandbox.restore();
21+
});
22+
23+
it("should inspect jQuery.getJSON's usage of jQuery.ajax", function () {
24+
var url = "https://jsonplaceholder.typicode.com/todos/1";
25+
jQuery.ajax(url);
26+
var spyCall = jQuery.ajax.getCall(0);
27+
28+
assert.equals(url, spyCall.args[0]);
29+
})
30+
});

docs/_releases/latest/fakes.md

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,49 @@ breadcrumb: fakes
66

77
### Introduction
88

9-
`fake` was introduced with Sinon with v5. It simplifies and merges concepts from [`spies`][spies] and [`stubs`][stubs].
9+
`fake` is available in Sinon from v5 onwards. It allows creation of a `fake` `Function` with the ability to set a default [behavior](#fakes-with-behavior). Set the [behavior](#fakes-with-behavior) using `Functions` with the same API as those in a [`sinon.stub`][stubs]. The created `fake` `Function`, with or without behavior has the same API as a (`sinon.spy`)[spies].
1010

1111
In Sinon, a `fake` is a `Function` that records arguments, return value, the value of `this` and exception thrown (if any) for all of its calls.
1212

13-
It can be created with or without behavior; it can wrap an existing function.
14-
1513
A fake is immutable: once created, the behavior will not change.
1614

1715
Unlike [`sinon.spy`][spies] and [`sinon.stub`][stubs] methods, the `sinon.fake` API knows only how to create fakes, and doesn't concern itself with plugging them into the system under test. To plug the fakes into the system under test, you can use the [`sinon.replace*`](../sandbox#sandboxreplaceobject-property-replacement) methods.
1816

19-
2017
### Creating a fake
2118

19+
Create a `fake` `Function` with or without [behavior](#fakes-with-behavior). The created `Function` has the same API as a [`sinon.spy`][spies].
20+
21+
#### Creating a fake without behavior
22+
2223
```js
2324
// create a basic fake, with no behavior
2425
var fake = sinon.fake();
2526

2627
fake();
28+
// undefined
2729

28-
console.log(fake.callCount);
30+
fake.callCount;
2931
// 1
3032
```
3133

34+
#### Creating a fake with custom behaviour
35+
36+
```js
37+
// create a fake that returns the text "foo"
38+
var fake = sinon.fake.returns('foo');
39+
40+
fake()
41+
// foo
42+
```
43+
3244
### Fakes with behavior
3345

34-
Fakes can be created with behavior, which cannot be changed once the fake has been created.
46+
Fakes cannot change once created with behaviour.
3547

3648
#### `sinon.fake.returns(value);`
3749

38-
Creates a fake that returns the `value` argument
50+
Creates a fake that returns the `value` argument.
51+
3952

4053
```js
4154
var fake = sinon.fake.returns('apple pie');
@@ -48,6 +61,7 @@ fake();
4861

4962
Creates a fake that throws an `Error` with the provided value as the `message` property.
5063

64+
5165
If an `Error` is passed as the `value` argument, then that will be the thrown value. If any other value is passed, then that will be used for the `message` property of the thrown `Error`.
5266

5367
```js
@@ -61,16 +75,19 @@ fake();
6175

6276
Creates a fake that returns a resolved `Promise` for the passed value.
6377

78+
6479
#### `sinon.fake.rejects(value);`
6580

6681
Creates a fake that returns a rejected `Promise` for the passed value.
6782

83+
6884
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.
6985

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

7288
`sinon.fake.yields` takes some values, and returns a function that when being called, expects the last argument to be a callback and invokes that callback with the same previously given values. The returned function is normally used to fake a service function that takes a callback as the last argument.
7389

90+
7491
In code example below, the '[readFile](https://nodejs.org/api/fs.html#fs_fs_readfile_path_options_callback)' function of the 'fs' module is replaced with a fake function created by `sinon.fake.yields`. When the fake function is called, it always calls the last argument it received, which is expected to be a callback, with the values that the `yields` function previously took.
7592

7693
```js
@@ -85,6 +102,7 @@ console.log('end of this event loop');
85102

86103
Similar to `yields`, `yieldsAsync` also returns a function that when invoked, the function expects the last argument to be a callback and invokes that callback with the same previously given values. However, the returned function invokes that callback asynchronously rather than immediately, i.e. in the next event loop.
87104

105+
88106
Compare the output of the code example below with the output of the code example above for `yields` to see the difference.
89107

90108
```js
@@ -100,10 +118,14 @@ console.log('end of this event loop');
100118

101119
Wraps an existing `Function` to record all interactions, while leaving it up to the `func` to provide the behavior.
102120

121+
The created `fake` `Function` has the same API as a [`sinon.spy`][spies].
122+
103123
This is useful when complex behavior not covered by the `sinon.fake.*` methods is required or when wrapping an existing function or method.
104124

105125
### Instance properties
106126

127+
The instance properties are the same as a [`sinon.spy`][spies].
128+
107129
#### `f.callback`
108130

109131
This property is a convenience to get a reference to the last callback passed in the last to the fake.

0 commit comments

Comments
 (0)