PageRenderTime 56ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/test/client-pageable.js

https://github.com/rickhewes/backbone-pageable
JavaScript | 772 lines | 646 code | 122 blank | 4 comment | 0 complexity | abae7c77eda664bdbee729c35f34f866 MD5 | raw file
Possible License(s): MIT
  1. $(document).ready(function () {
  2. "use strict";
  3. var a, c, b, models, comparator;
  4. module("Backbone.PageableCollection - Client", {
  5. setup: function () {
  6. a = new Backbone.Model({"name": "a"});
  7. c = new Backbone.Model({"name": "c"});
  8. b = new Backbone.Model({"name": "b"});
  9. models = [
  10. {"name": "a"},
  11. {"name": "c"},
  12. {"name": "b"}
  13. ];
  14. comparator = function (model) {
  15. return model.get("name");
  16. };
  17. }
  18. });
  19. test("_makeFullCollection", function () {
  20. var sync = function () {};
  21. var col = new (Backbone.PageableCollection.extend({
  22. url: "test/makeFullCollection",
  23. model: Backbone.Model
  24. }))();
  25. col.sync = sync;
  26. var fullCol = col._makeFullCollection(models,
  27. {comparator: comparator});
  28. ok(!_.isUndefined(fullCol));
  29. ok(_.isUndefined(fullCol.constructor.prototype.comparator));
  30. strictEqual(fullCol.comparator, comparator);
  31. strictEqual(fullCol.sync, sync);
  32. strictEqual(fullCol.constructor.prototype.model, Backbone.Model);
  33. strictEqual(fullCol.model, Backbone.Model);
  34. strictEqual(fullCol.constructor.prototype.url, "test/makeFullCollection");
  35. strictEqual(fullCol.url, "test/makeFullCollection");
  36. strictEqual(fullCol.at(0).get("name"), "a");
  37. strictEqual(fullCol.at(1).get("name"), "b");
  38. strictEqual(fullCol.at(2).get("name"), "c");
  39. a.collection = col;
  40. c.collection = col;
  41. b.collection = col;
  42. fullCol = col._makeFullCollection([
  43. a, c, b
  44. ]);
  45. strictEqual(fullCol.at(0).get("name"), "a");
  46. strictEqual(fullCol.at(1).get("name"), "c");
  47. strictEqual(fullCol.at(2).get("name"), "b");
  48. strictEqual(fullCol.at(0).collection, col);
  49. strictEqual(fullCol.at(1).collection, col);
  50. strictEqual(fullCol.at(2).collection, col);
  51. });
  52. test("initialize", function () {
  53. // TODO: test options.full attaches comparator to fullcollection only
  54. var col = new Backbone.PageableCollection(null, {
  55. mode: "client"
  56. });
  57. ok(col);
  58. ok(col.fullCollection);
  59. var mods = models.slice();
  60. col = new Backbone.PageableCollection(mods[0], {
  61. mode: "client"
  62. });
  63. strictEqual(col.state.totalRecords, 1);
  64. strictEqual(col.fullCollection.size(), 1);
  65. strictEqual(col.fullCollection.at(0).get("name"), "a");
  66. col = new Backbone.PageableCollection(mods, {
  67. comparator: comparator,
  68. state: {
  69. pageSize: 2
  70. },
  71. mode: "client"
  72. });
  73. strictEqual(col.state.totalRecords, 3);
  74. strictEqual(col.comparator, comparator);
  75. strictEqual(col.size(), 2);
  76. strictEqual(col.at(0).get("name"), "a");
  77. strictEqual(col.at(1).get("name"), "c");
  78. strictEqual(col.fullCollection.size(), 3);
  79. strictEqual(col.at(0), col.fullCollection.at(0));
  80. strictEqual(col.fullCollection.at(0).get("name"), "a");
  81. strictEqual(col.fullCollection.at(1).get("name"), "c");
  82. strictEqual(col.fullCollection.at(2).get("name"), "b");
  83. mods = models.slice();
  84. col = new Backbone.PageableCollection(mods, {
  85. state: {
  86. pageSize: 1,
  87. sortKey: "name"
  88. },
  89. full: true,
  90. mode: "client"
  91. });
  92. strictEqual(col.state.totalRecords, 3);
  93. ok(!_.isUndefined(col.fullCollection.comparator));
  94. ok(_.isUndefined(col.comparator));
  95. strictEqual(col.size(), 1);
  96. strictEqual(col.at(0).get("name"), "a");
  97. strictEqual(col.fullCollection.size(), 3);
  98. strictEqual(col.fullCollection.at(0).get("name"), "a");
  99. strictEqual(col.fullCollection.at(1).get("name"), "b");
  100. strictEqual(col.fullCollection.at(2).get("name"), "c");
  101. });
  102. test("add", 49, function () {
  103. var col = new Backbone.PageableCollection(models, {
  104. state: {
  105. pageSize: 2
  106. },
  107. mode: "client"
  108. });
  109. var lastTotalRecords = col.state.totalRecords;
  110. var onAdd = function () {
  111. strictEqual(col.state.totalRecords, lastTotalRecords + 1);
  112. };
  113. col.fullCollection.on("add", onAdd);
  114. col.on("add", onAdd);
  115. var d = new Backbone.Model({name: "d"});
  116. col.add(d);
  117. strictEqual(col.state.totalRecords, 4);
  118. strictEqual(col.state.totalPages, 2);
  119. strictEqual(col.fullCollection.size(), 4);
  120. strictEqual(col.size(), 2);
  121. strictEqual(col.at(0).get("name"), "a");
  122. strictEqual(col.at(1).get("name"), "c");
  123. strictEqual(col.fullCollection.at(0).get("name"), "a");
  124. strictEqual(col.fullCollection.at(1).get("name"), "c");
  125. strictEqual(col.fullCollection.at(2).get("name"), "d");
  126. strictEqual(col.fullCollection.at(3).get("name"), "b");
  127. lastTotalRecords = col.state.totalRecords;
  128. var e = new Backbone.Model({name: "e"});
  129. col.fullCollection.push(e);
  130. strictEqual(col.state.totalRecords, 5);
  131. strictEqual(col.state.totalPages, 3);
  132. strictEqual(col.fullCollection.size(), 5);
  133. strictEqual(col.size(), 2);
  134. strictEqual(col.at(0).get("name"), "a");
  135. strictEqual(col.at(1).get("name"), "c");
  136. strictEqual(col.fullCollection.at(4).get("name"), "e");
  137. strictEqual(col.indexOf(e.cid), -1);
  138. lastTotalRecords = col.state.totalRecords;
  139. var f = new Backbone.Model({name: "f"});
  140. col.fullCollection.unshift(f);
  141. strictEqual(col.state.totalRecords, 6);
  142. strictEqual(col.state.totalPages, 3);
  143. strictEqual(col.fullCollection.size(), 6);
  144. strictEqual(col.size(), 2);
  145. strictEqual(col.at(0).get("name"), "f");
  146. strictEqual(col.at(1).get("name"), "a");
  147. // test add at page col on page 2
  148. col.getPage(2);
  149. lastTotalRecords = col.state.totalRecords;
  150. var g = new Backbone.Model({name: "g"});
  151. col.add(g, {at: 1});
  152. strictEqual(col.size(), 2);
  153. strictEqual(col.fullCollection.size(), 7);
  154. strictEqual(col.state.totalRecords, 7);
  155. strictEqual(col.state.totalPages, 4);
  156. strictEqual(col.state.lastPage, 4);
  157. strictEqual(col.state.currentPage, 2);
  158. strictEqual(col.last().get("name"), "g");
  159. strictEqual(col.fullCollection.at(3).get("name"), "g");
  160. // test ability to add to empty collection
  161. col.fullCollection.reset();
  162. lastTotalRecords = col.state.totalRecords;
  163. col.add(new Backbone.Model({name: "a"}));
  164. // test ability to add an array of models
  165. col.off("add", onAdd);
  166. col.fullCollection.off("add", onAdd);
  167. col.fullCollection.reset();
  168. col.add([{name: "a"}, {name: "c"}, {name: "b"}]);
  169. strictEqual(col.size(), 2);
  170. strictEqual(col.fullCollection.size(), 3);
  171. deepEqual(col.toJSON(), [{name: "a"}, {name: "c"}]);
  172. deepEqual(col.fullCollection.toJSON(), [{name: "a"}, {name: "c"}, {name: "b"}]);
  173. col.fullCollection.reset();
  174. col.fullCollection.add([{name: "a"}, {name: "c"}, {name: "b"}]);
  175. strictEqual(col.size(), 2);
  176. strictEqual(col.fullCollection.size(), 3);
  177. deepEqual(col.toJSON(), [{name: "a"}, {name: "c"}]);
  178. deepEqual(col.fullCollection.toJSON(), [{name: "a"}, {name: "c"}, {name: "b"}]);
  179. });
  180. test("remove", 46, function () {
  181. var col = new Backbone.PageableCollection([
  182. {"name": "a"},
  183. {"name": "c"},
  184. {"name": "b"},
  185. {"name": "d"}
  186. ], {
  187. state: {
  188. pageSize: 1
  189. },
  190. mode: "client"
  191. });
  192. var lastTotalRecords = col.state.totalRecords;
  193. var lastTotalPages = col.state.totalPages;
  194. var onRemove = function () {
  195. ok(true);
  196. strictEqual(col.state.totalRecords, lastTotalRecords - 1);
  197. strictEqual(col.state.totalPages, lastTotalPages - 1);
  198. };
  199. col.on("remove", onRemove);
  200. col.fullCollection.on("remove", onRemove);
  201. col.fullCollection.remove(col.fullCollection.last());
  202. strictEqual(col.state.totalRecords, 3);
  203. strictEqual(col.state.totalPages, 3);
  204. strictEqual(col.size(), 1);
  205. strictEqual(col.at(0).get("name"), "a");
  206. strictEqual(col.fullCollection.size(), 3);
  207. strictEqual(col.fullCollection.at(0).get("name"), "a");
  208. strictEqual(col.fullCollection.at(1).get("name"), "c");
  209. strictEqual(col.fullCollection.at(2).get("name"), "b");
  210. lastTotalRecords = col.state.totalRecords;
  211. lastTotalPages = col.state.totalPages;
  212. col.fullCollection.remove(col.fullCollection.first());
  213. strictEqual(col.state.totalRecords, 2);
  214. strictEqual(col.state.totalPages, 2);
  215. strictEqual(col.size(), 1);
  216. strictEqual(col.at(0).get("name"), "c");
  217. strictEqual(col.fullCollection.size(), 2);
  218. strictEqual(col.fullCollection.at(0).get("name"), "c");
  219. strictEqual(col.fullCollection.at(1).get("name"), "b");
  220. lastTotalRecords = col.state.totalRecords;
  221. lastTotalPages = col.state.totalPages;
  222. col.remove(col.first());
  223. strictEqual(col.state.totalRecords, 1);
  224. strictEqual(col.state.totalPages, 1);
  225. strictEqual(col.size(), 1);
  226. strictEqual(col.at(0).get("name"), "b");
  227. strictEqual(col.fullCollection.size(), 1);
  228. strictEqual(col.fullCollection.at(0).get("name"), "b");
  229. col.off("remove", onRemove);
  230. col.fullCollection.off("remove", onRemove);
  231. onRemove = function () {
  232. ok(true);
  233. strictEqual(col.state.totalRecords, null);
  234. strictEqual(col.state.totalPages, null);
  235. };
  236. col.on("remove", onRemove);
  237. col.fullCollection.on("remove", onRemove);
  238. col.remove(col.fullCollection.first());
  239. strictEqual(col.state.totalRecords, null);
  240. strictEqual(col.state.totalPages, null);
  241. strictEqual(col.size(), 0);
  242. strictEqual(col.fullCollection.size(), 0);
  243. });
  244. test("add handlers are run before remove handlers", 2, function () {
  245. var addRan = false;
  246. var onAdd = function () {
  247. addRan = true;
  248. };
  249. var onRemove = function () {
  250. strictEqual(addRan, true);
  251. addRan = false;
  252. };
  253. var col = new Backbone.PageableCollection(models, {
  254. state: {
  255. pageSize: 1
  256. },
  257. mode: "client"
  258. });
  259. col.on("add", onAdd);
  260. col.on("remove", onRemove);
  261. col.unshift(new Backbone.Model({name: "d"}));
  262. col.fullCollection.unshift(new Backbone.Model({name: "e"}));
  263. });
  264. test("change", 6, function () {
  265. var col = new Backbone.PageableCollection(models, {
  266. state: {
  267. pageSize: 1
  268. },
  269. mode: "client"
  270. });
  271. var onChange = function () {
  272. ok(true);
  273. };
  274. col.on("change", onChange);
  275. col.fullCollection.on("change", onChange);
  276. col.at(0).set("name", "e");
  277. strictEqual(col.fullCollection.at(0).get("name"), "e");
  278. col.fullCollection.at(1).set("name", "f");
  279. col.fullCollection.at(0).set("name", "g");
  280. });
  281. test("sync", 5, function () {
  282. var ajax = $.ajax;
  283. $.ajax = function (settings) {
  284. settings.success();
  285. };
  286. var col = new (Backbone.PageableCollection.extend({
  287. url: "test-client-sync"
  288. }))(models, {
  289. state: {
  290. pageSize: 1
  291. },
  292. mode: "client"
  293. });
  294. var onSync = function () {
  295. ok(true);
  296. };
  297. col.on("sync", onSync);
  298. col.fullCollection.on("sync", onSync);
  299. col.at(0).save();
  300. col.fullCollection.at(0).save();
  301. col.fullCollection.at(1).save();
  302. $.ajax = ajax;
  303. });
  304. test("reset and sort", 76, function () {
  305. var mods = models.slice();
  306. var col = new Backbone.PageableCollection(mods, {
  307. state: {
  308. pageSize: 2
  309. },
  310. mode: "client"
  311. });
  312. var onReset = function () {
  313. ok(true);
  314. strictEqual(col.state.totalRecords, 4);
  315. strictEqual(col.state.totalPages, 2);
  316. strictEqual(col.state.lastPage, 2);
  317. };
  318. col.on("reset", onReset);
  319. col.fullCollection.on("reset", onReset);
  320. col.fullCollection.reset([
  321. {name: "e"},
  322. {name: "f"},
  323. {name: "d"},
  324. {name: "g"}
  325. ]);
  326. strictEqual(col.size(), 2);
  327. strictEqual(col.fullCollection.size(), 4);
  328. strictEqual(col.at(0).get("name"), "e");
  329. strictEqual(col.at(1).get("name"), "f");
  330. strictEqual(col.fullCollection.at(0).get("name"), "e");
  331. strictEqual(col.fullCollection.at(1).get("name"), "f");
  332. strictEqual(col.fullCollection.at(2).get("name"), "d");
  333. strictEqual(col.fullCollection.at(3).get("name"), "g");
  334. col.fullCollection.comparator = comparator;
  335. col.fullCollection.sort();
  336. strictEqual(col.size(), 2);
  337. strictEqual(col.at(0).get("name"), "d");
  338. strictEqual(col.at(1).get("name"), "e");
  339. mods = models.slice();
  340. col = new Backbone.PageableCollection(mods, {
  341. state: {
  342. pageSize: 2
  343. },
  344. mode: "client"
  345. });
  346. onReset = function () {
  347. ok(true);
  348. strictEqual(col.state.totalRecords, 3);
  349. strictEqual(col.state.totalPages, 2);
  350. strictEqual(col.state.lastPage, 2);
  351. };
  352. col.on("reset", onReset);
  353. col.fullCollection.on("reset", onReset);
  354. col.comparator = comparator;
  355. col.sort();
  356. strictEqual(col.at(0).get("name"), "a");
  357. strictEqual(col.at(1).get("name"), "c");
  358. strictEqual(col.fullCollection.at(0).get("name"), "a");
  359. strictEqual(col.fullCollection.at(1).get("name"), "c");
  360. strictEqual(col.fullCollection.at(2).get("name"), "b");
  361. col.comparator = null;
  362. col.off("reset", onReset);
  363. col.fullCollection.off("reset", onReset);
  364. onReset = function () {
  365. ok(true);
  366. strictEqual(col.state.totalRecords, 3);
  367. strictEqual(col.state.totalPages, 2);
  368. strictEqual(col.state.lastPage, 2);
  369. };
  370. col.on("reset", onReset);
  371. col.fullCollection.on("reset", onReset);
  372. mods = [new Backbone.Model({name: "g"}), col.at(0)];
  373. col.reset(mods);
  374. strictEqual(col.size(), 2);
  375. strictEqual(col.fullCollection.size(), 3);
  376. strictEqual(col.at(0).get("name"), "g");
  377. strictEqual(col.at(1).get("name"), "a");
  378. strictEqual(col.fullCollection.at(0).get("name"), "g");
  379. strictEqual(col.fullCollection.at(1).get("name"), "a");
  380. strictEqual(col.fullCollection.at(2).get("name"), "b");
  381. col.off("reset", onReset);
  382. col.fullCollection.off("reset", onReset);
  383. onReset = function () {
  384. ok(true);
  385. strictEqual(col.state.totalRecords, 4);
  386. strictEqual(col.state.totalPages, 2);
  387. strictEqual(col.state.lastPage, 2);
  388. };
  389. col.on("reset", onReset);
  390. col.fullCollection.on("reset", onReset);
  391. col.fullCollection.reset([
  392. {name: "j"},
  393. {name: "h"},
  394. {name: "i"},
  395. {name: "k"}
  396. ]);
  397. strictEqual(col.size(), 2);
  398. strictEqual(col.fullCollection.size(), 4);
  399. strictEqual(col.at(0).get("name"), "j");
  400. strictEqual(col.at(1).get("name"), "h");
  401. strictEqual(col.fullCollection.at(0).get("name"), "j");
  402. strictEqual(col.fullCollection.at(1).get("name"), "h");
  403. strictEqual(col.fullCollection.at(2).get("name"), "i");
  404. strictEqual(col.fullCollection.at(3).get("name"), "k");
  405. strictEqual(col.state.totalRecords, 4);
  406. strictEqual(col.state.lastPage, 2);
  407. strictEqual(col.state.totalPages, 2);
  408. col.off("reset", onReset);
  409. col.fullCollection.off("reset", onReset);
  410. onReset = function () {
  411. ok(true);
  412. ok(col.state.totalRecords === null);
  413. ok(col.state.totalPages === null);
  414. ok(col.state.lastPage === col.state.firstPage);
  415. ok(col.state.currentPage === col.state.firstPage);
  416. ok(col.length === 0);
  417. ok(col.fullCollection.length === 0);
  418. };
  419. col.on("reset", onReset);
  420. col.fullCollection.on("reset", onReset);
  421. col.fullCollection.reset();
  422. });
  423. test("fetch", 12, function () {
  424. var ajax = $.ajax;
  425. $.ajax = function (settings) {
  426. strictEqual(settings.url, "test-client-fetch");
  427. deepEqual(settings.data, {
  428. "sort_by": "name",
  429. "order": "desc"
  430. });
  431. settings.success([
  432. {name: "a"},
  433. {name: "c"},
  434. {name: "d"},
  435. {name: "b"}
  436. ]);
  437. };
  438. var col = new (Backbone.PageableCollection.extend({
  439. url: "test-client-fetch"
  440. }))(models, {
  441. state: {
  442. pageSize: 2,
  443. sortKey: "name",
  444. order: 1
  445. },
  446. mode: "client"
  447. });
  448. var onReset = function () {
  449. ok(true);
  450. };
  451. var onFullReset = function () {
  452. ok(true);
  453. };
  454. col.on("reset", onReset);
  455. col.fullCollection.on("reset", onFullReset);
  456. var oldParse = col.parse;
  457. col.parse = function () {
  458. ok(true);
  459. return oldParse.apply(this, arguments);
  460. };
  461. col.fetch();
  462. col.parse = oldParse;
  463. strictEqual(col.at(0).get("name"), "d");
  464. strictEqual(col.at(1).get("name"), "c");
  465. strictEqual(col.fullCollection.at(0).get("name"), "d");
  466. strictEqual(col.fullCollection.at(1).get("name"), "c");
  467. strictEqual(col.fullCollection.at(2).get("name"), "b");
  468. strictEqual(col.fullCollection.at(3).get("name"), "a");
  469. $.ajax = ajax;
  470. });
  471. test("getPageByOffset - firstPage is 0", function () {
  472. var manyModels = [
  473. {"name": "a1"},
  474. {"name": "a2"},
  475. {"name": "b1"},
  476. {"name": "b2"},
  477. {"name": "c1"},
  478. {"name": "c2"}
  479. ];
  480. var col = new Backbone.PageableCollection(manyModels, {
  481. state: {
  482. pageSize: 2,
  483. firstPage: 0,
  484. currentPage: 0,
  485. },
  486. mode: "client"
  487. });
  488. strictEqual(col.state.currentPage, 0);
  489. col.getPageByOffset(2);
  490. strictEqual(1, col.state.currentPage);
  491. strictEqual("b1", col.at(0).get("name"));
  492. col.getPageByOffset(1);
  493. strictEqual(0, col.state.currentPage);
  494. strictEqual("a1", col.at(0).get("name"));
  495. col.getPageByOffset(col.state.totalRecords - 1);
  496. strictEqual(2, col.state.currentPage);
  497. strictEqual("c1", col.at(0).get("name"));
  498. sinon.stub(col, "getPage");
  499. col.getPageByOffset(0);
  500. ok(col.getPage.calledOnce);
  501. });
  502. test("getPageByOffset - firstPage is 1", function () {
  503. var manyModels = [
  504. {"name": "a1"},
  505. {"name": "a2"},
  506. {"name": "b1"},
  507. {"name": "b2"},
  508. {"name": "c1"},
  509. {"name": "c2"}
  510. ];
  511. var col = new Backbone.PageableCollection(manyModels, {
  512. state: {
  513. pageSize: 2,
  514. firstPage: 1,
  515. currentPage: 1
  516. },
  517. mode: "client"
  518. });
  519. strictEqual(1, col.state.currentPage);
  520. col.getPageByOffset(2);
  521. strictEqual(2, col.state.currentPage);
  522. strictEqual("b1", col.at(0).get("name"));
  523. col.getPageByOffset(1);
  524. strictEqual(1, col.state.currentPage);
  525. strictEqual("a1", col.at(0).get("name"));
  526. col.getPageByOffset(col.state.totalRecords - 1);
  527. strictEqual(3, col.state.currentPage);
  528. strictEqual("c1", col.at(0).get("name"));
  529. sinon.stub(col, "getPage");
  530. col.getPageByOffset(0);
  531. ok(col.getPage.calledOnce);
  532. });
  533. test("getPage", function () {
  534. var col = new Backbone.PageableCollection(models, {
  535. state: {
  536. pageSize: 2
  537. },
  538. mode: "client"
  539. });
  540. col.getPage(2);
  541. strictEqual(col.size(), 1);
  542. strictEqual(col.at(0).get("name"), "b");
  543. sinon.stub(col, "fetch");
  544. col.getPage(1, {fetch: true});
  545. ok(col.fetch.calledOnce);
  546. });
  547. test("getFirstPage", function () {
  548. var col = new Backbone.PageableCollection(models, {
  549. state: {
  550. pageSize: 2
  551. },
  552. mode: "client"
  553. });
  554. col.getFirstPage();
  555. strictEqual(col.size(), 2);
  556. strictEqual(col.at(0).get("name"), "a");
  557. strictEqual(col.at(1).get("name"), "c");
  558. sinon.stub(col, "fetch");
  559. col.getFirstPage({fetch: true});
  560. ok(col.fetch.calledOnce);
  561. });
  562. test("getPreviousPage", function () {
  563. var col = new Backbone.PageableCollection(models, {
  564. state: {
  565. pageSize: 2
  566. },
  567. mode: "client"
  568. });
  569. col.getNextPage();
  570. col.getPreviousPage();
  571. strictEqual(col.size(), 2);
  572. strictEqual(col.at(0).get("name"), "a");
  573. strictEqual(col.at(1).get("name"), "c");
  574. sinon.stub(col, "fetch");
  575. col.getNextPage();
  576. col.getPreviousPage({fetch: true});
  577. ok(col.fetch.calledOnce);
  578. });
  579. test("getNextPage", function () {
  580. var col = new Backbone.PageableCollection(models, {
  581. state: {
  582. pageSize: 2
  583. },
  584. mode: "client"
  585. });
  586. col.getNextPage();
  587. strictEqual(col.size(), 1);
  588. strictEqual(col.at(0).get("name"), "b");
  589. sinon.stub(col, "fetch");
  590. col.getPreviousPage();
  591. col.getNextPage({fetch: true});
  592. ok(col.fetch.calledOnce);
  593. });
  594. test("getLastPage", function () {
  595. var col = new Backbone.PageableCollection(models, {
  596. state: {
  597. pageSize: 2
  598. },
  599. mode: "client"
  600. });
  601. col.getLastPage();
  602. strictEqual(col.size(), 1);
  603. strictEqual(col.at(0).get("name"), "b");
  604. sinon.stub(col, "fetch");
  605. col.getLastPage({fetch: true});
  606. ok(col.fetch.calledOnce);
  607. });
  608. test("setPageSize", function () {
  609. var col = new Backbone.PageableCollection(models, {
  610. state: {
  611. pageSize: 2
  612. },
  613. mode: "client"
  614. });
  615. col.setPageSize(1);
  616. strictEqual(col.state.pageSize, 1);
  617. strictEqual(col.state.totalPages, 3);
  618. strictEqual(col.state.lastPage, 3);
  619. });
  620. test("issue #15", function () {
  621. var col = new Backbone.PageableCollection(models, {
  622. state: {
  623. pageSize: 2,
  624. currentPage: 2
  625. },
  626. mode: "client"
  627. });
  628. col.fullCollection.remove(col.fullCollection.last());
  629. strictEqual(col.state.currentPage, 1);
  630. strictEqual(col.state.totalRecords, 2);
  631. strictEqual(col.state.lastPage, 1);
  632. strictEqual(col.state.totalPages, 1);
  633. });
  634. test("hasNext and hasPrevious", function () {
  635. var col = new Backbone.PageableCollection(models, {
  636. state: {
  637. pageSize: 1
  638. },
  639. mode: "client"
  640. });
  641. strictEqual(col.hasPrevious(), false);
  642. strictEqual(col.hasNext(), true);
  643. col.getNextPage();
  644. strictEqual(col.hasPrevious(), true);
  645. strictEqual(col.hasNext(), true);
  646. col.getLastPage();
  647. strictEqual(col.hasPrevious(), true);
  648. strictEqual(col.hasNext(), false);
  649. });
  650. });