ba0249c5 by Adam Heath

CLose to full test coverage.

1 parent 9542d73d
...@@ -2,26 +2,33 @@ define(function(require) { ...@@ -2,26 +2,33 @@ define(function(require) {
2 'use strict'; 2 'use strict';
3 var Backbone = require('backbone'); 3 var Backbone = require('backbone');
4 4
5 function preventDefault(e) {
6 if (e) {
7 e.preventDefault();
8 }
9 }
5 function stepFalse(e) { 10 function stepFalse(e) {
6 e.preventDefault(); 11 preventDefault(e);
7 return false; 12 return false;
8 } 13 }
9 var Pagination = Backbone.Model.extend({ 14 var Pagination = Backbone.Model.extend({
10 defaults: { 15 defaults: function defaults() {
11 currentPage: 1, 16 return {
12 hasNext: false, 17 currentPage: 1,
13 hasNextJumpPage: false, 18 hasNext: false,
14 hasPrevious: false, 19 hasNextJumpPage: false,
15 hasPreviousJumpPage: false, 20 hasPrevious: false,
16 nextJumpPage: undefined, 21 hasPreviousJumpPage: false,
17 nextPage: undefined, 22 nextJumpPage: undefined,
18 pages: [], 23 nextPage: undefined,
19 pageJump: 5, 24 pages: [],
20 pageSize: 10, 25 pageJump: 5,
21 previousJumpPage: undefined, 26 pageSize: 10,
22 previousPage: undefined, 27 previousJumpPage: undefined,
23 totalCount: 0, 28 previousPage: undefined,
24 totalPages: 0, 29 totalCount: 0,
30 totalPages: 0,
31 };
25 }, 32 },
26 initialize: function(data, options) { 33 initialize: function(data, options) {
27 var buildPages = function buildPages() { 34 var buildPages = function buildPages() {
...@@ -55,8 +62,8 @@ define(function(require) { ...@@ -55,8 +62,8 @@ define(function(require) {
55 startAt = 1; 62 startAt = 1;
56 } 63 }
57 if (endAt - startAt < 9) { 64 if (endAt - startAt < 9) {
58 /* global console:false */ 65 ///* global console:false */
59 console.log('foo'); 66 //console.log('foo');
60 } 67 }
61 68
62 for (var i = startAt; i < endAt; i++) { 69 for (var i = startAt; i < endAt; i++) {
...@@ -90,6 +97,21 @@ define(function(require) { ...@@ -90,6 +97,21 @@ define(function(require) {
90 totalPages: totalPages, 97 totalPages: totalPages,
91 }); 98 });
92 }; 99 };
100 this.on('change:pageSize change:currentPage change:totalCount', function() {
101 var currentPage = parseInt(this.get('currentPage'));
102 var pageSize = parseInt(this.get('pageSize'));
103 var totalCount = parseInt(this.get('totalCount'));
104 if (!currentPage || !pageSize || !totalCount) {
105 return;
106 }
107 var pages = [];
108 var totalPages = Math.floor((totalCount + pageSize - 1) / pageSize);
109 if (currentPage < 0) {
110 this.set('currentPage', 1);
111 } else if (currentPage > totalPages) {
112 this.set('currentPage', totalPages);
113 }
114 }, this);
93 this.on('change:pageSize change:currentPage change:totalCount', buildPages, this); 115 this.on('change:pageSize change:currentPage change:totalCount', buildPages, this);
94 var installActions = _.bind(function installActions(eventName, nextName, hasNextName, previousName, hasPreviousName, pageCount) { 116 var installActions = _.bind(function installActions(eventName, nextName, hasNextName, previousName, hasPreviousName, pageCount) {
95 this.on(eventName, function() { 117 this.on(eventName, function() {
...@@ -98,7 +120,7 @@ define(function(require) { ...@@ -98,7 +120,7 @@ define(function(require) {
98 var next, previous; 120 var next, previous;
99 if (hasNext) { 121 if (hasNext) {
100 next = _.bind(function(e) { 122 next = _.bind(function(e) {
101 e.preventDefault(); 123 preventDefault(e);
102 this.set('currentPage', this.get('currentPage') + pageCount); 124 this.set('currentPage', this.get('currentPage') + pageCount);
103 return false; 125 return false;
104 }, this); 126 }, this);
...@@ -107,7 +129,7 @@ define(function(require) { ...@@ -107,7 +129,7 @@ define(function(require) {
107 } 129 }
108 if (hasPrevious) { 130 if (hasPrevious) {
109 previous = _.bind(function(e) { 131 previous = _.bind(function(e) {
110 e.preventDefault(); 132 preventDefault(e);
111 this.set('currentPage', this.get('currentPage') - pageCount); 133 this.set('currentPage', this.get('currentPage') - pageCount);
112 return false; 134 return false;
113 }, this); 135 }, this);
......
1 define(function(require) {
2 'use strict';
3 var _ = require('underscore');
4 var Backbone = require('backbone');
5 var Pagination = require('solr/model/Pagination');
6
7 describe('Pagination', function() {
8 it('loads', function() {
9 expect(Pagination).toBeDefined();
10 });
11 describe(':pages', function() {
12 it('default value is not shared globally', function() {
13 var p1 = new Pagination(), p2 = new Pagination();
14 expect(p1.get('pages')).not.toBe(p2.get('pages'));
15 });
16 });
17 describe('singleton', function() {
18 var p;
19
20 function checkHas(hasNext, hasNextJump, hasNextJumpPage, hasPrevious, hasPreviousJump, hasPreviousJumpPage) {
21 var wanted = {
22 hasNext: hasNext,
23 hasNextJump: hasNextJump,
24 hasNextJumpPage: hasNextJumpPage,
25 hasPrevious: hasPrevious,
26 hasPreviousJump: hasPreviousJump,
27 hasPreviousJumpPage: hasPreviousJumpPage,
28 };
29 it('has', function() {
30 var got = {
31 hasNext: p.get('hasNext'),
32 hasNextJump: p.get('hasNextJump'),
33 hasNextJumpPage: p.get('hasNextJumpPage'),
34 hasPrevious: p.get('hasPrevious'),
35 hasPreviousJump: p.get('hasPreviousJump'),
36 hasPreviousJumpPage: p.get('hasPreviousJumpPage'),
37 };
38 expect(got).toEqual(wanted);
39 });
40 }
41 function checkPages(wantedLength, offset, currentPage) {
42 var pages;
43 beforeEach(function() {
44 pages = p.get('pages');
45 });
46 //console.log(JSON.stringify(pages));
47 var wanted = { length: wantedLength, pages: [] };
48 for (var i = 0; i < wantedLength; i++) {
49 wanted.pages[i] = { current: i + 1 === currentPage, number: i + 1 + offset };
50 }
51 it('pages', function() {
52 pages = p.get('pages');
53 var got = { length: pages.length, pages: [] };
54 for (var i = 0; i < pages.length; i++) {
55 var page = pages[i];
56 got.pages[i] = { current: page.current, number: page.number };
57 }
58 expect(got).toEqual(wanted);
59 });
60 }
61
62 beforeEach(function() {
63 p = new Pagination();
64 });
65 describe('default settings', function() {
66 checkHas(false, undefined, false, false, undefined, false);
67 checkPages(0, 0, 1);
68 });
69 describe('35 items', function() {
70 beforeEach(function() {
71 p.set({totalCount:35});
72 });
73 describe('initial settings', function() {
74 checkHas(true, false, false, false, false, false);
75 checkPages(4, 0, 1);
76 });
77 describe('clamping', function() {
78 describe('set currentPage=5', function() {
79 beforeEach(function() {
80 p.set({currentPage: 5});
81 });
82 checkHas(false, false, false, true, false, false);
83 checkPages(4, 0, 4);
84 });
85 describe('set currentPage=-1', function() {
86 beforeEach(function() {
87 p.set({currentPage: -1});
88 });
89 checkHas(true, false, false, false, false, false);
90 checkPages(4, 0, 1);
91 });
92 });
93 describe('next[1]', function() {
94 beforeEach(function() {
95 p.next();
96 });
97 checkPages(4, 0, 2);
98 describe('previous', function() {
99 beforeEach(function() {
100 p.previous();
101 });
102 checkHas(true, false, false, false, false, false);
103 checkPages(4, 0, 1);
104 });
105 describe('next[2]', function() {
106 beforeEach(function() {
107 p.next();
108 p.next();
109 });
110 checkHas(false, false, false, true, false, false);
111 checkPages(4, 0, 4);
112 describe('next[1] - clamp', function() {
113 beforeEach(function() {
114 p.next();
115 });
116 checkHas(false, false, false, true, false, false);
117 checkPages(4, 0, 4);
118 });
119 describe('page[0].jump', function() {
120 beforeEach(function() {
121 p.get('pages')[0].jump();
122 });
123 checkHas(true, false, false, false, false, false);
124 checkPages(4, 0, 1);
125 });
126 describe('page[3].jump', function() {
127 beforeEach(function() {
128 p.get('pages')[3].jump();
129 });
130 checkHas(false, false, false, true, false, false);
131 checkPages(4, 0, 4);
132 });
133 describe('decrease to 25 items', function() {
134 beforeEach(function() {
135 p.set({totalCount:25});
136 });
137 checkHas(false, false, false, true, false, false);
138 checkPages(3, 0, 3);
139 describe('increase to 150 items', function() {
140 beforeEach(function() {
141 p.set({totalCount:150});
142 });
143 checkHas(true, true, false, true, false, false);
144 checkPages(9, 0, 3);
145 });
146 });
147 });
148 });
149 describe('previous[1]', function() {
150 beforeEach(function() {
151 p.previous();
152 });
153 checkHas(true, false, false, false, false, false);
154 checkPages(4, 0, 1);
155 });
156 });
157 describe('150 items', function() {
158 beforeEach(function() {
159 p.set({totalCount:150});
160 });
161 checkHas(true, true, false, false, false, false);
162 checkPages(9, 0, 1);
163 describe('page[7].jump', function() {
164 beforeEach(function() {
165 p.get('pages')[7].jump();
166 });
167 checkHas(true, true, false, true, true, false);
168 checkPages(9, 3, 5);
169 describe('previousJump', function() {
170 beforeEach(function() {
171 p.previousJump();
172 });
173 checkHas(true, true, false, true, false, false);
174 checkPages(9, 0, 3);
175 });
176 });
177 });
178 describe('no page jump', function() {
179 beforeEach(function() {
180 p.set({pageJump: false});
181 p.set({totalCount:150});
182 });
183 checkHas(true, false, false, false, false, false);
184 checkPages(9, 0, 1);
185 });
186 });
187 });
188 });