PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/test/header.js

https://github.com/antonyraj/backgrid
JavaScript | 320 lines | 249 code | 64 blank | 7 comment | 0 complexity | 630ab9586c6242b40f5045dca8bf8a7a 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 HeaderCell", function () {
  8. it("throws TypeError if a column is not given", function () {
  9. expect(function () {
  10. new Backgrid.HeaderCell({
  11. collection: new Backbone.Collection()
  12. });
  13. }).toThrow(new TypeError("'column' is required"));
  14. });
  15. it("throws TypeError if a collection is not given", function () {
  16. expect(function () {
  17. new Backgrid.HeaderCell({
  18. column: [{
  19. name: "name",
  20. cell: "string"
  21. }]
  22. });
  23. }).toThrow(new TypeError("'collection' is required"));
  24. });
  25. var col;
  26. var cell;
  27. beforeEach(function () {
  28. col = new Backbone.Collection([{id: 2}, {id: 1}, {id: 3}]);
  29. cell = new Backgrid.HeaderCell({
  30. column: {
  31. name: "id",
  32. cell: "integer"
  33. },
  34. collection: col
  35. });
  36. cell.render();
  37. });
  38. it("renders a table header cell with an anchor wrapping the label text and an optional sort caret", function () {
  39. expect(cell.el.tagName).toBe("TH");
  40. expect(cell.$el.find("a").text()).toBe("id");
  41. expect(cell.$el.find(".sort-caret").length).toBe(1);
  42. cell.column.set("sortable", false);
  43. cell.render();
  44. expect(cell.el.tagName).toBe("TH");
  45. expect(cell.$el.find("a").text()).toBe("id");
  46. expect(cell.$el.find(".sort-caret").length).toBe(0);
  47. });
  48. it("sorts the underlying collection in ascending order upon clicking the sort caret once", function () {
  49. cell.$el.find("a").click();
  50. expect(cell.collection.toJSON()).toEqual([{id: 1}, {id: 2}, {id: 3}]);
  51. });
  52. it("sorts the underlying collection in descending order upon clicking the sort caret twice", function () {
  53. cell.$el.find("a").click().click();
  54. expect(cell.direction()).toBe("descending");
  55. expect(cell.collection.toJSON()).toEqual([{id: 3}, {id: 2}, {id: 1}]);
  56. });
  57. it("sorts the underlying collection in default order upon clicking the sort caret thrice", function () {
  58. cell.$el.find("a").click().click().click();
  59. expect(cell.direction()).toBeNull();
  60. expect(cell.collection.toJSON()).toEqual([{id: 2}, {id: 1}, {id: 3}]);
  61. });
  62. it("can sort on a server-mode Backbone.PageableCollection", function () {
  63. var oldAjax = $.ajax;
  64. $.ajax = function (settings) {
  65. settings.success([{"total_entries": 3}, [{id: 2}, {id: 1}]]);
  66. };
  67. var books = new Backbone.PageableCollection([{id: 1}, {id: 2}], {
  68. url: "test-headercell",
  69. state: {
  70. pageSize: 3
  71. }
  72. });
  73. cell = new Backgrid.HeaderCell({
  74. column: {
  75. name: "title",
  76. cell: "string"
  77. },
  78. collection: books
  79. });
  80. cell.render();
  81. expect(cell.collection.at(0).get("id")).toBe(1);
  82. expect(cell.collection.at(1).get("id")).toBe(2);
  83. cell.$el.find("a").click().click();
  84. expect(cell.collection.at(0).get("id")).toBe(2);
  85. expect(cell.collection.at(1).get("id")).toBe(1);
  86. $.ajax = oldAjax;
  87. });
  88. it("can sort on a client-mode Backbone.PageableCollection", function () {
  89. var books = new Backbone.PageableCollection([{
  90. title: "Alice's Adventures in Wonderland"
  91. }, {
  92. title: "A Tale of Two Cities"
  93. }, {
  94. title: "The Catcher in the Rye"
  95. }], {
  96. state: {
  97. pageSize: 1
  98. },
  99. mode: "client"
  100. });
  101. cell = new Backgrid.HeaderCell({
  102. column: {
  103. name: "title",
  104. cell: "string"
  105. },
  106. collection: books
  107. });
  108. cell.render();
  109. cell.$el.find("a").click();
  110. expect(cell.collection.toJSON()).toEqual([{
  111. title: "A Tale of Two Cities"
  112. }]);
  113. cell.collection.getPage(2);
  114. expect(cell.collection.toJSON()).toEqual([{
  115. title: "Alice's Adventures in Wonderland"
  116. }]);
  117. cell.collection.getPage(3);
  118. expect(cell.collection.toJSON()).toEqual([{
  119. title: "The Catcher in the Rye"
  120. }]);
  121. cell.collection.getFirstPage();
  122. cell.$el.find("a").click();
  123. expect(cell.collection.toJSON()).toEqual([{
  124. title: "The Catcher in the Rye"
  125. }]);
  126. cell.$el.find("a").click();
  127. expect(cell.collection.toJSON()).toEqual([{
  128. title: "Alice's Adventures in Wonderland"
  129. }]);
  130. });
  131. });
  132. describe("A HeaderRow", function () {
  133. var Book = Backbone.Model.extend({});
  134. var Books = Backbone.Collection.extend({
  135. model: Book
  136. });
  137. var books;
  138. var row;
  139. beforeEach(function () {
  140. books = new Books([{
  141. title: "Alice's Adventures in Wonderland",
  142. year: 1865
  143. }, {
  144. title: "A Tale of Two Cities",
  145. year: 1859
  146. }, {
  147. title: "The Catcher in the Rye",
  148. year: 1951
  149. }]);
  150. row = new Backgrid.HeaderRow({
  151. columns: [{
  152. name: "name",
  153. cell: "string"
  154. }, {
  155. name: "year",
  156. cell: "integer"
  157. }],
  158. collection: books
  159. });
  160. row.render();
  161. });
  162. it("throws TypeError when a list of column definition is not given", function () {
  163. expect(function () {
  164. new Backgrid.HeaderRow({
  165. collection: new Backbone.Collection()
  166. });
  167. }).toThrow(new TypeError("'columns' is required"));
  168. });
  169. it("throws TypeError when a collection is not given", function () {
  170. expect(function () {
  171. new Backgrid.HeaderRow({
  172. columns: [{
  173. name: "name",
  174. cell: "string"
  175. }]
  176. });
  177. }).toThrow(new TypeError("'collection' is required"));
  178. });
  179. it("renders a row of header cells", function () {
  180. expect(row.$el[0].tagName).toBe("TR");
  181. expect(row.$el[0].innerHTML).toBe('<th><a>name<b class="sort-caret"></b></a></th>' +
  182. '<th><a>year<b class="sort-caret"></b></a></th>');
  183. });
  184. it("resets the carets of the non-sorting columns", function () {
  185. row.$el.find("a").eq(0).click(); // ascending
  186. row.$el.find("a").eq(1).click(); // ascending, resets the previous
  187. expect(row.$el.find("a").eq(0).hasClass("ascending")).toBe(false);
  188. expect(row.$el.find("a").eq(1).hasClass("ascending")).toBe(false);
  189. });
  190. it("inserts or removes a cell if a column is added or removed", function () {
  191. row.columns.add({name: "price", cell: "number"});
  192. expect(row.$el.children().length).toBe(3);
  193. expect(row.$el.children().last()[0].outerHTML).toBe('<th><a>price<b class="sort-caret"></b></a></th>');
  194. row.columns.add({name: "publisher", cell: "string", renderable: false});
  195. expect(row.$el.children().length).toBe(4);
  196. expect(row.$el.children().last().find("a").text()).toBe("publisher");
  197. expect(row.$el.children().last().css("display")).toBe("none");
  198. row.columns.remove(row.columns.first());
  199. expect(row.$el.children().length).toBe(3);
  200. expect(row.$el.children().first()[0].outerHTML).toBe('<th><a>year<b class="sort-caret"></b></a></th>');
  201. });
  202. });
  203. describe("A Header", function () {
  204. it("throws TypeError if a list of column definitions is not given", function () {
  205. expect(function () {
  206. new Backgrid.Header({
  207. collection: new Backbone.Collection()
  208. });
  209. }).toThrow(new TypeError("'columns' is required"));
  210. });
  211. it("throws TypeError if a collection is not given", function () {
  212. expect(function () {
  213. new Backgrid.Header({
  214. columns: [{
  215. name: "title",
  216. cell: "string"
  217. }]
  218. });
  219. }).toThrow(new TypeError("'collection' is required"));
  220. });
  221. var Book = Backbone.Model.extend({});
  222. var Books = Backbone.Collection.extend({
  223. model: Book
  224. });
  225. var books;
  226. var head;
  227. beforeEach(function () {
  228. books = new Books([{
  229. title: "Alice's Adventures in Wonderland",
  230. year: 1865
  231. }, {
  232. title: "A Tale of Two Cities",
  233. year: 1859
  234. }, {
  235. title: "The Catcher in the Rye",
  236. year: 1951
  237. }]);
  238. head = new Backgrid.Header({
  239. columns: [{
  240. name: "name",
  241. cell: "string"
  242. }, {
  243. name: "year",
  244. cell: "integer"
  245. }],
  246. collection: books
  247. });
  248. head.render();
  249. });
  250. it("renders a header with a row of header cells", function () {
  251. expect(head.$el[0].tagName).toBe("THEAD");
  252. expect(head.$el[0].innerHTML).toBe('<tr><th><a>name<b class="sort-caret"></b></a></th>' +
  253. '<th><a>year<b class="sort-caret"></b></a></th></tr>');
  254. });
  255. });