PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/test/infinite-pageable.js

https://github.com/rickhewes/backbone-pageable
JavaScript | 275 lines | 226 code | 42 blank | 7 comment | 3 complexity | 1d187072d67333b80126d4c3b52e0390 MD5 | raw file
Possible License(s): MIT
  1. $(document).ready(function () {
  2. "use strict";
  3. var col;
  4. module("Backbone.PageableCollection - Infinite", {
  5. setup: function () {
  6. col = new (Backbone.PageableCollection.extend({
  7. url: "url"
  8. }))([
  9. {id: 1},
  10. {id: 3},
  11. {id: 2},
  12. {id: 4}
  13. ], {
  14. state: {
  15. pageSize: 2,
  16. currentPage: 2
  17. },
  18. mode: "infinite"
  19. });
  20. }
  21. });
  22. test("initialize", function () {
  23. ok(col.fullCollection instanceof Backbone.Collection);
  24. strictEqual(col.url, "url");
  25. strictEqual(col.mode, "infinite");
  26. strictEqual(col.state.totalRecords, 4);
  27. deepEqual(col.links, {
  28. "1": "url",
  29. "2": "url"
  30. });
  31. deepEqual(col.toJSON(), [{id: 2}, {id: 4}]);
  32. deepEqual(col.fullCollection.toJSON(), [{id: 1}, {id: 3}, {id: 2}, {id: 4}]);
  33. col = new (Backbone.PageableCollection.extend({
  34. url: "url"
  35. }))(null, {
  36. state: {
  37. firstPage: 0
  38. },
  39. mode: "infinite"
  40. });
  41. ok(col.links[0] === "url");
  42. });
  43. test("parseLinks", function () {
  44. var xhr = {
  45. getResponseHeader: function (header) {
  46. if (header.toLowerCase() == "link") {
  47. return '<https://api.github.com/user/repos?page=3&per_page=2>; rel="next", <https://api.github.com/user/repos?page=50&per_page=2>; rel="last"';
  48. }
  49. return null;
  50. }
  51. };
  52. var links = col.parseLinks({}, {xhr: xhr});
  53. deepEqual(links, {
  54. next: "https://api.github.com/user/repos?page=3&per_page=2"
  55. });
  56. strictEqual(col.state.totalRecords, 100);
  57. strictEqual(col.state.totalPages, 50);
  58. strictEqual(col.state.lastPage, 50);
  59. xhr.getResponseHeader = function () {
  60. return null;
  61. };
  62. links = col.parseLinks({}, {xhr: xhr});
  63. deepEqual(links, {});
  64. });
  65. test("fetch", 3, function () {
  66. var ajax = $.ajax;
  67. $.ajax = function (settings) {
  68. strictEqual(settings.url, "url");
  69. deepEqual(settings.data, {
  70. page: 2,
  71. "per_page": 2,
  72. "total_entries": 4,
  73. "total_pages": 2
  74. });
  75. settings.success([
  76. {id: 5},
  77. {id: 6}
  78. ]);
  79. };
  80. var oldParse = col.parse;
  81. col.parse = function () {
  82. ok(true);
  83. return oldParse.apply(this, arguments);
  84. };
  85. col.parseLinks = function () {
  86. return {first: "url-1", next: "url-2"};
  87. };
  88. // makes sure normal add, remove and sort events are suppressed
  89. col.on("all", function (event) {
  90. if (_.contains(["add", "remove", "sort"], event)) ok(false);
  91. });
  92. col.fetch();
  93. col.parse = oldParse;
  94. $.ajax = ajax;
  95. });
  96. test("get*Page", 43, function () {
  97. var col = new (Backbone.PageableCollection.extend({
  98. url: "url"
  99. }))(null, {
  100. state: {
  101. pageSize: 2
  102. },
  103. mode: "infinite"
  104. });
  105. throws(function () {
  106. col.getPage("nosuchpage");
  107. });
  108. sinon.stub(col, "parseLinks").returns({next: "url2", last: "lastUrl"});
  109. var ajax = $.ajax;
  110. $.ajax = function (settings) {
  111. settings.success([
  112. {id: 2},
  113. {id: 1}
  114. ]);
  115. };
  116. // test paging in the first page gets a page full of models and a link for
  117. // the next page
  118. col.getFirstPage({success: function () {
  119. strictEqual(col.state.currentPage, col.state.firstPage);
  120. strictEqual(col.state.totalRecords, 2);
  121. strictEqual(col.state.totalPages, 1);
  122. strictEqual(col.state.lastPage, 1);
  123. strictEqual(col.fullCollection.length, 2);
  124. deepEqual(col.links, {
  125. "1": "url",
  126. "2": "url2"
  127. });
  128. deepEqual(col.toJSON(), [{id: 2}, {id: 1}]);
  129. deepEqual(col.fullCollection.toJSON(), [{id: 2}, {id: 1}]);
  130. }});
  131. col.parseLinks.reset();
  132. col.parseLinks.returns({next: "url3"});
  133. $.ajax = function (settings) {
  134. settings.success([
  135. {id: 3},
  136. {id: 4}
  137. ]);
  138. };
  139. // test paging for a page that has a link but no models results in a fetch
  140. col.getNextPage({success: function () {
  141. strictEqual(col.state.currentPage, 2);
  142. strictEqual(col.state.totalRecords, 4);
  143. strictEqual(col.state.totalPages, 2);
  144. strictEqual(col.state.lastPage, 2);
  145. strictEqual(col.fullCollection.length, 4);
  146. deepEqual(col.links, {
  147. "1": "url",
  148. "2": "url2",
  149. "3": "url3"
  150. });
  151. deepEqual(col.toJSON(), [{id: 3}, {id: 4}]);
  152. deepEqual(col.fullCollection.toJSON(), [{id: 2}, {id: 1}, {id: 3}, {id: 4}]);
  153. }});
  154. col.parseLinks.reset();
  155. $.ajax = function () {
  156. ok(false, "ajax should not be called");
  157. };
  158. // test paging backward use cache
  159. col.getPreviousPage();
  160. strictEqual(col.parseLinks.called, false);
  161. strictEqual(col.state.currentPage, 1);
  162. strictEqual(col.state.totalRecords, 4);
  163. strictEqual(col.state.totalPages, 2);
  164. strictEqual(col.state.lastPage, 2);
  165. strictEqual(col.fullCollection.length, 4);
  166. deepEqual(col.links, {
  167. "1": "url",
  168. "2": "url2",
  169. "3": "url3"
  170. });
  171. deepEqual(col.toJSON(), [{id: 2}, {id: 1}]);
  172. deepEqual(col.fullCollection.toJSON(), [{id: 2}, {id: 1}, {id: 3}, {id: 4}]);
  173. // test paging to last page
  174. col.getLastPage();
  175. strictEqual(col.parseLinks.called, false);
  176. strictEqual(col.state.currentPage, 2);
  177. strictEqual(col.state.totalRecords, 4);
  178. strictEqual(col.state.totalPages, 2);
  179. strictEqual(col.state.lastPage, 2);
  180. strictEqual(col.fullCollection.length, 4);
  181. deepEqual(col.links, {
  182. "1": "url",
  183. "2": "url2",
  184. "3": "url3"
  185. });
  186. deepEqual(col.toJSON(), [{id: 3}, {id: 4}]);
  187. deepEqual(col.fullCollection.toJSON(), [{id: 2}, {id: 1}, {id: 3}, {id: 4}]);
  188. $.ajax = function (settings) {
  189. settings.success([
  190. {id: 4},
  191. {id: 5}
  192. ]);
  193. };
  194. // test force fetch update the current page under 0.9.9+ and resets otherwise
  195. col.getPage(col.state.currentPage, {fetch: true});
  196. strictEqual(col.state.currentPage, 2);
  197. strictEqual(col.state.totalRecords, 4);
  198. strictEqual(col.state.totalPages, 2);
  199. strictEqual(col.state.lastPage, 2);
  200. strictEqual(col.fullCollection.length, 4);
  201. deepEqual(col.links, {
  202. "1": "url",
  203. "2": "url2",
  204. "3": "url3"
  205. });
  206. deepEqual(col.toJSON(), [{id: 4}, {id: 5}]);
  207. deepEqual(col.fullCollection.toJSON(), [{id: 2}, {id: 1}, {id: 4}, {id: 5}]);
  208. col.parseLinks.restore();
  209. $.ajax = ajax;
  210. });
  211. test("hasNext and hasPrevious", function () {
  212. var col = new (Backbone.PageableCollection.extend({
  213. url: "url"
  214. }))([
  215. {id: 1},
  216. {id: 2},
  217. {id: 3}
  218. ], {
  219. state: {
  220. pageSize: 1
  221. },
  222. mode: "infinite"
  223. });
  224. strictEqual(col.hasPrevious(), false);
  225. strictEqual(col.hasNext(), true);
  226. col.getNextPage();
  227. strictEqual(col.hasPrevious(), true);
  228. strictEqual(col.hasNext(), true);
  229. col.getLastPage();
  230. strictEqual(col.hasPrevious(), true);
  231. strictEqual(col.hasNext(), false);
  232. });
  233. });