Adds unit test framework and a first unit test.
[quanweb.git] / js / test / BooksModelTest.js
diff --git a/js/test/BooksModelTest.js b/js/test/BooksModelTest.js
new file mode 100644 (file)
index 0000000..38997e4
--- /dev/null
@@ -0,0 +1,49 @@
+'use strict';
+
+describe('Scroll forward should not advance beyond end', function() {
+    var bm = BooksModel;
+    var calledRefreshData;
+
+    //       [first, last, pageSize, count, setting, expectFirst, expectLast, expectRefresh]
+    data = [ [    0,  19 ,       20,   100,      20,         20 ,        39 ,          true ],
+             [    0,  19 ,       20,   100,     100,         80 ,        99 ,          true ],
+             [    0,  19 ,       20,   100,      80,         80 ,        99 ,          true ],
+             [    0,  19 ,       20,   100,      79,         79 ,        98 ,          true ],
+             [   79,  99 ,       20,   100,      50,         50 ,        69 ,          true ],
+             [    0,  11 ,       20,    12,      19,          0 ,        11 ,         false ],
+             [    0, (-1),       20,     0,      20,          0 ,       (-1),         false ]
+           ];
+
+    function doTest(datum) {
+        bm.first = datum[0];
+        bm.last = datum[1];
+        bm.pageSize = datum[2];
+        bm.count = datum[3];
+
+        var oldFunc = bm.refreshData;
+        calledRefreshData = false;
+        try {
+            bm.refreshData = function() {
+                calledRefreshData = true;
+            };
+
+            bm.adjustPos(datum[4]);
+        }
+        finally {
+            bm.refreshData = oldFunc;
+        }
+    }
+
+    it('should stay within valid range, and call refreshData() if bm.first changes', function() {
+        var i = 4;
+        for (i = 0; i < data.length; i += 1) {
+            datum = data[i];
+            doTest(datum);
+            expect(bm.first).toBe(datum[5]);
+            expect(bm.last).toBe(datum[6]);
+            expect(calledRefreshData).toBe(datum[7]);
+        }
+    });
+});
+
+