PageRenderTime 45ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/test/body.js

https://github.com/antonyraj/backgrid
JavaScript | 330 lines | 264 code | 53 blank | 13 comment | 0 complexity | f67db4c67995306b1e2e8b90d6573137 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 Body", function () {
  8. it("throws TypeError if columns is not given", function () {
  9. expect(function () {
  10. new Backgrid.Body({
  11. collection: new Backbone.Collection()
  12. });
  13. }).toThrow(new TypeError("'columns' is required"));
  14. });
  15. it("throws TypeError if collection is not given", function () {
  16. expect(function () {
  17. new Backgrid.Body({
  18. columns: [{
  19. name: "name",
  20. cell: "string"
  21. }]
  22. });
  23. }).toThrow(new TypeError("'collection' is required"));
  24. });
  25. var Book = Backbone.Model.extend({});
  26. var Books = Backbone.Collection.extend({
  27. model: Book
  28. });
  29. var books;
  30. var body;
  31. beforeEach(function () {
  32. books = new Books([{
  33. title: "Alice's Adventures in Wonderland"
  34. }, {
  35. title: "A Tale of Two Cities"
  36. }, {
  37. title: "The Catcher in the Rye"
  38. }]);
  39. body = new Backgrid.Body({
  40. columns: [{
  41. name: "title",
  42. cell: "string"
  43. }],
  44. collection: books
  45. });
  46. body.render();
  47. });
  48. it("renders table rows using the given column definitions and collection", function () {
  49. expect(body.el.tagName).toBe("TBODY");
  50. var $trs = body.$el.children();
  51. expect($trs.length).toBe(3);
  52. expect(body.el.innerHTML).toBe('<tr><td class="string-cell">Alice\'s Adventures in Wonderland</td></tr>' +
  53. '<tr><td class="string-cell">A Tale of Two Cities</td></tr>' +
  54. '<tr><td class="string-cell">The Catcher in the Rye</td></tr>');
  55. });
  56. it("will render a new row if a new model is added to its collection", function () {
  57. body.collection.add({
  58. title: "The Great Gatsby"
  59. });
  60. var $trs = body.$el.children();
  61. expect($trs.length).toBe(4);
  62. expect($trs[3].outerHTML).toBe('<tr><td class="string-cell">The Great Gatsby</td></tr>');
  63. body.collection.add({
  64. title: "Les Misérables"
  65. }, {at: 1});
  66. $trs = body.$el.children();
  67. expect($trs.length).toBe(5);
  68. expect($trs[1].outerHTML).toBe('<tr><td class="string-cell">Les Misérables</td></tr>');
  69. });
  70. it("will render a new row by calling insertRow directly with a new model", function () {
  71. books = new Books();
  72. body = new Backgrid.Body({
  73. columns: [{
  74. name: "title",
  75. cell: "string"
  76. }],
  77. collection: books
  78. });
  79. body.render();
  80. body.insertRow({
  81. title: "The Great Gatsby"
  82. });
  83. var $trs = body.$el.children();
  84. expect($trs.length).toBe(1);
  85. expect($trs[0].outerHTML).toBe('<tr><td class="string-cell">The Great Gatsby</td></tr>');
  86. body.insertRow({
  87. title: "Les Misérables"
  88. }, {at: 0});
  89. $trs = body.$el.children();
  90. expect($trs.length).toBe(2);
  91. expect($trs[0].outerHTML).toBe('<tr><td class="string-cell">Les Misérables</td></tr>');
  92. });
  93. it("will remove a row from the DOM if a model is removed from its collection", function () {
  94. var twocities = body.collection.at(1);
  95. body.collection.remove(twocities);
  96. var $trs = body.$el.children();
  97. expect($trs.length).toBe(2);
  98. expect(body.el.innerHTML).toBe('<tr><td class="string-cell">Alice\'s Adventures in Wonderland</td></tr>' +
  99. '<tr><td class="string-cell">The Catcher in the Rye</td></tr>');
  100. });
  101. it("will remove a row from the DOM is removeRow is called directly with a model", function () {
  102. var twocities = body.collection.at(1);
  103. body.removeRow(twocities);
  104. var $trs = body.$el.children();
  105. expect($trs.length).toBe(2);
  106. expect(body.el.innerHTML).toBe('<tr><td class="string-cell">Alice\'s Adventures in Wonderland</td></tr>' +
  107. '<tr><td class="string-cell">The Catcher in the Rye</td></tr>');
  108. });
  109. it("will refresh if its collection is reset", function () {
  110. var eventFired = false;
  111. var handler = function () {
  112. eventFired = true;
  113. };
  114. body.collection.on("backgrid:refresh", handler);
  115. body.collection.reset([{
  116. title: "Oliver Twist"
  117. }]);
  118. body.collection.off("backgrid:refresh", handler);
  119. expect(eventFired).toBe(true);
  120. var $trs = body.$el.children();
  121. expect($trs.length).toBe(1);
  122. expect(body.el.innerHTML).toBe('<tr><td class="string-cell">Oliver Twist</td></tr>');
  123. });
  124. it("will render rows using the Row class supplied in the constructor options", function () {
  125. var CustomRow = Backgrid.Row.extend({});
  126. spyOn(CustomRow.prototype, "render").andCallThrough();
  127. body = new Backgrid.Body({
  128. columns: [{
  129. name: "title",
  130. cell: "string"
  131. }],
  132. collection: books,
  133. row: CustomRow
  134. });
  135. body.render();
  136. expect(CustomRow.prototype.render).toHaveBeenCalled();
  137. });
  138. describe("maintain page size at page boundary", function () {
  139. var col;
  140. beforeEach(function () {
  141. col = new Backbone.PageableCollection([
  142. {id: 1},
  143. {id: 2},
  144. {id: 3}
  145. ], {
  146. state: {
  147. pageSize: 2
  148. },
  149. mode: "client"
  150. });
  151. body = new Backgrid.Body({
  152. columns: [{
  153. name: "id",
  154. cell: "integer"
  155. }],
  156. collection: col
  157. });
  158. body.render();
  159. });
  160. it("when adding to a full page", function () {
  161. col.add(new Backbone.Model({id: 4}));
  162. expect(body.$el.find("tr").length).toBe(2);
  163. });
  164. it("when removing from a full page", function () {
  165. col.remove(col.get(1));
  166. expect(body.$el.find("tr").length).toBe(2);
  167. });
  168. });
  169. it("will not display the empty row if collection is not empty", function () {
  170. body = new Backgrid.Body({
  171. emptyText: " ",
  172. columns: [{
  173. name: "title",
  174. cell: "string"
  175. }],
  176. collection: books
  177. });
  178. body.render();
  179. expect(body.$el.find("tr.empty").length).toBe(0);
  180. });
  181. it("will not display the empty row if `options.emptyText` is not supplied", function () {
  182. expect(body.$el.find("tr.empty").length).toBe(0);
  183. books.reset();
  184. body = new Backgrid.Body({
  185. columns: [{
  186. name: "title",
  187. cell: "string"
  188. }],
  189. collection: books
  190. });
  191. body.render();
  192. expect(body.$el.find("tr.empty").length).toBe(0);
  193. });
  194. it("will display the empty row if the collection is empty and `options.emptyText` is supplied", function () {
  195. books.reset();
  196. body = new Backgrid.Body({
  197. emptyText: " ",
  198. columns: [{
  199. name: "title",
  200. cell: "string"
  201. }],
  202. collection: books
  203. });
  204. body.render();
  205. expect(body.$el.find("tr.empty").length).toBe(1);
  206. expect(body.$el.find("tr.empty > td").attr("colspan")).toBe("1");
  207. });
  208. it("will clear the empty row if a new model is added to an empty collection", function () {
  209. books.reset();
  210. body = new Backgrid.Body({
  211. emptyText: " ",
  212. columns: [{
  213. name: "title",
  214. cell: "string"
  215. }],
  216. collection: books
  217. });
  218. body.render();
  219. expect(body.$el.find("tr.empty").length).toBe(1);
  220. books.add({name: "Oliver Twist"});
  221. expect(body.$el.find("tr.empty").length).toBe(0);
  222. books.reset();
  223. expect(body.$el.find("tr.empty").length).toBe(1);
  224. body.insertRow({title: "The Catcher in the Rye"});
  225. expect(body.$el.find("tr.empty").length).toBe(0);
  226. });
  227. it("will put the next editable and renderable cell in edit mode when a save or one of the navigation commands is triggered via backgrid:edited from the collection", function () {
  228. var people = new Backbone.Collection([
  229. {name: "alice", age: 28, married: false},
  230. {name: "bob", age: 30, married: true}
  231. ]);
  232. var columns = new Backgrid.Columns([{
  233. name: "name",
  234. cell: "string"
  235. }, {
  236. name: "age",
  237. cell: "integer",
  238. editable: false
  239. }, {
  240. name: "sex",
  241. cell: "boolean",
  242. renderable: false
  243. }]);
  244. var body = new Backgrid.Body({
  245. collection: people,
  246. columns: columns
  247. });
  248. body.render();
  249. body.rows[0].cells[0].enterEditMode();
  250. // right
  251. people.trigger("backgrid:edited", people.at(0), columns.at(0), new Backgrid.Command({keyCode: 9}));
  252. expect(body.rows[0].cells[0].$el.hasClass("editor")).toBe(false);
  253. expect(body.rows[1].cells[0].$el.hasClass("editor")).toBe(true);
  254. // left
  255. people.trigger("backgrid:edited", people.at(1), columns.at(0), new Backgrid.Command({keyCode: 9, shiftKey: true}));
  256. expect(body.rows[0].cells[0].$el.hasClass("editor")).toBe(true);
  257. expect(body.rows[1].cells[0].$el.hasClass("editor")).toBe(false);
  258. // down
  259. people.trigger("backgrid:edited", people.at(0), columns.at(0), new Backgrid.Command({keyCode: 40}));
  260. expect(body.rows[0].cells[0].$el.hasClass("editor")).toBe(false);
  261. expect(body.rows[1].cells[0].$el.hasClass("editor")).toBe(true);
  262. // up
  263. people.trigger("backgrid:edited", people.at(1), columns.at(0), new Backgrid.Command({keyCode: 38}));
  264. expect(body.rows[0].cells[0].$el.hasClass("editor")).toBe(true);
  265. expect(body.rows[1].cells[0].$el.hasClass("editor")).toBe(false);
  266. // enter
  267. people.trigger("backgrid:edited", people.at(0), columns.at(0), new Backgrid.Command({keyCode: 13}));
  268. expect(body.rows[0].cells[0].$el.hasClass("editor")).toBe(false);
  269. expect(body.rows[1].cells[0].$el.hasClass("editor")).toBe(false);
  270. // esc
  271. body.rows[1].cells[0].enterEditMode();
  272. people.trigger("backgrid:edited", people.at(1), columns.at(0), new Backgrid.Command({keyCode: 27}));
  273. expect(body.rows[0].cells[0].$el.hasClass("editor")).toBe(false);
  274. expect(body.rows[1].cells[0].$el.hasClass("editor")).toBe(false);
  275. });
  276. });