Skip to content

Commit

Permalink
Merge pull request #4007 from patheard/sandbox
Browse files Browse the repository at this point in the history
Test: use sinon.sandbox for easier test restore
  • Loading branch information
nschonni committed Dec 11, 2013
2 parents 26d4814 + 729e16b commit 43eb0ab
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 29 deletions.
7 changes: 4 additions & 3 deletions src/plugins/data-picture/test.js
Expand Up @@ -14,14 +14,15 @@
* teardown `after()` for more than one test suite (as is the case below.)
*/
describe( "[data-pic] test suite", function() {
var spy;
var spy,
sandbox = sinon.sandbox.create();

/*
* Before beginning the test suite, this function is executed once.
*/
before(function() {
// Spy on jQuery's trigger method to see how it's called during the plugin's initialization
spy = sinon.spy( $.prototype, "trigger" );
spy = sandbox.spy( $.prototype, "trigger" );

// Trigger the plugin's initialization
$( "[data-pic]" ).trigger( "wb-init.wb-pic" );
Expand All @@ -32,7 +33,7 @@ describe( "[data-pic] test suite", function() {
*/
after(function() {
// Restore the original behaviour of trigger once the tests are finished
$.prototype.trigger.restore();
sandbox.restore();

// Remove test data from the page
$( ".test[data-pic]" ).remove();
Expand Down
13 changes: 7 additions & 6 deletions src/plugins/favicon/test.js
Expand Up @@ -16,14 +16,15 @@
*/
describe( "Favicon test suite", function() {

var spies = {};
var spy,
sandbox = sinon.sandbox.create();

/*
* Before beginning the test suite, this function is executed once.
*/
before(function( done ) {
// Spy on jQuery's trigger methods
spies.trigger = sinon.spy( $.prototype, "trigger" );
spy = sandbox.spy( $.prototype, "trigger" );

wb.doc.on( "mobile.wb-favicon", "link[rel='shortcut icon']", function() {
done();
Expand All @@ -35,7 +36,7 @@ describe( "Favicon test suite", function() {
*/
after(function() {
// Restore the original behaviour of trigger once the tests are finished
$.prototype.trigger.restore();
sandbox.restore();
});

/*
Expand All @@ -44,16 +45,16 @@ describe( "Favicon test suite", function() {
describe( "init events", function() {

it( "should trigger mobile.wb-favicon event", function() {
expect( spies.trigger.calledWith( "mobile.wb-favicon" ) ).to.equal( true );
expect( spy.calledWith( "mobile.wb-favicon" ) ).to.equal( true );
});

it( "should have been triggered on a link[rel='shortcut icon'] element", function() {
var call, i, j, lenCalls, lenElms,
isSelector = false;

// Loop over calls made on the trigger() spy
for ( i = 0, lenCalls = spies.trigger.callCount; !isSelector && i < lenCalls; i += 1 ) {
call = spies.trigger.getCall( i );
for ( i = 0, lenCalls = spy.callCount; !isSelector && i < lenCalls; i += 1 ) {
call = spy.getCall( i );
// There may be multiple `this` objects for each call
for ( j = 0, lenElms = call.thisValue.length; !isSelector && j < lenElms; j += 1 ) {
isSelector = call.thisValue[ j ].nodeName === "LINK" && call.thisValue[ j ].rel === "shortcut icon";
Expand Down
7 changes: 4 additions & 3 deletions src/plugins/prettify/test.js
Expand Up @@ -14,14 +14,15 @@
* teardown `after()` for more than one test suite (as is the case below.)
*/
describe( "Prettify test suite", function() {
var spy;
var spy,
sandbox = sinon.sandbox.create();

/*
* Before beginning the test suite, this function is executed once.
*/
before(function(done) {
// Spy on jQuery's trigger method to see how it's called during the plugin's initialization
spy = sinon.spy( $.prototype, "trigger" );
spy = sandbox.spy( $.prototype, "trigger" );

// Start the tests once the plugin has been initialized
$( ".wb-prettify" ).removeClass( "all-pre" );
Expand All @@ -35,7 +36,7 @@ describe( "Prettify test suite", function() {
*/
after(function() {
// Restore the original behaviour of trigger once the tests are finished
$.prototype.trigger.restore();
sandbox.restore();

// Remove test data from the page
$( "pre.test" ).remove();
Expand Down
22 changes: 8 additions & 14 deletions src/plugins/session-timeout/test.js
Expand Up @@ -18,21 +18,22 @@ describe( "Session Timeout test suite", function() {

var clock,
server,
spies = {};
spies = {},
sandbox = sinon.sandbox.create();

/*
* Before beginning the test suite, this function is executed once.
*/
before(function( done ) {
// Spy on jQuery's trigger and post methods
spies.trigger = sinon.spy( $.prototype, "trigger" );
spies.post = sinon.spy( $, "post" );
spies.trigger = sandbox.spy( $.prototype, "trigger" );
spies.post = sandbox.spy( $, "post" );

// Fake server to test POST requests
server = sinon.fakeServer.create();
server = sandbox.useFakeServer();

// Use a fake timer (allows for easy testing of setTimeout calls)
clock = sinon.useFakeTimers();
clock = sandbox.useFakeTimers();

// Wait for the reset event from the plugin's init method before beginning the test
$( ".wb-sessto" )
Expand All @@ -55,15 +56,8 @@ describe( "Session Timeout test suite", function() {
* After finishing the test suite, this function is executed once.
*/
after(function() {
// Restore the original behaviour of trigger and post once the tests are finished
$.prototype.trigger.restore();
$.post.restore();

// Restore server
server.restore();

// Restore the global clock
clock.restore();
// Restore the original behaviour of spies, server and timer
sandbox.restore();
});

/*
Expand Down
7 changes: 4 additions & 3 deletions src/plugins/toggle/test.js
Expand Up @@ -14,14 +14,15 @@
* teardown `after()` for more than one test suite (as is the case below.)
*/
describe( "Toggle test suite", function() {
var spy;
var spy,
sandbox = sinon.sandbox.create();

/*
* Before begining the test suite, this function is exectued once.
*/
before(function() {
// Spy on jQuery's trigger method to see how it's called during the plugin's initialization
spy = sinon.spy( $.prototype, "trigger" );
spy = sandbox.spy( $.prototype, "trigger" );

$( ".wb-toggle" )
.removeClass( "wb-toggle-inited" )
Expand All @@ -33,7 +34,7 @@ describe( "Toggle test suite", function() {
*/
after(function() {
// Restore the original behaviour of trigger once the tests are finished
$.prototype.trigger.restore();
sandbox.restore();
});

/*
Expand Down

0 comments on commit 43eb0ab

Please sign in to comment.