Adds unit test framework and a first unit test.
[quanweb.git] / js / test / BooksModelTest.js
1 'use strict';
2
3 describe('Scroll forward should not advance beyond end', function() {
4     var bm = BooksModel;
5     var calledRefreshData;
6
7     //       [first, last, pageSize, count, setting, expectFirst, expectLast, expectRefresh]
8     data = [ [    0,  19 ,       20,   100,      20,         20 ,        39 ,          true ],
9              [    0,  19 ,       20,   100,     100,         80 ,        99 ,          true ],
10              [    0,  19 ,       20,   100,      80,         80 ,        99 ,          true ],
11              [    0,  19 ,       20,   100,      79,         79 ,        98 ,          true ],
12              [   79,  99 ,       20,   100,      50,         50 ,        69 ,          true ],
13              [    0,  11 ,       20,    12,      19,          0 ,        11 ,         false ],
14              [    0, (-1),       20,     0,      20,          0 ,       (-1),         false ]
15            ];
16
17     function doTest(datum) {
18         bm.first = datum[0];
19         bm.last = datum[1];
20         bm.pageSize = datum[2];
21         bm.count = datum[3];
22
23         var oldFunc = bm.refreshData;
24         calledRefreshData = false;
25         try {
26             bm.refreshData = function() {
27                 calledRefreshData = true;
28             };
29
30             bm.adjustPos(datum[4]);
31         }
32         finally {
33             bm.refreshData = oldFunc;
34         }
35     }
36
37     it('should stay within valid range, and call refreshData() if bm.first changes', function() {
38         var i = 4;
39         for (i = 0; i < data.length; i += 1) {
40             datum = data[i];
41             doTest(datum);
42             expect(bm.first).toBe(datum[5]);
43             expect(bm.last).toBe(datum[6]);
44             expect(calledRefreshData).toBe(datum[7]);
45         }
46     });
47 });
48
49