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 Row", function () { |
9
|
|
|
|
10
|
|
|
it("renders a row of cells using a model's values and a list of column definitions", function () { |
11
|
|
|
var row = new Backgrid.Row({ |
12
|
|
|
model: new Backbone.Model({ |
13
|
|
|
name: "name", |
14
|
|
|
age: 18 |
15
|
|
|
}), |
16
|
|
|
columns: [{ |
17
|
|
|
name: "name", |
18
|
|
|
cell: "string" |
19
|
|
|
}, { |
20
|
|
|
name: "age", |
21
|
|
|
cell: "integer" |
22
|
|
|
}] |
23
|
|
|
}); |
24
|
|
|
|
25
|
|
|
row.render(); |
26
|
|
|
|
27
|
|
|
expect(row.el.tagName).toBe("TR"); |
28
|
|
|
|
29
|
|
|
var $tds = row.$el.children(); |
30
|
|
|
expect($tds.eq(0).text()).toBe("name"); |
31
|
|
|
expect($tds.eq(1).text()).toBe("18"); |
32
|
|
|
}); |
33
|
|
|
|
34
|
|
|
it("hides or shows a cell if a column's renderable attribute changes", function () { |
35
|
|
|
|
36
|
|
|
var row = new Backgrid.Row({ |
37
|
|
|
model: new Backbone.Model({ |
38
|
|
|
name: "name" |
39
|
|
|
}), |
40
|
|
|
columns: [{ |
41
|
|
|
name: "name", |
42
|
|
|
cell: "string" |
43
|
|
|
}] |
44
|
|
|
}); |
45
|
|
|
|
46
|
|
|
row.render(); |
47
|
|
|
|
48
|
|
|
var $tds = row.$el.children(); |
49
|
|
|
expect($tds.eq(0).text()).toBe("name"); |
50
|
|
|
expect($tds.eq(0).hasClass("renderable")).toBe(true); |
51
|
|
|
|
52
|
|
|
row.columns.at(0).set("renderable", false); |
53
|
|
|
$tds = row.$el.children(); |
54
|
|
|
expect($tds.eq(0).text()).toBe("name"); |
55
|
|
|
expect($tds.eq(0).hasClass("renderable")).toBe(false); |
56
|
|
|
|
57
|
|
|
row.columns.at(0).set("renderable", true); |
58
|
|
|
$tds = row.$el.children(); |
59
|
|
|
expect($tds.eq(0).text()).toBe("name"); |
60
|
|
|
expect($tds.eq(0).hasClass("renderable")).toBe(true); |
61
|
|
|
}); |
62
|
|
|
|
63
|
|
|
it("inserts or removes a cell if a column is added or removed", function () { |
64
|
|
|
var row = new Backgrid.Row({ |
65
|
|
|
model: new Backbone.Model({ |
66
|
|
|
name: "name", |
67
|
|
|
age: 18, |
68
|
|
|
birthday: "1987-06-05" |
69
|
|
|
}), |
70
|
|
|
columns: [{ |
71
|
|
|
name: "name", |
72
|
|
|
cell: "string" |
73
|
|
|
}] |
74
|
|
|
}); |
75
|
|
|
|
76
|
|
|
row.render(); |
77
|
|
|
|
78
|
|
|
row.columns.add({ |
79
|
|
|
name: "age", |
80
|
|
|
cell: "integer" |
81
|
|
|
}); |
82
|
|
|
var $tds = row.$el.children(); |
83
|
|
|
expect($tds.length).toBe(2); |
84
|
|
|
expect($tds.eq(1).text()).toBe("18"); |
85
|
|
|
|
86
|
|
|
row.columns.add({ |
87
|
|
|
name: "birthday", |
88
|
|
|
cell: "date", |
89
|
|
|
renderable: false |
90
|
|
|
}); |
91
|
|
|
$tds = row.$el.children(); |
92
|
|
|
expect($tds.length).toBe(3); |
93
|
|
|
expect($tds.last().text()).toBe("1987-06-05"); |
94
|
|
|
|
95
|
|
|
row.columns.remove(row.columns.first()); |
96
|
|
|
$tds = row.$el.children(); |
97
|
|
|
expect($tds.length).toBe(2); |
98
|
|
|
expect($tds.first().text()).toBe("18"); |
99
|
|
|
expect($tds.last().text()).toBe("1987-06-05"); |
100
|
|
|
}); |
101
|
|
|
|
102
|
|
|
}); |
103
|
|
|
|
104
|
|
|
describe("A Empty Row", function () { |
105
|
|
|
var row; |
106
|
|
|
|
107
|
|
|
beforeEach(function () { |
108
|
|
|
row = new Backgrid.EmptyRow({ |
109
|
|
|
emptyText: " ", |
110
|
|
|
columns: [{ |
111
|
|
|
name: "title", |
112
|
|
|
cell: "string" |
113
|
|
|
}, { |
114
|
|
|
name: "author", |
115
|
|
|
cell: "string" |
116
|
|
|
}] |
117
|
|
|
}); |
118
|
|
|
|
119
|
|
|
row.render(); |
120
|
|
|
}); |
121
|
|
|
|
122
|
|
|
it("renders a table row", function () { |
123
|
|
|
expect(row.el.tagName).toEqual("TR"); |
124
|
|
|
}); |
125
|
|
|
|
126
|
|
|
it("sets a css class", function () { |
127
|
|
|
expect($(row.el).hasClass("empty")).toBe(true); |
128
|
|
|
}); |
129
|
|
|
|
130
|
|
|
it("renders a single column", function () { |
131
|
|
|
expect($(row.el).find("td").length).toEqual(1); |
132
|
|
|
}); |
133
|
|
|
|
134
|
|
|
it("spans the columns", function () { |
135
|
|
|
expect($(row.el).find("td").attr("colspan")).toEqual("2"); |
136
|
|
|
}); |
137
|
|
|
|
138
|
|
|
it("sets the content to a space by default", function () { |
139
|
|
|
expect($(row.el).find("td").text()).toEqual(" "); |
140
|
|
|
}); |
141
|
|
|
|
142
|
|
|
it("accepts a string option for the text in the row", function () { |
143
|
|
|
row = new Backgrid.EmptyRow({ |
144
|
|
|
columns: [{ |
145
|
|
|
name: "title" |
146
|
|
|
}, { |
147
|
|
|
name: "author" |
148
|
|
|
}], |
149
|
|
|
emptyText: "No data" |
150
|
|
|
}); |
151
|
|
|
|
152
|
|
|
row.render(); |
153
|
|
|
|
154
|
|
|
expect($(row.el).find("td").text()).toEqual("No data"); |
155
|
|
|
}); |
156
|
|
|
|
157
|
|
|
it("accepts a function option for the text in the row", function () { |
158
|
|
|
row = new Backgrid.EmptyRow({ |
159
|
|
|
columns: [{ |
160
|
|
|
name: "title" |
161
|
|
|
}, { |
162
|
|
|
name: "author" |
163
|
|
|
}], |
164
|
|
|
emptyText: function () { |
165
|
|
|
return "No data"; |
166
|
|
|
} |
167
|
|
|
}); |
168
|
|
|
|
169
|
|
|
row.render(); |
170
|
|
|
|
171
|
|
|
expect($(row.el).find("td").text()).toEqual("No data"); |
172
|
|
|
}); |
173
|
|
|
|
174
|
|
|
it("clears the content between two renderings", function () { |
175
|
|
|
row.render(); |
176
|
|
|
|
177
|
|
|
expect($(row.el).find("td").length).toEqual(1); |
178
|
|
|
}); |
179
|
|
|
}); |
180
|
|
|
|