PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/backgrid-0.3.5/test/grid.js

https://github.com/kuzbida/backgrid_test
JavaScript | 143 lines | 116 code | 20 blank | 7 comment | 3 complexity | 98d651452ae3faced04e37708df03132 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 Grid", function () {
  8. var Book = Backbone.Model.extend({});
  9. var Books = Backbone.Collection.extend({
  10. model: Book
  11. });
  12. var books;
  13. var grid;
  14. beforeEach(function () {
  15. books = new Books([{
  16. id: 1,
  17. title: "Alice's Adventures in Wonderland"
  18. }, {
  19. id: 2,
  20. title: "A Tale of Two Cities"
  21. }, {
  22. id: 3,
  23. title: "The Catcher in the Rye"
  24. }]);
  25. grid = new Backgrid.Grid({
  26. columns: [{
  27. name: "title",
  28. cell: "string"
  29. }],
  30. collection: books,
  31. footer: Backgrid.Footer
  32. });
  33. });
  34. it("renders a table with a body, optional header, and an optional footer section", function () {
  35. spyOn(grid, "trigger");
  36. spyOn(grid.header, "render").andCallThrough();
  37. spyOn(grid.footer, "render").andCallThrough();
  38. spyOn(grid.body, "render").andCallThrough();
  39. grid.render();
  40. expect(grid.el.tagName).toBe("TABLE");
  41. expect(grid.header.render.calls.length).toBe(1);
  42. expect(grid.footer.render.calls.length).toBe(1);
  43. expect(grid.body.render.calls.length).toBe(1);
  44. expect(grid.trigger.calls.length).toBe(1);
  45. expect(grid.trigger).toHaveBeenCalledWith("backgrid:rendered", grid);
  46. });
  47. it("will render a table with the header, body, footer and row classes supplied in the constructor options", function () {
  48. var CustomHeader = Backgrid.Header.extend({});
  49. var CustomBody = Backgrid.Body.extend({});
  50. var CustomRow = Backgrid.Row.extend({});
  51. var CustomFooter = Backgrid.Footer.extend({});
  52. grid = new Backgrid.Grid({
  53. columns: [{
  54. name: "title",
  55. cell: "string"
  56. }],
  57. collection: books,
  58. header: CustomHeader,
  59. body: CustomBody,
  60. row: CustomRow,
  61. footer: CustomFooter,
  62. className: "class-name"
  63. });
  64. grid.render();
  65. expect(grid.header instanceof CustomHeader).toBe(true);
  66. expect(grid.body instanceof CustomBody).toBe(true);
  67. expect(grid.body.rows[0] instanceof CustomRow).toBe(true);
  68. expect(grid.footer instanceof CustomFooter).toBe(true);
  69. expect(grid.header.className).not.toBe("class-name");
  70. expect(grid.body.className).not.toBe("class-name");
  71. expect(grid.body.rows[0].className).not.toBe("class-name");
  72. expect(grid.footer.className).not.toBe("class-name");
  73. });
  74. it("will clean up all its decendant views when remove is called", function () {
  75. expect(grid.remove().constructor).toBe(Backgrid.Grid);
  76. });
  77. it("will delegate insertRow, removeRow and sort to the body", function () {
  78. spyOn(grid.body, "insertRow").andCallThrough();
  79. spyOn(grid.body, "removeRow").andCallThrough();
  80. spyOn(grid.body, "sort").andCallThrough();
  81. grid.insertRow({});
  82. expect(grid.body.insertRow).toHaveBeenCalledWith({});
  83. var last = grid.collection.last();
  84. grid.removeRow(last);
  85. expect(grid.body.removeRow).toHaveBeenCalledWith(last);
  86. grid.sort("title", "descending");
  87. expect(grid.body.sort).toHaveBeenCalledWith("title", "descending");
  88. });
  89. it("will delegate to columns.add and columns.remove from insertColumn and removeColumn", function () {
  90. spyOn(grid.columns, "add").andCallThrough();
  91. spyOn(grid.columns, "remove").andCallThrough();
  92. grid.insertColumn({name: "id", cell: "integer"});
  93. expect(grid.columns.add).toHaveBeenCalledWith({name: "id", cell: "integer"});
  94. var col = grid.columns.last();
  95. grid.removeColumn(col);
  96. expect(grid.columns.remove).toHaveBeenCalledWith(col);
  97. });
  98. it("will refresh on columns reset", function () {
  99. grid.render();
  100. grid.columns.reset([{
  101. name: "id",
  102. cell: "integer"
  103. }]);
  104. var thead = grid.el.childNodes[0];
  105. expect(thead.tagName == "THEAD").toBe(true);
  106. expect($(thead).find("tr").length).toBe(1);
  107. expect($(thead).find("tr > th.editable.sortable.renderable.id > a > b.sort-caret").length).toBe(1);
  108. expect($(thead).find("tr > th.editable.sortable.renderable.id > a").text()).toBe("id");
  109. var tfoot = grid.el.childNodes[1];
  110. expect(tfoot.tagName == "TFOOT").toBe(true);
  111. expect(tfoot.childNodes.length).toBe(0);
  112. var tbody = grid.el.lastChild;
  113. expect(tbody.tagName == "TBODY").toBe(true);
  114. expect($(tbody).find("tr").length).toBe(3);
  115. expect($(tbody).find("tr:nth-child(1) > td.integer-cell.editable.sortable.renderable").length).toBe(1);
  116. expect($(tbody).find("tr:nth-child(1) > td.integer-cell.editable.sortable.renderable").text()).toBe("1");
  117. expect($(tbody).find("tr:nth-child(2) > td.integer-cell.editable.sortable.renderable").length).toBe(1);
  118. expect($(tbody).find("tr:nth-child(2) > td.integer-cell.editable.sortable.renderable").text()).toBe("2");
  119. expect($(tbody).find("tr:nth-child(3) > td.integer-cell.editable.sortable.renderable").length).toBe(1);
  120. expect($(tbody).find("tr:nth-child(3) > td.integer-cell.editable.sortable.renderable").text()).toBe("3");
  121. });
  122. });