PageRenderTime 63ms CodeModel.GetById 37ms RepoModel.GetById 1ms app.codeStats 0ms

/test/grid.js

https://github.com/antonyraj/backgrid
JavaScript | 125 lines | 100 code | 18 blank | 7 comment | 0 complexity | 8f6bc8d6fefe6b53ed913626eda87c4a 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("throws TypeError if a list of column definition is not given", function () {
  35. expect(function () {
  36. new Backgrid.Grid({
  37. collection: books
  38. });
  39. }).toThrow(new TypeError("'columns' is required"));
  40. });
  41. it("throws TypeError if a collection is not given", function () {
  42. expect(function () {
  43. new Backgrid.Grid({
  44. columns: [{
  45. name: "title",
  46. cell: "string"
  47. }]
  48. });
  49. }).toThrow(new TypeError("'collection' is required"));
  50. });
  51. it("renders a table with a header, body and an optional footer section", function () {
  52. spyOn(grid, "trigger");
  53. spyOn(grid.header, "render").andCallThrough();
  54. spyOn(grid.footer, "render").andCallThrough();
  55. spyOn(grid.body, "render").andCallThrough();
  56. grid.render();
  57. expect(grid.el.tagName).toBe("TABLE");
  58. expect(grid.header.render.calls.length).toBe(1);
  59. expect(grid.footer.render.calls.length).toBe(1);
  60. expect(grid.body.render.calls.length).toBe(1);
  61. expect(grid.trigger.calls.length).toBe(1);
  62. expect(grid.trigger).toHaveBeenCalledWith("backgrid:rendered", grid);
  63. });
  64. it("will render a table with the header, body, footer and row classes supplied in the constructor options", function () {
  65. var CustomHeader = Backgrid.Header.extend({});
  66. var CustomBody = Backgrid.Body.extend({});
  67. var CustomRow = Backgrid.Row.extend({});
  68. var CustomFooter = Backgrid.Footer.extend({});
  69. grid = new Backgrid.Grid({
  70. columns: [{
  71. name: "title",
  72. cell: "string"
  73. }],
  74. collection: books,
  75. header: CustomHeader,
  76. body: CustomBody,
  77. row: CustomRow,
  78. footer: CustomFooter,
  79. className: "class-name"
  80. });
  81. grid.render();
  82. expect(grid.header instanceof CustomHeader).toBe(true);
  83. expect(grid.body instanceof CustomBody).toBe(true);
  84. expect(grid.body.rows[0] instanceof CustomRow).toBe(true);
  85. expect(grid.footer instanceof CustomFooter).toBe(true);
  86. expect(grid.header.className).not.toBe("class-name");
  87. expect(grid.body.className).not.toBe("class-name");
  88. expect(grid.body.rows[0].className).not.toBe("class-name");
  89. expect(grid.footer.className).not.toBe("class-name");
  90. });
  91. it("will clean up all its decendant views when remove is called", function () {
  92. expect(grid.remove().constructor).toBe(Backgrid.Grid);
  93. });
  94. it("will refresh on columns reset", function () {
  95. grid.render();
  96. grid.columns.reset([{
  97. name: "id",
  98. cell: "integer"
  99. }]);
  100. expect(grid.el.innerHTML).toBe('<thead><tr><th><a>id<b class="sort-caret"></b></a></th></tr></thead>' +
  101. '<tfoot></tfoot>' +
  102. '<tbody><tr><td class="integer-cell">1</td></tr>' +
  103. '<tr><td class="integer-cell">2</td></tr>' +
  104. '<tr><td class="integer-cell">3</td></tr></tbody>');
  105. });
  106. });