-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathchildviewContainer.spec.js
248 lines (179 loc) · 6.24 KB
/
childviewContainer.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
describe("childview container", function(){
describe("when providing an array of views to the constructor", function(){
var container;
beforeEach(function(){
var views = [
new Backbone.View(),
new Backbone.View(),
new Backbone.View()
];
container = new Backbone.ChildViewContainer(views);
});
it("should add all of the views", function(){
expect(container.length).toBe(3);
});
});
describe("when adding a view that does not have a model to the container", function(){
var container, view, foundView, indexView;
beforeEach(function(){
view = new Backbone.View();
container = new Backbone.ChildViewContainer();
container.add(view);
foundView = container.findByCid(view.cid);
indexView = container.findByIndex(0);
});
it("should make the view retrievable by the view's cid", function(){
expect(foundView).toBe(view);
});
it("should make the view retrievable by numeric index", function(){
expect(indexView).toBe(view);
});
it("should update the size of the chidren", function(){
expect(container.length).toBe(1);
})
});
describe("when adding a view that has a model, to the container", function(){
var container, view, foundView, model;
beforeEach(function(){
model = new Backbone.Model();
view = new Backbone.View({
model: model
});
container = new Backbone.ChildViewContainer();
container.add(view);
foundView = container.findByModel(model);
});
it("should make the view retrievable by the model", function(){
expect(foundView).toBe(view);
});
});
describe("when adding a view with a custom index value", function(){
var container, view, foundView;
beforeEach(function(){
view = new Backbone.View();
container = new Backbone.ChildViewContainer();
container.add(view, "custom indexer");
foundView = container.findByCustom("custom indexer");
});
it("should make the view retrievable by the custom indexer", function(){
expect(foundView).toBe(view);
});
});
describe("when removing a view", function(){
var container, view, model, cust;
beforeEach(function(){
model = new Backbone.Model();
cust = "custom indexer";
view = new Backbone.View({
model: model
});
container = new Backbone.ChildViewContainer();
container.add(view, cust);
container.remove(view);
});
it("should update the size of the children", function(){
expect(container.length).toBe(0);
});
it("should remove the index by model", function(){
var v = container.findByModel(model);
expect(v).toBeUndefined();
});
it("should remove the index by custom", function(){
var v = container.findByCustom(cust);
expect(v).toBeUndefined();
});
it("should remove the view from the container", function(){
var v = container.findByCid(view.cid);
expect(v).toBeUndefined();
});
});
describe("adding or removing a view", function(){
var container, view, model;
beforeEach(function(){
model = new Backbone.Model();
view = new Backbone.View({
model: model
});
container = new Backbone.ChildViewContainer();
});
it("should return itself when adding, for chaining methods", function(){
expect(container.add(view)).toBe(container);
});
it("should return itself when removing, for chaining methods", function(){
expect(container.remove(view)).toBe(container);
});
});
describe("when a container has 2 views in it", function(){
describe("and applying a method with parameters", function(){
var container, v1, v2;
beforeEach(function(){
v1 = new Backbone.View();
v1.someFunc = jasmine.createSpy("some func");
v2 = new Backbone.View();
v2.someFunc = jasmine.createSpy("some func");
container = new Backbone.ChildViewContainer();
container.add(v1);
container.add(v2);
container.apply("someFunc", ["1", "2"]);
});
it("should call that method on the first view", function(){
expect(v1.someFunc).toHaveBeenCalledWith("1", "2");
});
it("should call that method on the second view", function(){
expect(v2.someFunc).toHaveBeenCalledWith("1", "2");
});
});
describe("and calling a method with parameters", function(){
var container, v1, v2;
beforeEach(function(){
v1 = new Backbone.View();
v1.someFunc = jasmine.createSpy("some func");
v2 = new Backbone.View();
v2.someFunc = jasmine.createSpy("some func");
container = new Backbone.ChildViewContainer();
container.add(v1);
container.add(v2);
container.call("someFunc", "1", "2");
});
it("should call that method on the first view", function(){
expect(v1.someFunc).toHaveBeenCalledWith("1", "2");
});
it("should call that method on the second view", function(){
expect(v2.someFunc).toHaveBeenCalledWith("1", "2");
});
});
describe("and calling a method that doesn't exist on one of the views", function(){
var container, v1, v2;
beforeEach(function(){
v1 = new Backbone.View();
v2 = new Backbone.View();
v2.someFunc = jasmine.createSpy("some func");
container = new Backbone.ChildViewContainer();
container.add(v1);
container.add(v2);
container.call("someFunc", "1", "2");
});
it("should call that method on the second view", function(){
expect(v2.someFunc).toHaveBeenCalledWith("1", "2");
});
});
});
describe("iterators and collection functions", function(){
var container, view, views;
beforeEach(function(){
views = [];
view = new Backbone.View();
container = new Backbone.ChildViewContainer();
container.add(view);
container.each(function(v){
views.push(v);
});
});
it("should provide a .each iterator", function(){
expect(_.isFunction(container.each)).toBe(true);
});
it("should iterate the views with the .each function", function(){
expect(views[0]).toBe(view);
});
});
});