summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/index.html30
-rw-r--r--test/test.js45
2 files changed, 75 insertions, 0 deletions
diff --git a/test/index.html b/test/index.html
new file mode 100644
index 0000000..adc06ba
--- /dev/null
+++ b/test/index.html
@@ -0,0 +1,30 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>jQuery Once - Test Suite</title>
+ <link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.11.0.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>
+
+ <script src="http://code.jquery.com/jquery-1.10.0.js"></script>
+ <script src="http://code.jquery.com/qunit/qunit-1.11.0.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
new file mode 100644
index 0000000..533c532
--- /dev/null
+++ b/test/test.js
@@ -0,0 +1,45 @@
+test("Properly executed", function() {
+ // Create one once() call.
+ $('#test1 span').once(function() {
+ $(this).data('test1', 'foobar');
+ });
+
+ var data = $('#test1 span').data('test1');
+ ok(data === "foobar");
+});
+
+test("Called only once", 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', callback);
+ }
+
+ // Verify that it was only called once.
+ var count = $('#test2 span').data('count');
+ ok(count === 1);
+});
+
+test("Apply the class correctly", function() {
+ // Verify that the element starts without the class.
+ var hasClass = $('#test3 span').hasClass('test3-processed');
+ ok(!hasClass, 'Processed class not applied in the beginning.');
+
+ // Create one once() call.
+ $('#test3 span').once('test3', function() {
+ // Do nothing.
+ });
+
+ // Verify the class is applied.
+ hasClass = $('#test3 span').hasClass('test3-processed');
+ ok(hasClass, 'The processed class is properly applied after once().');
+});