From 729e16b1bdf12d6437d039a2c224531f9cbc2448 Mon Sep 17 00:00:00 2001 From: Pat Heard Date: Wed, 11 Dec 2013 07:47:02 -0500 Subject: [PATCH] Test: use sinon.sandbox for easier test restore --- src/plugins/data-picture/test.js | 7 ++++--- src/plugins/favicon/test.js | 13 +++++++------ src/plugins/prettify/test.js | 7 ++++--- src/plugins/session-timeout/test.js | 22 ++++++++-------------- src/plugins/toggle/test.js | 7 ++++--- 5 files changed, 27 insertions(+), 29 deletions(-) diff --git a/src/plugins/data-picture/test.js b/src/plugins/data-picture/test.js index 04e10ccdfc4..ce4776cb017 100644 --- a/src/plugins/data-picture/test.js +++ b/src/plugins/data-picture/test.js @@ -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" ); @@ -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(); diff --git a/src/plugins/favicon/test.js b/src/plugins/favicon/test.js index 1358e089e33..77cc32d5736 100644 --- a/src/plugins/favicon/test.js +++ b/src/plugins/favicon/test.js @@ -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(); @@ -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(); }); /* @@ -44,7 +45,7 @@ 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() { @@ -52,8 +53,8 @@ describe( "Favicon test suite", function() { 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"; diff --git a/src/plugins/prettify/test.js b/src/plugins/prettify/test.js index 2e8bf3911b7..3cd19ad9f29 100644 --- a/src/plugins/prettify/test.js +++ b/src/plugins/prettify/test.js @@ -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" ); @@ -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(); diff --git a/src/plugins/session-timeout/test.js b/src/plugins/session-timeout/test.js index 0e51df84e0c..b84fa753323 100644 --- a/src/plugins/session-timeout/test.js +++ b/src/plugins/session-timeout/test.js @@ -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" ) @@ -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(); }); /* diff --git a/src/plugins/toggle/test.js b/src/plugins/toggle/test.js index 9f321b44018..54462f2ae9f 100644 --- a/src/plugins/toggle/test.js +++ b/src/plugins/toggle/test.js @@ -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" ) @@ -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(); }); /*