diff options
-rw-r--r-- | .eslintrc | 3 | ||||
-rw-r--r-- | Gruntfile.js | 17 | ||||
-rw-r--r-- | package.json | 13 | ||||
-rw-r--r-- | test/test.js | 144 |
4 files changed, 98 insertions, 79 deletions
@@ -2,7 +2,8 @@ "env": { "browser": true, "node": true, - "amd": true + "amd": true, + "mocha": true }, "globals": { "jQuery": true diff --git a/Gruntfile.js b/Gruntfile.js deleted file mode 100644 index af1d6f7..0000000 --- a/Gruntfile.js +++ /dev/null @@ -1,17 +0,0 @@ -/*jshint node:true*/ - -module.exports = function(grunt) { - grunt.initConfig({ - pkg: grunt.file.readJSON('package.json'), - qunit: { - files: [ - 'test/index.html' - ] - } - }); - - grunt.loadNpmTasks('grunt-contrib-qunit'); - - grunt.registerTask('default', ['qunit']); - -}; diff --git a/package.json b/package.json index adf2094..194948c 100644 --- a/package.json +++ b/package.json @@ -51,15 +51,16 @@ }, "devDependencies": { "eslint": "~0.11.0-alpha.0", - "grunt": "~0.4.5", - "grunt-contrib-qunit": "~0.5.2", + "jsdom": "^2.0.0", + "mocha": "*", + "mocha-jsdom": "^0.2.0", "projectz": "~0.3.15", - "uglify-js": "~2.4.15", - "qunitjs": "~1.16.0" + "qunitjs": "~1.16.0", + "uglify-js": "~2.4.15" }, "scripts": { - "pretest": "./node_modules/.bin/eslint jquery.once.js", - "test": "grunt", + "pretest": "./node_modules/.bin/eslint jquery.once.js test/test.js", + "test": "./node_modules/.bin/mocha", "projectz": "./node_modules/.bin/projectz compile", "release": "./node_modules/.bin/uglifyjs jquery.once.js -o jquery.once.min.js --comments --source-map jquery.once.min.js.map --mangle", "jsdoc": "jsdoc jquery.once.js README.md" diff --git a/test/test.js b/test/test.js index 4f35b66..c462a27 100644 --- a/test/test.js +++ b/test/test.js @@ -1,67 +1,100 @@ -test("String ID required", function() { - expect(1); - try { - $("#test1 span").once(function () {}); - } - catch (e) { - ok(e, "Error is triggered when ID is not a string."); - } -}); +var jsdom = require("mocha-jsdom"); +var assert = require("assert"); + +describe("jQuery Once", function() { + "use strict"; + + /** + * The global instance of jQuery. + */ + var $; + + /** + * Turn the Mocha test environment into a DOM environment with JSDom. + */ + jsdom(); + + /** + * Before the tests initiate, load jQuery and jQuery Once. + */ + before(function() { + $ = require("jquery"); + require("../jquery.once.js"); + }); -test(".once('test1-2') properly executed", function() { - // Create one once('test1-2') call. - $('#test1 span').once('test1-2').data('test1-2', 'foo'); + it("should require ID to be a string", function () { + // Build the body HTML. + document.body.innerHTML = "<p>This is <span>Test 1</span>.</p>"; - // Create another once('test1-2') call. - $('#test1 span').once('test1-2').data('test1-2', 'bar'); + // Expect it to throw an error. + assert.throws(function() { + $("span").once(function () { + // Nothing. + }); + }); + }); - // The data should result to the first once() call. - var data = $('#test1 span').data('test1-2'); - ok(data === "foo"); -}); + it("properly executes .once('test2')", function () { + // Prepare the document body. + document.body.innerHTML = "<p>This is <span>Test 2</span>.</p>"; -test("Called only once with name, counted", function() { - // Count the number of times once() was called. - $('#test2 span').data('count', 0); - - // Create the once() callback. - var callback = function() { - var count = $('#test2 span').data('count'); - count++; - $('#test2 span').data('count', count); - }; - - // Call once() a bunch of times. - for (var i = 0; i < 10; i++) { - $('#test2 span').once('count').each(callback); - } - - // Verify that it was only called once. - var count = $('#test2 span').data('count'); - ok(count === 1, 'It was called ' + count + ' times.'); -}); + // Create one once('test2') call. + $("span").once("test2").data("test2", "foo"); + + // Create another once('test2') call. + $("span").once("test2").data("test2", "bar"); + + // The data should result to the first once() call. + var data = $("span").data("test2"); + assert.equal(data, "foo"); + }); + + 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); -test("Called only once without name, counted", function() { - // Count the number of times once() was called. - $('#test2 span').data('once', 0); + // Create the once() callback. + var callback = function() { + // Increment the count variable stored in the data. + $("span").data("count", $("span").data("count") + 1); + }; - // Create the once() callback. - var callback = function() { + // Call once() a bunch of times. + for (var i = 0; i < 10; i++) { + $("span").once("count").each(callback); + } + + // Verify that it was only called once. + var count = $("span").data("count"); + assert.equal(count, 1, "It was called " + count + " times."); + }); + + it("is called only once without an ID", function() { + // Count the number of times once() was called. + $("span").data("once", 0); + + // Create the once() callback. + var callback = function() { + $("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); + } + + // Verify that it was only called once. var count = $('#test2 span').data('once'); - count++; - $('#test2 span').data('once', count); - }; - - // Call once() a bunch of times. - for (var i = 0; i < 10; i++) { - $('#test2 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.'); + ok(count === 1, 'It was called ' + count + ' times.'); + }); + }); +/* + test("Apply the value to data correctly", function() { // Verify that the element starts without the class. var hasData = $('#test3 span').data('jquery-once-test3'); @@ -106,3 +139,4 @@ test("Finding elements correctly through findOnce()", function() { ok(hasData, 'Finding the correct span element after once().'); }); }); +*/ |