[extensions-web] js: Fix and add tests for the paginator



commit 004980ea88af54ac929546c1b0294b444f36cb32
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Tue Apr 10 13:37:20 2012 -0400

    js: Fix and add tests for the paginator

 sweettooth/static/js/paginatorUtils.js      |   11 +++++-
 sweettooth/static/js/test/paginatorUtils.js |   49 +++++++++++++++++++++++++++
 sweettooth/static/js/test/tests-main.js     |    2 +-
 3 files changed, 59 insertions(+), 3 deletions(-)
---
diff --git a/sweettooth/static/js/paginatorUtils.js b/sweettooth/static/js/paginatorUtils.js
index b81a392..4698bd7 100644
--- a/sweettooth/static/js/paginatorUtils.js
+++ b/sweettooth/static/js/paginatorUtils.js
@@ -21,12 +21,19 @@ define(['jquery', 'hashParamUtils'], function($, hashParamUtils) {
         var contextLeft = Math.max(number-context, 2);
         var contextRight = Math.min(number+context+1, numPages);
 
+        // If we're showing the ellipsis to emit one number,
+        // just show the number instead.
+        if (contextLeft === 3)
+            contextLeft = 2;
+        if (contextRight === numPages - 1)
+            contextRight = numPages;
+
         var $elem = $('<div>', {'class': 'paginator-content'});
 
         if (number > 1) {
             makeLink(number-1, 'prev', '\u00ab').appendTo($elem);
             makeLink(1, 'first').appendTo($elem);
-            if (number-context > 2)
+            if (contextLeft > 3)
                 $elem.append($('<span>', {'class': 'ellipses'}).text("..."));
 
             for (var i = contextLeft; i < number; i++)
@@ -39,7 +46,7 @@ define(['jquery', 'hashParamUtils'], function($, hashParamUtils) {
             for (var i = number+1; i < contextRight; i++)
                 makeLink(i).appendTo($elem);
 
-            if (numPages - (number+context) > 2)
+            if (numPages - contextRight > 1)
                 $elem.append($('<span>', {'class': 'ellipses'}).text("..."));
 
             makeLink(numPages, 'last').appendTo($elem);
diff --git a/sweettooth/static/js/test/paginatorUtils.js b/sweettooth/static/js/test/paginatorUtils.js
new file mode 100644
index 0000000..eb325f0
--- /dev/null
+++ b/sweettooth/static/js/test/paginatorUtils.js
@@ -0,0 +1,49 @@
+require(['paginatorUtils', 'jquery', 'test/qunit'], function(paginatorUtils) {
+
+    function validateModel(page, numPages, context, expected) {
+        var model = [];
+        var html = paginatorUtils.buildPaginator(page, numPages, context);
+        html.find('.paginator-content').children().each(function() {
+            if ($(this).hasClass("prev") || $(this).hasClass("next"))
+                return;
+
+            model.push($(this).text());
+        });
+        deepEqual(model, expected);
+    }
+
+    test("testModel", function() {
+        validateModel(1, 1, 3, ['1']);
+        validateModel(1, 2, 3, ['1', '2']);
+        validateModel(1, 3, 3, ['1', '2', '3']);
+        validateModel(1, 4, 3, ['1', '2', '3', '4']);
+        validateModel(1, 5, 3, ['1', '2', '3', '4', '5']);
+        validateModel(1, 6, 3, ['1', '2', '3', '4', '5', '6']);
+        validateModel(1, 7, 3, ['1', '2', '3', '4', '...', '7']);
+        validateModel(1, 8, 3, ['1', '2', '3', '4', '...', '8']);
+        validateModel(1, 9, 3, ['1', '2', '3', '4', '...', '9']);
+
+        validateModel(1, 5, 3, ['1', '2', '3', '4', '5']);
+        validateModel(2, 5, 3, ['1', '2', '3', '4', '5']);
+        validateModel(3, 5, 3, ['1', '2', '3', '4', '5']);
+        validateModel(4, 5, 3, ['1', '2', '3', '4', '5']);
+        validateModel(5, 5, 3, ['1', '2', '3', '4', '5']);
+
+        validateModel(3, 10, 3, ['1', '2', '3', '4', '5', '6', '...', '10']);
+        validateModel(4, 10, 3, ['1', '2', '3', '4', '5', '6', '7', '...', '10']);
+        validateModel(5, 10, 3, ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']);
+        validateModel(6, 10, 3, ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']);
+        validateModel(7, 10, 3, ['1', '...', '4', '5', '6', '7', '8', '9', '10']);
+
+        validateModel(1, 1, 2, ['1']);
+        validateModel(1, 2, 2, ['1', '2']);
+        validateModel(1, 3, 2, ['1', '2', '3']);
+        validateModel(1, 4, 2, ['1', '2', '3', '4']);
+        validateModel(1, 5, 2, ['1', '2', '3', '4', '5']);
+        validateModel(1, 6, 2, ['1', '2', '3', '...', '6']);
+        validateModel(1, 7, 2, ['1', '2', '3', '...', '7']);
+        validateModel(1, 8, 2, ['1', '2', '3', '...', '8']);
+        validateModel(1, 9, 2, ['1', '2', '3', '...', '9']);
+    });
+
+});
diff --git a/sweettooth/static/js/test/tests-main.js b/sweettooth/static/js/test/tests-main.js
index cc046db..c7ab6ba 100644
--- a/sweettooth/static/js/test/tests-main.js
+++ b/sweettooth/static/js/test/tests-main.js
@@ -1 +1 @@
-require(['test/extensions']);
+require(['test/extensions', 'test/paginatorUtils']);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]