diff options
author | Rob Loach <robloach@gmail.com> | 2014-12-28 03:26:20 -0500 |
---|---|---|
committer | Rob Loach <robloach@gmail.com> | 2014-12-28 03:26:20 -0500 |
commit | 5469875723c349ae292087160f08fe2088465e5c (patch) | |
tree | 85785d9ce22b7fed8a629e2edb06a6493973aa65 /test | |
parent | 18f8b7c31d54d00113e725cbec37e33487f0fe0c (diff) | |
download | jquery-once-5469875723c349ae292087160f08fe2088465e5c.zip jquery-once-5469875723c349ae292087160f08fe2088465e5c.tar.gz jquery-once-5469875723c349ae292087160f08fe2088465e5c.tar.bz2 |
Move more tests to Mocha
Diffstat (limited to 'test')
-rw-r--r-- | test/index.html | 39 | ||||
-rw-r--r-- | test/test.js | 94 |
2 files changed, 44 insertions, 89 deletions
diff --git a/test/index.html b/test/index.html deleted file mode 100644 index 6a0293f..0000000 --- a/test/index.html +++ /dev/null @@ -1,39 +0,0 @@ -<!DOCTYPE html> -<html> -<head> - <title>jQuery Once - Test Suite</title> - <link rel="stylesheet" href="../node_modules/qunitjs/qunit/qunit.css"> -</head> -<body id="body"> - <div id="qunit"></div> - <div id="qunit-fixture"> - - <div id="test1"> - <p>This is <span>Test 1</span>.</p> - </div> - - <div id="test2"> - <p>This is <span>Test 2</span>.</p> - </div> - - <div id="test3"> - <p>This is <span>Test 3</span>.</p> - </div> - - <div id="test4"> - <p>This is <span>Test 4</span>.</p> - </div> - - <div id="test5"> - <p>This is <span>Test 5</span>.</p> - <p>This is <span>Test 5 #2</span>.</p> - </div> - - </div> - - <script src="../node_modules/jquery/dist/jquery.js"></script> - <script src="../node_modules/qunitjs/qunit/qunit.js"></script> - <script src="../jquery.once.js"></script> - <script src="test.js"></script> -</body> -</html> diff --git a/test/test.js b/test/test.js index c462a27..e0c4711 100644 --- a/test/test.js +++ b/test/test.js @@ -1,6 +1,9 @@ var jsdom = require("mocha-jsdom"); var assert = require("assert"); +/** + * Automated tests for jQuery Once. + */ describe("jQuery Once", function() { "use strict"; @@ -22,10 +25,15 @@ describe("jQuery Once", function() { require("../jquery.once.js"); }); - it("should require ID to be a string", function () { + /** + * Before each test, reset the document body so that there is fresh data. + */ + beforeEach(function() { // Build the body HTML. - document.body.innerHTML = "<p>This is <span>Test 1</span>.</p>"; + document.body.innerHTML = "<p>This is the <span>Test</span>.</p>"; + }); + it("should require ID to be a string", function () { // Expect it to throw an error. assert.throws(function() { $("span").once(function () { @@ -35,9 +43,6 @@ describe("jQuery Once", function() { }); it("properly executes .once('test2')", function () { - // Prepare the document body. - document.body.innerHTML = "<p>This is <span>Test 2</span>.</p>"; - // Create one once('test2') call. $("span").once("test2").data("test2", "foo"); @@ -50,9 +55,6 @@ describe("jQuery Once", function() { }); it("is called only once with an ID", function() { - // Prepare the document body. - document.body.innerHTML = "<p>This is <span>Test 3</span>.</p>"; - // Count the number of times once() was called. $("span").data("count", 0); @@ -78,65 +80,57 @@ describe("jQuery Once", function() { // Create the once() callback. var callback = function() { - $("span").data("once", $("span").data('once') + 1); + $("span").data("once", $("span").data("once") + 1); }; // Call once() a bunch of times. for (var i = 0; i < 10; i++) { - $('#test2 span').once().each(callback); + $("span").once().each(callback); } // Verify that it was only called once. - var count = $('#test2 span').data('once'); - ok(count === 1, 'It was called ' + count + ' times.'); + var count = $("span").data("once"); + assert.equal(count, 1, "It was called " + count + " times."); }); -}); - -/* + it("retrieves empty once data correctly", function() { + // Verify that the element starts without the class. + var hasData = $("span").data("jquery-once-test3"); + assert(!hasData, "Value not applied in the beginning."); -test("Apply the value to data correctly", function() { - // Verify that the element starts without the class. - var hasData = $('#test3 span').data('jquery-once-test3'); - ok(!hasData, 'Value not applied in the beginning.'); + // Create one once() call. + $("span").once("test3"); - // Create one once() call. - $('#test3 span').once('test3'); + // Verify the data is applied. + hasData = $("span").data("jquery-once-test3"); + assert(hasData, "The value is properly applied after once()."); + }); - // Verify the data is applied. - hasData = $('#test3 span').data('jquery-once-test3'); - ok(hasData, 'The value is properly applied after once().'); -}); + it("calls removeOnce() correctly", function() { + // Create one once() call. + $("span").once("test4"); -test("Remove the value from attribute correctly", function() { - // Create one once() call. - $('#test4 span').once('test4'); + // Verify the data is applied. + var hasData = $("span").data("jquery-once-test4"); + assert(hasData, "The value is properly applied after once()."); - // Verify the data is applied. - var hasData = $('#test4 span').data('jquery-once-test4'); - ok(hasData, 'The value is properly applied after once().'); + // Remove the once property. + $("span").removeOnce("test4"); + hasData = $("span").data("jquery-once-test4"); + assert(!hasData, "The value is properly removed when called removeOnce()."); + }); - // Remove the once property. - $('#test4 span').removeOnce('test4'); - hasData = $('#test4 span').data('jquery-once-test4'); - ok(!hasData, 'The value is properly removed when called removeOnce().'); -}); + it("calls findOnce() correctly", function() { + // Append an additional span to the end. + document.body.innerHTML += "<p>This is the <span>Test 2</span>.</p>"; -test("Finding elements correctly through findOnce()", function() { - // Create one once() call. - $('#test5 span').once('test5'); + // Create one once() call. + $("span").once("test5").data("foo", "bar"); - // Find the once'd element through the callback. - var elements = $('span').findOnce('test5').each(function() { - var hasData = $(this).data('jquery-once-test5'); - ok(hasData, 'Finding the correct span element after once() through callback.'); + // Find the once'd elements. + $("span").findOnce("test5").each(function() { + assert.equal($(this).data("foo"), "bar", "Found correct span data."); + }); }); - // Find the once'd element without the callback. - elements = $('span').findOnce('test5'); - elements.each(function() { - var hasData = $(this).data('jquery-once-test5'); - ok(hasData, 'Finding the correct span element after once().'); - }); }); -*/ |