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

/test/header.js

https://github.com/TechnotronicOz/backgrid
JavaScript | 401 lines | 328 code | 66 blank | 7 comment | 0 complexity | 2f438a27e8fbf2e8a5041a630e800e53 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. var col;
  9. var cell;
  10. beforeEach(function () {
  11. col = new Backbone.Collection([{id: 2}, {id: 1}, {id: 3}]);
  12. cell = new Backgrid.HeaderCell({
  13. column: {
  14. name: "id",
  15. cell: "integer"
  16. },
  17. collection: col
  18. });
  19. cell.render();
  20. });
  21. it("renders a table header cell with the label text and an optional anchor with sort-caret", function () {
  22. expect(cell.el.tagName).toBe("TH");
  23. expect(cell.$el.find("a").text()).toBe("id");
  24. expect(cell.$el.find(".sort-caret").length).toBe(1);
  25. cell.column.set("sortable", false);
  26. cell.render();
  27. expect(cell.el.tagName).toBe("TH");
  28. expect(cell.$el.text()).toBe("id");
  29. expect(cell.$el.find(".sort-caret").length).toBe(0);
  30. });
  31. it("adds an editable, sortable and a renderable class to the cell if these column attributes are true", function () {
  32. var column = {
  33. name: "title",
  34. cell: "string"
  35. };
  36. cell = new Backgrid.HeaderCell({
  37. column: column,
  38. collection: col
  39. });
  40. expect(cell.$el.hasClass("editable")).toBe(true);
  41. expect(cell.$el.hasClass("sortable")).toBe(true);
  42. expect(cell.$el.hasClass("renderable")).toBe(true);
  43. cell.column.set("editable", false);
  44. expect(cell.$el.hasClass("editable")).toBe(false);
  45. cell.column.set("sortable", false);
  46. expect(cell.$el.hasClass("sortable")).toBe(false);
  47. cell.column.set("renderable", false);
  48. expect(cell.$el.hasClass("renderable")).toBe(false);
  49. var TrueCol = Backgrid.Column.extend({
  50. mySortable: function () { return true; },
  51. myRenderable: function () { return true; },
  52. myEditable: function () { return true; }
  53. });
  54. var FalseCol = Backgrid.Column.extend({
  55. mySortable: function () { return false; },
  56. myRenderable: function () { return false; },
  57. myEditable: function () { return false; }
  58. });
  59. column = new TrueCol({
  60. name: "title",
  61. cell: "string",
  62. sortable: "mySortable",
  63. renderable: "myRenderable",
  64. editable: "myEditable"
  65. });
  66. cell = new Backgrid.HeaderCell({
  67. column: column,
  68. collection: col
  69. });
  70. expect(cell.$el.hasClass("editable")).toBe(true);
  71. expect(cell.$el.hasClass("sortable")).toBe(true);
  72. expect(cell.$el.hasClass("renderable")).toBe(true);
  73. column = new FalseCol({
  74. name: "title",
  75. cell: "string",
  76. sortable: "mySortable",
  77. renderable: "myRenderable",
  78. editable: "myEditable"
  79. });
  80. cell = new Backgrid.HeaderCell({
  81. column: column,
  82. collection: col
  83. });
  84. expect(cell.$el.hasClass("editable")).toBe(false);
  85. expect(cell.$el.hasClass("sortable")).toBe(false);
  86. expect(cell.$el.hasClass("renderable")).toBe(false);
  87. column = new Backgrid.Column({
  88. name: "title",
  89. cell: "string",
  90. sortable: function () { return true; },
  91. editable: function () { return true; },
  92. renderable: function () { return true; }
  93. });
  94. cell = new Backgrid.HeaderCell({
  95. column: column,
  96. collection: col
  97. });
  98. expect(cell.$el.hasClass("editable")).toBe(true);
  99. expect(cell.$el.hasClass("sortable")).toBe(true);
  100. expect(cell.$el.hasClass("renderable")).toBe(true);
  101. });
  102. it("will rerender with the column name and/or label changes", function () {
  103. expect(cell.$el.find("a").text(), "id");
  104. expect(cell.$el.hasClass("id"), true);
  105. cell.column.set("name", "name");
  106. expect(cell.$el.find("name"), true);
  107. expect(cell.$el.hasClass("name"), true);
  108. cell.column.set("label", "Name");
  109. expect(cell.$el.find("a").text(), "Name");
  110. expect(cell.$el.hasClass("Name"), true);
  111. });
  112. it("will put a class indicating the sorting direction if `direction` is set in the column", function () {
  113. cell = new Backgrid.HeaderCell({
  114. column: {
  115. name: "id",
  116. cell: "integer",
  117. direction: "descending"
  118. },
  119. collection: col
  120. });
  121. cell.render();
  122. expect(cell.el.tagName).toBe("TH");
  123. expect(cell.$el.find("a").text()).toBe("id");
  124. expect(cell.$el.find(".sort-caret").length).toBe(1);
  125. expect(cell.$el.hasClass("descending")).toBe(true);
  126. });
  127. it("triggers `backgrid:sort` with the column and direction set to 'ascending' if the column's direction is not set", function () {
  128. var column, direction;
  129. cell.collection.on("backgrid:sort", function (col, dir) { column = col; direction = dir; });
  130. cell.$el.find("a").click();
  131. expect(column).toBe(cell.column);
  132. expect(direction).toBe("ascending");
  133. });
  134. it("triggers `backgrid:sort` with the column and direction set to 'descending' if the column's direction is set to 'ascending'", function () {
  135. var column, direction;
  136. cell.collection.on("backgrid:sort", function (col, dir) { column = col; direction = dir; });
  137. cell.column.set("direction", "ascending");
  138. cell.$el.find("a").click();
  139. expect(column).toBe(cell.column);
  140. expect(direction).toBe("descending");
  141. });
  142. it("triggers `backgrid:sort` with the column and direction set to `null` if the column's direction is set to 'descending'", function () {
  143. var column, direction;
  144. cell.collection.on("backgrid:sort", function (col, dir) { column = col; direction = dir; });
  145. cell.column.set("direction", "descending");
  146. cell.$el.find("a").click();
  147. expect(column).toBe(cell.column);
  148. expect(direction).toBeNull();
  149. });
  150. it("will set the column to the correct direction when `change:direction` is triggered from the column", function () {
  151. cell.column.set("direction", "ascending");
  152. expect(cell.$el.hasClass("ascending")).toBe(true);
  153. cell.column.set("direction", "descending");
  154. expect(cell.$el.hasClass("descending")).toBe(true);
  155. cell.column.set("direction", null);
  156. expect(cell.$el.hasClass("ascending")).toBe(false);
  157. expect(cell.$el.hasClass("descending")).toBe(false);
  158. });
  159. it("will remove its direction CSS class if `sort` is triggered from the collection or pageableCollection#fullCollection", function () {
  160. cell.column.set("direction", "ascending");
  161. cell.collection.comparator = "id";
  162. cell.collection.sort();
  163. expect(cell.$el.hasClass("ascending")).toBe(false);
  164. expect(cell.$el.hasClass("descending")).toBe(false);
  165. col = new Backbone.PageableCollection(col.toJSON(), {
  166. mode: "client"
  167. });
  168. col.setSorting("id", 1);
  169. cell = new Backgrid.HeaderCell({
  170. column: {
  171. name: "id",
  172. cell: "integer"
  173. },
  174. collection: col
  175. });
  176. cell.column.set("direction", "ascending");
  177. cell.collection.fullCollection.comparator = "id";
  178. cell.collection.fullCollection.sort();
  179. expect(cell.$el.hasClass("ascending")).toBe(false);
  180. expect(cell.$el.hasClass("descending")).toBe(false);
  181. });
  182. it("with `sortType` set to `toggle`, triggers `backgrid:sort` with the column and direction set to 'ascending' if the column's direction is not set", function () {
  183. var column, direction;
  184. cell.column.set("sortType", "toggle");
  185. cell.collection.on("backgrid:sort", function (col, dir) { column = col; direction = dir; });
  186. cell.$el.find("a").click();
  187. expect(column).toBe(cell.column);
  188. expect(direction).toBe("ascending");
  189. });
  190. it("with `sortType` set to `toggle`, triggers `backgrid:sort` with the column and direction set to 'descending' if the column's direction is set to 'ascending'", function () {
  191. var column, direction;
  192. cell.column.set("sortType", "toggle");
  193. cell.column.set("direction", "ascending");
  194. cell.collection.on("backgrid:sort", function (col, dir) { column = col; direction = dir; });
  195. cell.$el.find("a").click();
  196. expect(column).toBe(cell.column);
  197. expect(direction).toBe("descending");
  198. });
  199. it("with `sortType` set to `toggle`, triggers `backgrid:sort` with the column and direction set to 'ascending' if the column's direction is set to 'descending'", function () {
  200. var column, direction;
  201. cell.column.set("sortType", "toggle");
  202. cell.column.set("direction", "descending");
  203. cell.collection.on("backgrid:sort", function (col, dir) { column = col; direction = dir; });
  204. cell.$el.find("a").click();
  205. expect(column).toBe(cell.column);
  206. expect(direction).toBe("ascending");
  207. });
  208. });
  209. describe("A HeaderRow", function () {
  210. var Book = Backbone.Model.extend({});
  211. var Books = Backbone.Collection.extend({
  212. model: Book
  213. });
  214. var books;
  215. var row;
  216. beforeEach(function () {
  217. books = new Books([{
  218. title: "Alice's Adventures in Wonderland",
  219. year: 1865
  220. }, {
  221. title: "A Tale of Two Cities",
  222. year: 1859
  223. }, {
  224. title: "The Catcher in the Rye",
  225. year: 1951
  226. }]);
  227. row = new Backgrid.HeaderRow({
  228. columns: [{
  229. name: "name",
  230. cell: "string"
  231. }, {
  232. name: "year",
  233. cell: "integer"
  234. }],
  235. collection: books
  236. });
  237. row.render();
  238. });
  239. it("renders a row of header cells", function () {
  240. expect(row.$el[0].tagName).toBe("TR");
  241. var th1 = $(row.el.childNodes[0]);
  242. expect(th1.hasClass("editable")).toBe(true);
  243. expect(th1.hasClass("sortable")).toBe(true);
  244. expect(th1.hasClass("renderable")).toBe(true);
  245. expect(th1.hasClass("name")).toBe(true);
  246. expect(th1.find("a").text()).toBe("name");
  247. expect(th1.find("a").eq(1).is($("b", {className: "sort-caret"})));
  248. var th2 = $(row.el.childNodes[1]);
  249. expect(th2.hasClass("editable")).toBe(true);
  250. expect(th2.hasClass("sortable")).toBe(true);
  251. expect(th2.hasClass("renderable")).toBe(true);
  252. expect(th2.hasClass("year")).toBe(true);
  253. expect(th2.find("a").text()).toBe("year");
  254. expect(th2.find("a > b:last-child").eq(0).hasClass("sort-caret")).toBe(true);
  255. });
  256. it("resets the carets of the non-sorting columns", function () {
  257. row.$el.find("a").eq(0).click(); // ascending
  258. row.$el.find("a").eq(1).click(); // ascending, resets the previous
  259. expect(row.$el.find("a").eq(0).hasClass("ascending")).toBe(false);
  260. expect(row.$el.find("a").eq(1).hasClass("ascending")).toBe(false);
  261. });
  262. it("inserts or removes a cell if a column is added or removed", function () {
  263. row.columns.add({name: "price", cell: "number"});
  264. expect(row.$el.children().length).toBe(3);
  265. var lastTh = $(row.el.lastChild);
  266. expect(lastTh.hasClass("editable")).toBe(true);
  267. expect(lastTh.hasClass("sortable")).toBe(true);
  268. expect(lastTh.hasClass("renderable")).toBe(true);
  269. expect(lastTh.hasClass("price")).toBe(true);
  270. expect(lastTh.find("a").text()).toBe("price");
  271. expect(lastTh.find("a > b:last-child").eq(0).hasClass("sort-caret")).toBe(true);
  272. row.columns.add({name: "publisher", cell: "string", renderable: false});
  273. expect(row.$el.children().length).toBe(4);
  274. expect(row.$el.children().last().find("a").text()).toBe("publisher");
  275. expect(row.$el.children().last().hasClass("renderable")).toBe(false);
  276. row.columns.remove(row.columns.first());
  277. expect(row.$el.children().length).toBe(3);
  278. var firstTh = $(row.el.firstChild);
  279. expect(firstTh.hasClass("editable")).toBe(true);
  280. expect(firstTh.hasClass("sortable")).toBe(true);
  281. expect(firstTh.hasClass("renderable")).toBe(true);
  282. expect(firstTh.hasClass("year")).toBe(true);
  283. expect(firstTh.find("a").text()).toBe("year");
  284. expect(firstTh.find("a > b:last-child").eq(0).hasClass("sort-caret")).toBe(true);
  285. });
  286. });
  287. describe("A Header", function () {
  288. var Book = Backbone.Model.extend({});
  289. var Books = Backbone.Collection.extend({
  290. model: Book
  291. });
  292. var books;
  293. var head;
  294. beforeEach(function () {
  295. books = new Books([{
  296. title: "Alice's Adventures in Wonderland",
  297. year: 1865
  298. }, {
  299. title: "A Tale of Two Cities",
  300. year: 1859
  301. }, {
  302. title: "The Catcher in the Rye",
  303. year: 1951
  304. }]);
  305. head = new Backgrid.Header({
  306. columns: [{
  307. name: "name",
  308. cell: "string"
  309. }, {
  310. name: "year",
  311. cell: "integer"
  312. }],
  313. collection: books
  314. });
  315. head.render();
  316. });
  317. it("renders a header with a row of header cells", function () {
  318. expect(head.$el[0].tagName).toBe("THEAD");
  319. var th1 = $(head.row.el.childNodes[0]);
  320. expect(th1.hasClass("editable")).toBe(true);
  321. expect(th1.hasClass("sortable")).toBe(true);
  322. expect(th1.hasClass("renderable")).toBe(true);
  323. expect(th1.hasClass("name")).toBe(true);
  324. expect(th1.find("a").text()).toBe("name");
  325. expect(th1.find("a").eq(1).is($("b", {className: "sort-caret"})));
  326. var th2 = $(head.row.el.childNodes[1]);
  327. expect(th2.hasClass("editable")).toBe(true);
  328. expect(th2.hasClass("sortable")).toBe(true);
  329. expect(th2.hasClass("renderable")).toBe(true);
  330. expect(th2.hasClass("year")).toBe(true);
  331. expect(th2.find("a").text()).toBe("year");
  332. expect(th2.find("a > b:last-child").eq(0).hasClass("sort-caret")).toBe(true);
  333. });
  334. });