PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/test/extensions/paginator.js

https://github.com/antonyraj/backgrid
JavaScript | 669 lines | 541 code | 106 blank | 22 comment | 0 complexity | 2810e7b29c598002cee1bb454e06742f MD5 | raw file
Possible License(s): MIT
  1. /*
  2. backgrid
  3. http://github.com/wyuenho/backgrid
  4. Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors
  5. Licensed under the MIT @license.
  6. */
  7. describe("A PageHandle", function () {
  8. var collection;
  9. beforeEach(function () {
  10. collection = new Backbone.PageableCollection([
  11. {id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}
  12. ], {
  13. state: {
  14. pageSize: 2
  15. },
  16. mode: "client"
  17. });
  18. });
  19. describe("when under control mode", function () {
  20. it("can render a disabled rewind handle if current page = first page", function () {
  21. var handle = new Backgrid.Extension.PageHandle({
  22. collection: collection,
  23. isRewind: true,
  24. label: "first",
  25. title: "first"
  26. });
  27. handle.render();
  28. expect(handle.$el.hasClass("disabled")).toBe(true);
  29. expect(handle.$("a").attr("title")).toBe("first");
  30. expect(handle.$("a").text()).toBe("first");
  31. });
  32. it("can render a rewind handle if current page != first page", function () {
  33. collection.getPage(2);
  34. var handle = new Backgrid.Extension.PageHandle({
  35. collection: collection,
  36. isRewind: true,
  37. label: "first",
  38. title: "first"
  39. });
  40. handle.render();
  41. expect(handle.$el.hasClass("disabled")).toBe(false);
  42. expect(handle.$("a").attr("title")).toBe("first");
  43. expect(handle.$("a").text()).toBe("first");
  44. });
  45. it("the rewind handle will get first page on click", function () {
  46. collection.getPage(2);
  47. var handle = new Backgrid.Extension.PageHandle({
  48. collection: collection,
  49. isRewind: true,
  50. label: "first",
  51. title: "first"
  52. });
  53. handle.render();
  54. handle.$("a").click();
  55. expect(collection.state.currentPage).toBe(1);
  56. });
  57. it("can render a disabled back handle if current page = first page", function () {
  58. var handle = new Backgrid.Extension.PageHandle({
  59. collection: collection,
  60. isBack: true,
  61. label: "back",
  62. title: "back"
  63. });
  64. handle.render();
  65. expect(handle.$el.hasClass("disabled")).toBe(true);
  66. expect(handle.$("a").attr("title")).toBe("back");
  67. expect(handle.$("a").text()).toBe("back");
  68. });
  69. it("can render a back handle if current page != first page", function () {
  70. collection.getPage(2);
  71. var handle = new Backgrid.Extension.PageHandle({
  72. collection: collection,
  73. isBack: true,
  74. label: "back",
  75. title: "back"
  76. });
  77. handle.render();
  78. expect(handle.$el.hasClass("disabled")).toBe(false);
  79. expect(handle.$("a").attr("title")).toBe("back");
  80. expect(handle.$("a").text()).toBe("back");
  81. });
  82. it("the back handle will get the previous page on click", function () {
  83. collection.getPage(3);
  84. var handle = new Backgrid.Extension.PageHandle({
  85. collection: collection,
  86. isBack: true,
  87. label: "back",
  88. title: "back"
  89. });
  90. handle.render();
  91. handle.$("a").click();
  92. expect(collection.state.currentPage).toBe(2);
  93. });
  94. it("can render a disabled forward handle if current page = last page", function () {
  95. collection.getLastPage();
  96. var handle = new Backgrid.Extension.PageHandle({
  97. collection: collection,
  98. isForward: true,
  99. label: "next",
  100. title: "next"
  101. });
  102. handle.render();
  103. expect(handle.$el.hasClass("disabled")).toBe(true);
  104. expect(handle.$("a").attr("title")).toBe("next");
  105. expect(handle.$("a").text()).toBe("next");
  106. });
  107. it("can render a forward handle if current page != first page", function () {
  108. var handle = new Backgrid.Extension.PageHandle({
  109. collection: collection,
  110. isForward: true,
  111. label: "next",
  112. title: "next"
  113. });
  114. handle.render();
  115. expect(handle.$el.hasClass("disabled")).toBe(false);
  116. expect(handle.$("a").attr("title")).toBe("next");
  117. expect(handle.$("a").text()).toBe("next");
  118. });
  119. it("the forward handle will get next page on click", function () {
  120. var handle = new Backgrid.Extension.PageHandle({
  121. collection: collection,
  122. isForward: true,
  123. label: "next",
  124. title: "next"
  125. });
  126. handle.render();
  127. handle.$("a").click();
  128. expect(collection.state.currentPage).toBe(2);
  129. });
  130. it("can render a disabled fast forward handle if current page = last page", function () {
  131. collection.getLastPage();
  132. var handle = new Backgrid.Extension.PageHandle({
  133. collection: collection,
  134. isFastForward: true,
  135. label: "last",
  136. title: "last"
  137. });
  138. handle.render();
  139. expect(handle.$el.hasClass("disabled")).toBe(true);
  140. expect(handle.$("a").attr("title")).toBe("last");
  141. expect(handle.$("a").text()).toBe("last");
  142. });
  143. it("can render a fast forward handle if current page != first page", function () {
  144. var handle = new Backgrid.Extension.PageHandle({
  145. collection: collection,
  146. isFastForward: true,
  147. label: "last",
  148. title: "last"
  149. });
  150. handle.render();
  151. expect(handle.$el.hasClass("disabled")).toBe(false);
  152. expect(handle.$("a").attr("title")).toBe("last");
  153. expect(handle.$("a").text()).toBe("last");
  154. });
  155. it("the fast forward handle will get last page on click", function () {
  156. collection.getPage(2);
  157. var handle = new Backgrid.Extension.PageHandle({
  158. collection: collection,
  159. isFastForward: true,
  160. label: "last",
  161. title: "last"
  162. });
  163. handle.render();
  164. handle.$("a").click();
  165. expect(collection.state.currentPage).toBe(collection.state.lastPage);
  166. });
  167. });
  168. describe("when under discrete mode", function () {
  169. it("renders a 1-based label based on a 0-based pageIndex", function () {
  170. var handle = new Backgrid.Extension.PageHandle({
  171. collection: collection,
  172. pageIndex: 0
  173. });
  174. handle.render();
  175. expect(handle.$("a").text()).toBe("1");
  176. });
  177. it("renders a label independently of the pageIndex if one is given", function () {
  178. var handle = new Backgrid.Extension.PageHandle({
  179. collection: collection,
  180. pageIndex: 0,
  181. label: "a"
  182. });
  183. handle.render();
  184. expect(handle.$("a").text()).toBe("a");
  185. });
  186. it("renders a default title", function () {
  187. var handle = new Backgrid.Extension.PageHandle({
  188. collection: collection,
  189. pageIndex: 0
  190. });
  191. handle.render();
  192. expect(handle.$("a").attr("title")).toBe("Page 1");
  193. });
  194. it("renders the title if one is given", function () {
  195. var handle = new Backgrid.Extension.PageHandle({
  196. collection: collection,
  197. pageIndex: 0,
  198. title: "one"
  199. });
  200. handle.render();
  201. expect(handle.$("a").attr("title")).toBe("one");
  202. });
  203. it("renders a title template if one is given", function () {
  204. var handle = new Backgrid.Extension.PageHandle({
  205. collection: collection,
  206. pageIndex: 0,
  207. title: _.template("No. <%- label %>")
  208. });
  209. handle.render();
  210. expect(handle.$("a").attr("title")).toBe("No. 1");
  211. });
  212. it("renders an active page handle if current page = pageIndex", function () {
  213. var handle = new Backgrid.Extension.PageHandle({
  214. collection: collection,
  215. pageIndex: 0
  216. });
  217. handle.render();
  218. expect(handle.$el.hasClass("active")).toBe(true);
  219. expect(handle.$("a").attr("title")).toBe("Page 1");
  220. expect(handle.$("a").text()).toBe("1");
  221. });
  222. it("renders an page handle if current page != pageIndex", function () {
  223. var handle = new Backgrid.Extension.PageHandle({
  224. collection: collection,
  225. pageIndex: 1
  226. });
  227. handle.render();
  228. expect(handle.$el.hasClass("active")).toBe(false);
  229. expect(handle.$("a").attr("title")).toBe("Page 2");
  230. expect(handle.$("a").text()).toBe("2");
  231. });
  232. it("the handle will get the page on click", function () {
  233. var handle = new Backgrid.Extension.PageHandle({
  234. collection: collection,
  235. pageIndex: 1
  236. });
  237. handle.render();
  238. handle.$("a").click();
  239. expect(collection.state.currentPage).toBe(2);
  240. });
  241. });
  242. });
  243. describe("A Paginator", function () {
  244. var collection, paginator;
  245. describe("when under client mode", function () {
  246. beforeEach(function () {
  247. collection = new Backbone.PageableCollection([{id: 1}, {id: 2}, {id: 3}, {id: 4}, {id: 5}], {
  248. state: {
  249. pageSize: 2
  250. },
  251. mode: "client"
  252. });
  253. paginator = new Backgrid.Extension.Paginator({
  254. collection: collection,
  255. columns: [{name: "id", cell: "integer"}]
  256. });
  257. paginator.render();
  258. });
  259. it("#calculateWindow should return the correct start and end bound", function () {
  260. var window = paginator._calculateWindow();
  261. expect(window[0]).toBe(0);
  262. expect(window[1]).toBe(3);
  263. collection.fullCollection.pop();
  264. var window = paginator._calculateWindow();
  265. expect(window[0]).toBe(0);
  266. expect(window[1]).toBe(2);
  267. collection.fullCollection.pop();
  268. var window = paginator._calculateWindow();
  269. expect(window[0]).toBe(0);
  270. expect(window[1]).toBe(2);
  271. collection.fullCollection.shift();
  272. var window = paginator._calculateWindow();
  273. expect(window[0]).toBe(0);
  274. expect(window[1]).toBe(1);
  275. collection.fullCollection.reset();
  276. var window = paginator._calculateWindow();
  277. expect(window[0]).toBe(0);
  278. expect(window[1]).toBe(1);
  279. });
  280. it("renders 1 handle when the collection has <= 1 page", function () {
  281. paginator = new Backgrid.Extension.Paginator({
  282. collection: new Backbone.PageableCollection([], {
  283. state: {
  284. pageSize: 2
  285. },
  286. mode: "client"
  287. }),
  288. columns: [{name: "id", cell: "integer"}]
  289. });
  290. paginator.render();
  291. expect(paginator.$el.find("a").length).toBe(5);
  292. expect(paginator.$el.find("a[title='Page 1']").length).toBe(1);
  293. expect(paginator.$el.find("a[title='Page 2']").length).toBe(0);
  294. paginator = new Backgrid.Extension.Paginator({
  295. collection: new Backbone.PageableCollection([{id: 1}], {
  296. state: {
  297. pageSize: 2
  298. },
  299. mode: "client"
  300. }),
  301. columns: [{name: "id", cell: "integer"}]
  302. });
  303. paginator.render();
  304. expect(paginator.$el.find("a").length).toBe(5);
  305. expect(paginator.$el.find("a[title='Page 1']").length).toBe(1);
  306. expect(paginator.$el.find("a[title='Page 2']").length).toBe(0);
  307. paginator = new Backgrid.Extension.Paginator({
  308. collection: new Backbone.PageableCollection([{id: 1}, {id: 2}], {
  309. state: {
  310. pageSize: 2
  311. },
  312. mode: "client"
  313. }),
  314. columns: [{name: "id", cell: "integer"}]
  315. });
  316. paginator.render();
  317. expect(paginator.$el.find("a").length).toBe(5);
  318. expect(paginator.$el.find("a[title='Page 1']").length).toBe(1);
  319. expect(paginator.$el.find("a[title='Page 2']").length).toBe(0);
  320. });
  321. it("clicking on active or disabled page handles have no effect", function () {
  322. paginator = new Backgrid.Extension.Paginator({
  323. collection: new Backbone.PageableCollection([{id: 1}], {
  324. state: {
  325. pageSize: 1
  326. },
  327. mode: "client"
  328. }),
  329. columns: [{name: "id", cell: "integer"}]
  330. });
  331. paginator.$el.find("a").eq(0).click();
  332. expect(paginator.collection.state.currentPage).toBe(1);
  333. paginator.$el.find("a").eq(1).click();
  334. expect(paginator.collection.state.currentPage).toBe(1);
  335. paginator.$el.find("a").eq(2).click();
  336. expect(paginator.collection.state.currentPage).toBe(1);
  337. paginator.$el.find("a").eq(3).click();
  338. expect(paginator.collection.state.currentPage).toBe(1);
  339. paginator.$el.find("a").eq(4).click();
  340. expect(paginator.collection.state.currentPage).toBe(1);
  341. });
  342. it("has page handles that go to the correct pages when clicked", function () {
  343. // page 2
  344. paginator.$el.find("a").eq(3).click();
  345. expect(collection.state.currentPage).toBe(2);
  346. // page 1
  347. paginator.$el.find("a").eq(2).click();
  348. expect(collection.state.currentPage).toBe(1);
  349. // reset window size and rerender
  350. paginator.windowSize = 1;
  351. paginator.render();
  352. // last page
  353. paginator.$el.find("a").eq(4).click();
  354. expect(paginator.$el.find("a").eq(2).html()).toBe('3');
  355. expect(collection.state.currentPage).toBe(3);
  356. // prev page
  357. paginator.$el.find("a").eq(1).click();
  358. expect(paginator.$el.find("a").eq(2).html()).toBe('2');
  359. expect(collection.state.currentPage).toBe(2);
  360. // 0-based page indices
  361. collection = new Backbone.PageableCollection([{id: 1}, {id: 2}, {id: 3}], {
  362. mode: "client",
  363. state: {
  364. pageSize: 1,
  365. firstPage: 0
  366. }
  367. });
  368. paginator = new Backgrid.Extension.Paginator({
  369. collection: collection,
  370. columns: [{name: "id", cell: "integer"}]
  371. });
  372. paginator.render();
  373. // next page
  374. paginator.$el.find("a").eq(3).click();
  375. expect(collection.state.currentPage).toBe(1);
  376. // first page
  377. paginator.$el.find("a").eq(0).click();
  378. expect(collection.state.currentPage).toBe(0);
  379. });
  380. it("renders page handles <= windowSize", function () {
  381. expect(paginator.$el.find("a").length).toBe(7);
  382. paginator.windowSize = 1;
  383. paginator.render();
  384. expect(paginator.$el.find("a").length).toBe(5);
  385. });
  386. it("refreshes upon row insertion", function () {
  387. collection.add([{id: 6}, {id: 7}]);
  388. expect(paginator.$el.find("a").length).toBe(8);
  389. expect(paginator.$el.find("a[title='Page 4']").length).toBe(1);
  390. });
  391. it("refreshes upon row removal", function () {
  392. collection.remove(collection.first());
  393. expect(paginator.$el.find("a").length).toBe(6);
  394. expect(paginator.$el.find("a[title='Page 3']").length).toBe(0);
  395. });
  396. it("refreshes upon collection reset", function () {
  397. collection.fullCollection.reset();
  398. expect(paginator.$el.find("a").length).toBe(5);
  399. expect(paginator.$el.find("a[title='Page 1']").length).toBe(1);
  400. expect(paginator.$el.find("a[title='Page 2']").length).toBe(0);
  401. });
  402. it("will go back to the first page on sort", function () {
  403. paginator.$el.find("a").eq(3).click();
  404. collection.setSorting("id", -1);
  405. collection.fullCollection.sort();
  406. expect(paginator.$el.find("li").eq(2).hasClass("active")).toBe(true);
  407. });
  408. });
  409. describe("when under server mode", function () {
  410. beforeEach(function () {
  411. collection = new Backbone.PageableCollection([{id: 1}], {
  412. state: {
  413. pageSize: 1,
  414. totalRecords: 3
  415. }
  416. });
  417. paginator = new Backgrid.Extension.Paginator({
  418. collection: collection,
  419. columns: [{name: "id", cell: "integer"}]
  420. });
  421. paginator.render();
  422. });
  423. it("clicking on active or disabled page handles have no effect", function () {
  424. paginator = new Backgrid.Extension.Paginator({
  425. collection: new Backbone.PageableCollection([{id: 1}], {
  426. state: {
  427. pageSize: 1
  428. }
  429. }),
  430. columns: [{name: "id", cell: "integer"}]
  431. });
  432. paginator.$el.find("a").eq(0).click();
  433. expect(paginator.collection.state.currentPage).toBe(1);
  434. paginator.$el.find("a").eq(1).click();
  435. expect(paginator.collection.state.currentPage).toBe(1);
  436. paginator.$el.find("a").eq(2).click();
  437. expect(paginator.collection.state.currentPage).toBe(1);
  438. paginator.$el.find("a").eq(3).click();
  439. expect(paginator.collection.state.currentPage).toBe(1);
  440. paginator.$el.find("a").eq(4).click();
  441. expect(paginator.collection.state.currentPage).toBe(1);
  442. });
  443. it("has page handles that go to the correct pages when clicked", function () {
  444. paginator.windowSize = 1;
  445. paginator.render();
  446. collection.url = "url";
  447. var oldAjax = Backbone.ajax;
  448. // last page
  449. Backbone.ajax = function (settings) {
  450. settings.success([{id: 3}]);
  451. };
  452. paginator.$el.find("a").eq(4).click();
  453. expect(paginator.$el.find("a").eq(2).html()).toBe('3');
  454. expect(collection.state.currentPage).toBe(3);
  455. // prev page
  456. Backbone.ajax = function (settings) {
  457. settings.success([{id: 2}]);
  458. };
  459. paginator.$el.find("a").eq(1).click();
  460. expect(paginator.$el.find("a").eq(2).html()).toBe('2');
  461. expect(collection.state.currentPage).toBe(2);
  462. // 0-based page indices
  463. collection = new Backbone.PageableCollection([{id: 1}, {id: 2}], {
  464. state: {
  465. pageSize: 2,
  466. totalRecords: 5,
  467. firstPage: 0
  468. }
  469. });
  470. collection.url = "url";
  471. paginator = new Backgrid.Extension.Paginator({
  472. collection: collection,
  473. columns: [{name: "id", cell: "integer"}]
  474. });
  475. paginator.render();
  476. // page 2
  477. Backbone.ajax = function (settings) {
  478. settings.success([{id: 3}, {id: 4}]);
  479. };
  480. paginator.$el.find("a").eq(3).click();
  481. expect(collection.state.currentPage).toBe(1);
  482. // page 1
  483. Backbone.ajax = function (settings) {
  484. settings.success([{id: 1}, {id: 2}]);
  485. };
  486. paginator.$el.find("a").eq(2).click();
  487. expect(collection.state.currentPage).toBe(0);
  488. // next page
  489. Backbone.ajax = function (settings) {
  490. settings.success([{id: 3}, {id: 4}]);
  491. };
  492. paginator.$el.find("a").eq(5).click();
  493. expect(collection.state.currentPage).toBe(1);
  494. // first page
  495. Backbone.ajax = function (settings) {
  496. settings.success([{id: 1}, {id: 2}]);
  497. };
  498. paginator.$el.find("a").eq(0).click();
  499. expect(collection.state.currentPage).toBe(0);
  500. Backbone.ajax = oldAjax;
  501. });
  502. it("renders page handles <= windowSize", function () {
  503. expect(paginator.$el.find("a").length).toBe(7);
  504. paginator.windowSize = 1;
  505. paginator.render();
  506. expect(paginator.$el.find("a").length).toBe(5);
  507. });
  508. it("displays a single page handler number 1 when the collection is empty and totalRecords is null", function () {
  509. paginator = new Backgrid.Extension.Paginator({
  510. collection: new Backbone.PageableCollection(),
  511. columns: [{name: "id", cell: "integer"}]
  512. });
  513. paginator.render();
  514. expect(paginator.$el.find("a").length).toBe(5);
  515. expect(paginator.$el.find("a[title='Page 1']").length).toBe(1);
  516. expect(paginator.$el.find("a[title='Page 2']").length).toBe(0);
  517. });
  518. it("refreshes upon collection reset", function () {
  519. collection.reset([{id: 1}]);
  520. expect(paginator.$el.find("a").length).toBe(7);
  521. expect(paginator.$el.find("a[title='Page 1']").length).toBe(1);
  522. expect(paginator.$el.find("a[title='Page 2']").length).toBe(1);
  523. expect(paginator.$el.find("a[title='Page 3']").length).toBe(1);
  524. });
  525. });
  526. describe("renders only the control page handles declared", function () {
  527. beforeEach(function () {
  528. collection = new Backbone.PageableCollection([{id: 1}, {id: 2}, {id: 3}], {
  529. state: {
  530. pageSize: 1
  531. },
  532. mode: "client"
  533. });
  534. paginator = new Backgrid.Extension.Paginator({
  535. collection: collection,
  536. columns: [{name: "id", cell: "integer"}]
  537. });
  538. paginator.render();
  539. });
  540. it("defined under any mode", function () {
  541. paginator = new (Backgrid.Extension.Paginator.extend({
  542. controls: {
  543. back: {
  544. label: "prev",
  545. title: "prev"
  546. },
  547. forward: {
  548. label: "next",
  549. title: "next"
  550. }
  551. }
  552. }))({
  553. collection: collection,
  554. columns: [{name: "id", cell: "integer"}]
  555. });
  556. paginator.render();
  557. expect(paginator.$el.find("a").length).toBe(5);
  558. expect(paginator.$el.find("a").eq(0).html()).toBe("prev");
  559. expect(paginator.$el.find("a").eq(4).html()).toBe("next");
  560. });
  561. });
  562. });