-
-
Notifications
You must be signed in to change notification settings - Fork 770
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
Add runnable example for Spies documentation - Resolves #2040 #2122
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{% for example in page.examples %} | ||
{% assign filename = 'examples/' | append: example | append: '.stub' %} | ||
<template data-example-script="{{ example }}">{% include_relative {{filename}} %}</template> | ||
<script> | ||
(function(){ | ||
// get script example div | ||
var element = document.querySelector("[data-example-id={{ example }}]"); | ||
|
||
// get script example source from the template | ||
var scriptTemplate = document.querySelector("[data-example-script={{ example }}]"); | ||
|
||
var notebook = RunKit.createNotebook({ | ||
// the parent element for the new notebook | ||
element: element, | ||
// specify the source of the notebook | ||
source: scriptTemplate.innerHTML, | ||
theme: "untilted-c3qcs6lijfuc" | ||
}); | ||
})(); // IIFE - to avoid leaking variables | ||
</script> | ||
{% endfor %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require("@fatso83/mini-mocha").install(); | ||
var sinon = require("sinon"); | ||
var PubSub = require("pubsub-js"); | ||
var referee = require("@sinonjs/referee"); | ||
var assertTrue = referee.assert; | ||
|
||
describe('PubSub', function() { | ||
it("should call subscribers on publish", function () { | ||
var callback = sinon.spy(); | ||
|
||
PubSub.subscribe("message", callback); | ||
PubSub.publishSync("message"); | ||
|
||
assertTrue(callback.called); | ||
}) | ||
}); |
31 changes: 31 additions & 0 deletions
31
docs/release-source/release/examples/spies-2-wrap-object-methods.stub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require("@fatso83/mini-mocha").install(); | ||
var sinon = require("sinon"); | ||
var referee = require("@sinonjs/referee"); | ||
var assert = referee.assert; | ||
var jsdom = require('jsdom'); | ||
var JSDOM = jsdom.JSDOM; | ||
var window = (new JSDOM()).window; | ||
var document = (new JSDOM('')).window; | ||
var jQuery = require('jquery')(window); | ||
global.document = document; | ||
|
||
describe('Wrap all object method', function() { | ||
var sandbox = sinon.createSandbox(); | ||
|
||
beforeEach(function() { | ||
sandbox.spy(jQuery); | ||
}); | ||
|
||
afterEach(function() { | ||
sandbox.restore(); | ||
}); | ||
|
||
it("should inspect jQuery.getJSON's usage of jQuery.ajax", function () { | ||
var url = "https://jsonplaceholder.typicode.com/todos/1"; | ||
jQuery.getJSON(url); | ||
|
||
assert(jQuery.ajax.calledOnce); | ||
assert.equals(url, jQuery.ajax.getCall(0).args[0].url); | ||
assert.equals("json", jQuery.ajax.getCall(0).args[0].dataType); | ||
}) | ||
}); |
31 changes: 31 additions & 0 deletions
31
docs/release-source/release/examples/spies-3-wrap-existing-method.stub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require("@fatso83/mini-mocha").install(); | ||
var sinon = require("sinon"); | ||
var referee = require("@sinonjs/referee"); | ||
var assert = referee.assert; | ||
var jsdom = require('jsdom'); | ||
var JSDOM = jsdom.JSDOM; | ||
var window = (new JSDOM()).window; | ||
var document = (new JSDOM('')).window; | ||
var jQuery = require('jquery')(window); | ||
global.document = document; | ||
|
||
describe('Wrap existing method', function() { | ||
var sandbox = sinon.createSandbox(); | ||
|
||
beforeEach(function() { | ||
sandbox.spy(jQuery, "ajax"); | ||
}); | ||
|
||
afterEach(function() { | ||
sandbox.restore(); | ||
}); | ||
|
||
it("should inspect jQuery.getJSON's usage of jQuery.ajax", function () { | ||
var url = "https://jsonplaceholder.typicode.com/todos/1"; | ||
jQuery.getJSON(url); | ||
|
||
assert(jQuery.ajax.calledOnce); | ||
assert.equals(url, jQuery.ajax.getCall(0).args[0].url); | ||
assert.equals("json", jQuery.ajax.getCall(0).args[0].dataType); | ||
}) | ||
}); |
17 changes: 17 additions & 0 deletions
17
docs/release-source/release/examples/spies-4-pubsub-message-1.stub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require("@fatso83/mini-mocha").install(); | ||
var sinon = require("sinon"); | ||
var PubSub = require("pubsub-js"); | ||
var referee = require("@sinonjs/referee"); | ||
var assert = referee.assert; | ||
|
||
describe('PubSub', function() { | ||
it("should call subscribers with message as first argument", function () { | ||
var message = 'an example message'; | ||
var spy = sinon.spy(); | ||
|
||
PubSub.subscribe(message, spy); | ||
PubSub.publishSync(message, "some payload"); | ||
|
||
assert(spy.calledWith(message)); | ||
}) | ||
}); |
17 changes: 17 additions & 0 deletions
17
docs/release-source/release/examples/spies-5-pubsub-message-2.stub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require("@fatso83/mini-mocha").install(); | ||
var sinon = require("sinon"); | ||
var PubSub = require("pubsub-js"); | ||
var referee = require("@sinonjs/referee"); | ||
var assert = referee.assert; | ||
|
||
describe('PubSub', function() { | ||
it("should call subscribers with message as first argument", function () { | ||
var message = 'an example message'; | ||
var spy = sinon.spy(); | ||
|
||
PubSub.subscribe(message, spy); | ||
PubSub.publishSync(message, "some payload"); | ||
|
||
assert.equals(message, spy.args[0][0]); | ||
}) | ||
}); |
17 changes: 17 additions & 0 deletions
17
docs/release-source/release/examples/spies-6-pubsub-message-3.stub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require("@fatso83/mini-mocha").install(); | ||
var sinon = require("sinon"); | ||
var PubSub = require("pubsub-js"); | ||
var referee = require("@sinonjs/referee"); | ||
var assert = referee.assert; | ||
|
||
describe('PubSub', function() { | ||
it("should call subscribers with message as first argument", function () { | ||
var message = 'an example message'; | ||
var spy = sinon.spy(); | ||
|
||
PubSub.subscribe(message, spy); | ||
PubSub.publishSync(message, "some payload"); | ||
|
||
assert.equals(message, spy.getCall(0).args[0]); | ||
}) | ||
}); |
17 changes: 17 additions & 0 deletions
17
docs/release-source/release/examples/spies-7-with-args.stub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
require("@fatso83/mini-mocha").install(); | ||
var sinon = require("sinon"); | ||
var referee = require("@sinonjs/referee"); | ||
var assert = referee.assert; | ||
|
||
describe('withArgs', function() { | ||
it("should call method once with each argument", function () { | ||
var object = { method: function () {} }; | ||
var spy = sinon.spy(object, "method"); | ||
|
||
object.method(42); | ||
object.method(1); | ||
|
||
assert(spy.withArgs(42).calledOnce); | ||
assert(spy.withArgs(1).calledOnce); | ||
}) | ||
}); |
30 changes: 30 additions & 0 deletions
30
docs/release-source/release/examples/spies-8-spy-call.stub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require("@fatso83/mini-mocha").install(); | ||
var sinon = require("sinon"); | ||
var referee = require("@sinonjs/referee"); | ||
var assert = referee.assert; | ||
var jsdom = require('jsdom'); | ||
var JSDOM = jsdom.JSDOM; | ||
var window = (new JSDOM()).window; | ||
var document = (new JSDOM('')).window; | ||
var jQuery = require('jquery')(window); | ||
global.document = document; | ||
|
||
describe('Return nth call', function() { | ||
var sandbox = sinon.createSandbox(); | ||
|
||
beforeEach(function() { | ||
sandbox.spy(jQuery, "ajax"); | ||
}); | ||
|
||
afterEach(function() { | ||
sandbox.restore(); | ||
}); | ||
|
||
it("should inspect jQuery.getJSON's usage of jQuery.ajax", function () { | ||
var url = "https://jsonplaceholder.typicode.com/todos/1"; | ||
jQuery.ajax(url); | ||
var spyCall = jQuery.ajax.getCall(0); | ||
|
||
assert.equals(url, spyCall.args[0]); | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just put the script at the end of this file, along with the include