~caneypuggies/reformedchurcheslocator/couchapp-backbone

« back to all changes in this revision

Viewing changes to _attachments/js/vendor/backgrid/test/grid.js

  • Committer: Tim Black
  • Date: 2013-09-16 22:50:16 UTC
  • Revision ID: tim@alwaysreformed.com-20130916225016-zk8jiba25z33ew7h
Versioned Bower vendor directory

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  backgrid
 
3
  http://github.com/wyuenho/backgrid
 
4
 
 
5
  Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors
 
6
  Licensed under the MIT @license.
 
7
*/
 
8
describe("A Grid", function () {
 
9
 
 
10
  var Book = Backbone.Model.extend({});
 
11
  var Books = Backbone.Collection.extend({
 
12
    model: Book
 
13
  });
 
14
 
 
15
  var books;
 
16
  var grid;
 
17
  beforeEach(function () {
 
18
    books = new Books([{
 
19
      id: 1,
 
20
      title: "Alice's Adventures in Wonderland"
 
21
    }, {
 
22
      id: 2,
 
23
      title: "A Tale of Two Cities"
 
24
    }, {
 
25
      id: 3,
 
26
      title: "The Catcher in the Rye"
 
27
    }]);
 
28
 
 
29
    grid = new Backgrid.Grid({
 
30
      columns: [{
 
31
        name: "title",
 
32
        cell: "string"
 
33
      }],
 
34
      collection: books,
 
35
      footer: Backgrid.Footer
 
36
    });
 
37
  });
 
38
 
 
39
  it("throws TypeError if a list of column definition is not given", function () {
 
40
    expect(function () {
 
41
      new Backgrid.Grid({
 
42
        collection: books
 
43
      });
 
44
    }).toThrow(new TypeError("'columns' is required"));
 
45
  });
 
46
 
 
47
  it("throws TypeError if a collection is not given", function () {
 
48
    expect(function () {
 
49
      new Backgrid.Grid({
 
50
        columns: [{
 
51
          name: "title",
 
52
          cell: "string"
 
53
        }]
 
54
      });
 
55
    }).toThrow(new TypeError("'collection' is required"));
 
56
  });
 
57
 
 
58
  it("renders a table with a header, body and an optional footer section", function () {
 
59
 
 
60
    spyOn(grid, "trigger");
 
61
    spyOn(grid.header, "render").andCallThrough();
 
62
    spyOn(grid.footer, "render").andCallThrough();
 
63
    spyOn(grid.body, "render").andCallThrough();
 
64
 
 
65
    grid.render();
 
66
 
 
67
    expect(grid.el.tagName).toBe("TABLE");
 
68
    expect(grid.header.render.calls.length).toBe(1);
 
69
    expect(grid.footer.render.calls.length).toBe(1);
 
70
    expect(grid.body.render.calls.length).toBe(1);
 
71
    expect(grid.trigger.calls.length).toBe(1);
 
72
    expect(grid.trigger).toHaveBeenCalledWith("backgrid:rendered", grid);
 
73
  });
 
74
 
 
75
  it("will render a table with the header, body, footer and row classes supplied in the constructor options", function () {
 
76
 
 
77
    var CustomHeader = Backgrid.Header.extend({});
 
78
    var CustomBody = Backgrid.Body.extend({});
 
79
    var CustomRow = Backgrid.Row.extend({});
 
80
    var CustomFooter = Backgrid.Footer.extend({});
 
81
 
 
82
    grid = new Backgrid.Grid({
 
83
      columns: [{
 
84
        name: "title",
 
85
        cell: "string"
 
86
      }],
 
87
      collection: books,
 
88
      header: CustomHeader,
 
89
      body: CustomBody,
 
90
      row: CustomRow,
 
91
      footer: CustomFooter,
 
92
      className: "class-name"
 
93
    });
 
94
 
 
95
    grid.render();
 
96
 
 
97
    expect(grid.header instanceof CustomHeader).toBe(true);
 
98
    expect(grid.body instanceof CustomBody).toBe(true);
 
99
    expect(grid.body.rows[0] instanceof CustomRow).toBe(true);
 
100
    expect(grid.footer instanceof CustomFooter).toBe(true);
 
101
 
 
102
    expect(grid.header.className).not.toBe("class-name");
 
103
    expect(grid.body.className).not.toBe("class-name");
 
104
    expect(grid.body.rows[0].className).not.toBe("class-name");
 
105
    expect(grid.footer.className).not.toBe("class-name");
 
106
  });
 
107
 
 
108
  it("will clean up all its decendant views when remove is called", function () {
 
109
    expect(grid.remove().constructor).toBe(Backgrid.Grid);
 
110
  });
 
111
 
 
112
  it("will refresh on columns reset", function () {
 
113
    grid.render();
 
114
    grid.columns.reset([{
 
115
      name: "id",
 
116
      cell: "integer"
 
117
    }]);
 
118
    expect(grid.el.innerHTML).toBe('<thead><tr><th><a>id<b class="sort-caret"></b></a></th></tr></thead>' +
 
119
                                   '<tfoot></tfoot>' +
 
120
                                   '<tbody><tr><td class="integer-cell">1</td></tr>' +
 
121
                                   '<tr><td class="integer-cell">2</td></tr>' +
 
122
                                   '<tr><td class="integer-cell">3</td></tr></tbody>');
 
123
  });
 
124
 
 
125
});