can3p (can3p) wrote in changelog,
can3p
can3p
changelog

[ljcom] r11680: LJSUP-11421: add tests for LJ.throttle f...

Committer: dpetrov
LJSUP-11421: add tests for LJ.throttle function
A   trunk/htdocs/js/test/
A   trunk/htdocs/js/test/basic.js
A   trunk/htdocs/js/test/test.html
Added: trunk/htdocs/js/test/basic.js
===================================================================
--- trunk/htdocs/js/test/basic.js	                        (rev 0)
+++ trunk/htdocs/js/test/basic.js	2012-04-06 12:16:23 UTC (rev 11680)
@@ -0,0 +1,47 @@
+module("Basic.js");
+
+asyncTest("functions: throttle", 2, function() {
+	var counter = 0;
+	var incr = function(){ counter++; };
+	var throttledIncr = LJ.throttle(incr, 100);
+	throttledIncr(); throttledIncr(); throttledIncr();
+	setTimeout(throttledIncr, 70);
+	setTimeout(throttledIncr, 120);
+	setTimeout(throttledIncr, 140);
+	setTimeout(throttledIncr, 190);
+	setTimeout(throttledIncr, 220);
+	setTimeout(throttledIncr, 240);
+	setTimeout(function(){ equal(counter, 1, "incr was called immediately"); }, 30);
+	setTimeout(function(){ equal(counter, 4, "incr was throttled"); start(); }, 400);
+});
+
+asyncTest("functions: throttle arguments", 2, function() {
+	var value = 0;
+	var update = function(val){ value = val; };
+	var throttledUpdate = LJ.throttle(update, 100);
+	throttledUpdate(1); throttledUpdate(2); throttledUpdate(3);
+	setTimeout(function(){ throttledUpdate(4); }, 120);
+	setTimeout(function(){ throttledUpdate(5); }, 140);
+	setTimeout(function(){ throttledUpdate(6); }, 250);
+	setTimeout(function(){ equal(value, 1, "updated to latest value"); }, 40);
+	setTimeout(function(){ equal(value, 6, "updated to latest value"); start(); }, 400);
+});
+
+asyncTest("functions: throttle once", 2, function() {
+	var counter = 0;
+	var incr = function(){ return ++counter; };
+	var throttledIncr = LJ.throttle(incr, 100);
+	var result = throttledIncr();
+	setTimeout(function(){
+		equal(result, 1, "throttled functions return their value");
+		equal(counter, 1, "incr was called once"); start();
+	}, 220);
+});
+
+asyncTest("functions: throttle twice", 1, function() {
+	var counter = 0;
+	var incr = function(){ counter++; };
+	var throttledIncr = LJ.throttle(incr, 100);
+	throttledIncr(); throttledIncr();
+	setTimeout(function(){ equal(counter, 2, "incr was called twice"); start(); }, 220);
+});

Added: trunk/htdocs/js/test/test.html
===================================================================
--- trunk/htdocs/js/test/test.html	                        (rev 0)
+++ trunk/htdocs/js/test/test.html	2012-04-06 12:16:23 UTC (rev 11680)
@@ -0,0 +1,20 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <title>LiveJournal Test Suite</title>
+  <link rel="stylesheet" href="../qunit/qunit.css" type="text/css" media="screen" />
+  <script type="text/javascript" src="../jquery.js"></script>
+  <script type="text/javascript" src="../qunit/qunit.js"></script>
+  <script type="text/javascript" src="../basic.js"></script>
+  <script type="text/javascript" src="basic.js"></script>
+</head>
+<body>
+  <div class="underscore-test">
+    <h1 id="qunit-header">LiveJournal Test Suite</h1>
+    <div id="qunit-testrunner-toolbar"></div>
+    <h2 id="qunit-banner"></h2>
+    <h2 id="qunit-userAgent"></h2>
+    <ol id="qunit-tests"></ol>
+  </div>
+</body>
+</html>

Tags: can3p, dpetrov, html, js, ljcom
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments