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

/test/header.js

https://github.com/rhinoman/backgrid
JavaScript | 363 lines | 285 code | 71 blank | 7 comment | 0 complexity | c22c25c21363df0718ae9aa669299e10 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("with the sortType to `toggle`, sorts the underlying collection in ascending order upon clicking the sort caret once", function(){
  63. cell.column.set("sortType", "toggle");
  64. cell.$el.find("a").click();
  65. expect(cell.direction()).toBe("ascending");
  66. expect(cell.collection.toJSON()).toEqual([{id: 1}, {id: 2}, {id: 3}]);
  67. });
  68. it("with the sortType to `toggle`, sorts the underlying collection in descending order upon clicking the sort caret twice", function(){
  69. cell.column.set("sortType", "toggle");
  70. cell.$el.find("a").click().click();
  71. expect(cell.direction()).toBe("descending");
  72. expect(cell.collection.toJSON()).toEqual([{id: 3}, {id: 2}, {id: 1}]);
  73. });
  74. it("with the sortType to `toggle`, sorts the underlying collection back in ascending order upon clicking the sort caret thrice", function(){
  75. cell.column.set("sortType", "toggle");
  76. cell.$el.find("a").click().click().click();
  77. expect(cell.direction()).toBe("ascending");
  78. expect(cell.collection.toJSON()).toEqual([{id: 1}, {id: 2}, {id: 3}]);
  79. });
  80. it("sorts the underlying collection using a custom value extractor upon clicking the sort caret", function() {
  81. var sortValue = function (model, attr) {
  82. return 3 - model.get(attr);
  83. };
  84. cell = new Backgrid.HeaderCell({
  85. collection: col,
  86. column: {
  87. name: "id",
  88. cell: "integer",
  89. sortValue: sortValue
  90. },
  91. }).render();
  92. cell.$el.find("a").click();
  93. expect(cell.collection.toJSON()).toEqual([{id: 3}, {id: 2}, {id: 1}]);
  94. });
  95. it("can sort on a server-mode Backbone.PageableCollection", function () {
  96. var oldAjax = $.ajax;
  97. $.ajax = function (settings) {
  98. settings.success([{"total_entries": 3}, [{id: 2}, {id: 1}]]);
  99. };
  100. var books = new Backbone.PageableCollection([{id: 1}, {id: 2}], {
  101. url: "test-headercell",
  102. state: {
  103. pageSize: 3
  104. }
  105. });
  106. cell = new Backgrid.HeaderCell({
  107. column: {
  108. name: "title",
  109. cell: "string"
  110. },
  111. collection: books
  112. });
  113. cell.render();
  114. expect(cell.collection.at(0).get("id")).toBe(1);
  115. expect(cell.collection.at(1).get("id")).toBe(2);
  116. cell.$el.find("a").click().click();
  117. expect(cell.collection.at(0).get("id")).toBe(2);
  118. expect(cell.collection.at(1).get("id")).toBe(1);
  119. $.ajax = oldAjax;
  120. });
  121. it("can sort on a client-mode Backbone.PageableCollection", function () {
  122. var books = new Backbone.PageableCollection([{
  123. title: "Alice's Adventures in Wonderland"
  124. }, {
  125. title: "A Tale of Two Cities"
  126. }, {
  127. title: "The Catcher in the Rye"
  128. }], {
  129. state: {
  130. pageSize: 1
  131. },
  132. mode: "client"
  133. });
  134. cell = new Backgrid.HeaderCell({
  135. column: {
  136. name: "title",
  137. cell: "string",
  138. sortValue: function (model, attr) {
  139. return model.get(attr).length;
  140. }
  141. },
  142. collection: books
  143. });
  144. cell.render();
  145. cell.$el.find("a").click();
  146. expect(cell.collection.toJSON()).toEqual([{
  147. title: "A Tale of Two Cities"
  148. }]);
  149. cell.collection.getPage(2);
  150. expect(cell.collection.toJSON()).toEqual([{
  151. title: "The Catcher in the Rye"
  152. }]);
  153. cell.collection.getPage(3);
  154. expect(cell.collection.toJSON()).toEqual([{
  155. title: "Alice's Adventures in Wonderland"
  156. }]);
  157. cell.collection.getFirstPage();
  158. cell.$el.find("a").click();
  159. expect(cell.collection.toJSON()).toEqual([{
  160. title: "Alice's Adventures in Wonderland"
  161. }]);
  162. cell.$el.find("a").click();
  163. expect(cell.collection.toJSON()).toEqual([{
  164. title: "Alice's Adventures in Wonderland"
  165. }]);
  166. });
  167. });
  168. describe("A HeaderRow", function () {
  169. var Book = Backbone.Model.extend({});
  170. var Books = Backbone.Collection.extend({
  171. model: Book
  172. });
  173. var books;
  174. var row;
  175. beforeEach(function () {
  176. books = new Books([{
  177. title: "Alice's Adventures in Wonderland",
  178. year: 1865
  179. }, {
  180. title: "A Tale of Two Cities",
  181. year: 1859
  182. }, {
  183. title: "The Catcher in the Rye",
  184. year: 1951
  185. }]);
  186. row = new Backgrid.HeaderRow({
  187. columns: [{
  188. name: "name",
  189. cell: "string"
  190. }, {
  191. name: "year",
  192. cell: "integer"
  193. }],
  194. collection: books
  195. });
  196. row.render();
  197. });
  198. it("throws TypeError when a list of column definition is not given", function () {
  199. expect(function () {
  200. new Backgrid.HeaderRow({
  201. collection: new Backbone.Collection()
  202. });
  203. }).toThrow(new TypeError("'columns' is required"));
  204. });
  205. it("throws TypeError when a collection is not given", function () {
  206. expect(function () {
  207. new Backgrid.HeaderRow({
  208. columns: [{
  209. name: "name",
  210. cell: "string"
  211. }]
  212. });
  213. }).toThrow(new TypeError("'collection' is required"));
  214. });
  215. it("renders a row of header cells", function () {
  216. expect(row.$el[0].tagName).toBe("TR");
  217. expect(row.$el[0].innerHTML).toBe('<th class="editable sortable renderable"><a>name<b class="sort-caret"></b></a></th>' +
  218. '<th class="editable sortable renderable"><a>year<b class="sort-caret"></b></a></th>');
  219. });
  220. it("resets the carets of the non-sorting columns", function () {
  221. row.$el.find("a").eq(0).click(); // ascending
  222. row.$el.find("a").eq(1).click(); // ascending, resets the previous
  223. expect(row.$el.find("a").eq(0).hasClass("ascending")).toBe(false);
  224. expect(row.$el.find("a").eq(1).hasClass("ascending")).toBe(false);
  225. });
  226. it("inserts or removes a cell if a column is added or removed", function () {
  227. row.columns.add({name: "price", cell: "number"});
  228. expect(row.$el.children().length).toBe(3);
  229. expect(row.$el.children().last()[0].outerHTML).toBe('<th class="editable sortable renderable"><a>price<b class="sort-caret"></b></a></th>');
  230. row.columns.add({name: "publisher", cell: "string", renderable: false});
  231. expect(row.$el.children().length).toBe(4);
  232. expect(row.$el.children().last().find("a").text()).toBe("publisher");
  233. expect(row.$el.children().last().hasClass("renderable")).toBe(false);
  234. row.columns.remove(row.columns.first());
  235. expect(row.$el.children().length).toBe(3);
  236. expect(row.$el.children().first()[0].outerHTML).toBe('<th class="editable sortable renderable"><a>year<b class="sort-caret"></b></a></th>');
  237. });
  238. });
  239. describe("A Header", function () {
  240. it("throws TypeError if a list of column definitions is not given", function () {
  241. expect(function () {
  242. new Backgrid.Header({
  243. collection: new Backbone.Collection()
  244. });
  245. }).toThrow(new TypeError("'columns' is required"));
  246. });
  247. it("throws TypeError if a collection is not given", function () {
  248. expect(function () {
  249. new Backgrid.Header({
  250. columns: [{
  251. name: "title",
  252. cell: "string"
  253. }]
  254. });
  255. }).toThrow(new TypeError("'collection' is required"));
  256. });
  257. var Book = Backbone.Model.extend({});
  258. var Books = Backbone.Collection.extend({
  259. model: Book
  260. });
  261. var books;
  262. var head;
  263. beforeEach(function () {
  264. books = new Books([{
  265. title: "Alice's Adventures in Wonderland",
  266. year: 1865
  267. }, {
  268. title: "A Tale of Two Cities",
  269. year: 1859
  270. }, {
  271. title: "The Catcher in the Rye",
  272. year: 1951
  273. }]);
  274. head = new Backgrid.Header({
  275. columns: [{
  276. name: "name",
  277. cell: "string"
  278. }, {
  279. name: "year",
  280. cell: "integer"
  281. }],
  282. collection: books
  283. });
  284. head.render();
  285. });
  286. it("renders a header with a row of header cells", function () {
  287. expect(head.$el[0].tagName).toBe("THEAD");
  288. expect(head.$el[0].innerHTML).toBe('<tr><th class="editable sortable renderable"><a>name<b class="sort-caret"></b></a></th>' +
  289. '<th class="editable sortable renderable"><a>year<b class="sort-caret"></b></a></th></tr>');
  290. });
  291. });