forked from angular-ui/bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtabsSpec.js
379 lines (301 loc) · 11.4 KB
/
tabsSpec.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
describe('tabs', function() {
var elm, scope;
// load the tabs code
beforeEach(module('ui.bootstrap.tabs'));
// load the templates
beforeEach(module('template/tabs/tabs.html', 'template/tabs/pane.html'));
beforeEach(inject(function($rootScope, $compile) {
// we might move this tpl into an html file as well...
elm = angular.element(
'<div>' +
'<tabs>' +
'<pane heading="First Tab">' +
'first content is {{first}}' +
'</pane>' +
'<pane heading="Second Tab">' +
'second content is {{second}}' +
'</pane>' +
'</tabs>' +
'</div>');
scope = $rootScope;
$compile(elm)(scope);
scope.$digest();
}));
it('should create clickable titles', inject(function($compile, $rootScope) {
var titles = elm.find('ul.nav-tabs li a');
expect(titles.length).toBe(2);
expect(titles.eq(0).text()).toBe('First Tab');
expect(titles.eq(1).text()).toBe('Second Tab');
}));
it('should bind the content', function() {
var contents = elm.find('div.tab-content div.tab-pane');
expect(contents.length).toBe(2);
expect(contents.eq(0).text()).toBe('first content is ');
expect(contents.eq(1).text()).toBe('second content is ');
scope.$apply(function() {
scope.first = 123;
scope.second = 456;
});
expect(contents.eq(0).text()).toBe('first content is 123');
expect(contents.eq(1).text()).toBe('second content is 456');
});
it('should set active class on title', function() {
var titles = elm.find('ul.nav-tabs li');
expect(titles.eq(0)).toHaveClass('active');
expect(titles.eq(1)).not.toHaveClass('active');
});
it('should set active class on content', function() {
var contents = elm.find('div.tab-content div.tab-pane');
expect(contents.eq(0)).toHaveClass('active');
expect(contents.eq(1)).not.toHaveClass('active');
});
it('should change active and display on pane when title clicked', function() {
var titles = elm.find('ul.nav-tabs li');
var contents = elm.find('div.tab-content div.tab-pane');
// click the second tab
titles.eq(1).find('a').click();
// second title should be active
expect(titles.eq(0)).not.toHaveClass('active');
expect(titles.eq(1)).toHaveClass('active');
// second content should be active and visible
expect(contents.eq(0)).not.toHaveClass('active');
expect(contents.eq(0).css('display')).toBe('none');
expect(contents.eq(1)).toHaveClass('active');
expect(contents.eq(1).css('display')).not.toBe('none');
});
});
describe('remote selection', function() {
var elm, scope;
// load the tabs code
beforeEach(module('ui.bootstrap.tabs'));
// load the templates
beforeEach(module('template/tabs/tabs.html', 'template/tabs/pane.html'));
beforeEach(inject(function($rootScope, $compile) {
// we might move this tpl into an html file as well...
elm = angular.element(
'<div>' +
'<tabs>' +
'<pane ng-repeat="pane in panes" active="pane.active" heading="pane.title">' +
'{{pane.content}}}' +
'</pane>' +
'</tabs>' +
'</div>'
);
scope = $rootScope;
scope.panes = [
{ title:"Dynamic Title 1", content:"Dynamic content 1", active:true},
{ title:"Dynamic Title 2", content:"Dynamic content 2" }
];
$compile(elm)(scope);
scope.$digest();
}));
it('should handle select attribute when select/deselect', function() {
var titles = elm.find('ul.nav-tabs li');
scope.$apply('panes[1].active=true');
expect(titles.eq(1)).toHaveClass('active');
titles.eq(0).find('a').click();
expect(scope.panes[1].active).toBe(false);
});
it('should select last active tab when multiple panes evaluate to active=true', function() {
var titles = elm.find('ul.nav-tabs li');
scope.$apply('panes[0].active=true;panes[1].active=true');
expect(titles.eq(1)).toHaveClass('active');
});
it('should deselect all panes when all atrributes set to false', function() {
var titles = elm.find('ul.nav-tabs li');
scope.$apply('panes[0].active=false');
expect(titles.eq(0)).not.toHaveClass('active');
expect(titles.eq(1)).not.toHaveClass('active');
});
});
describe('tabs controller', function() {
var scope, ctrl;
beforeEach(module('ui.bootstrap.tabs'));
beforeEach(inject(function($controller, $rootScope) {
scope = $rootScope;
// instantiate the controller stand-alone, without the directive
ctrl = $controller('TabsController', {$scope: scope, $element: null});
}));
describe('select', function() {
it('should mark given pane selected', function() {
var pane = {};
scope.select(pane);
expect(pane.selected).toBe(true);
});
it('should deselect other panes', function() {
var pane1 = {}, pane2 = {}, pane3 = {};
ctrl.addPane(pane1);
ctrl.addPane(pane2);
ctrl.addPane(pane3);
scope.select(pane1);
expect(pane1.selected).toBe(true);
expect(pane2.selected).toBe(false);
expect(pane3.selected).toBe(false);
scope.select(pane2);
expect(pane1.selected).toBe(false);
expect(pane2.selected).toBe(true);
expect(pane3.selected).toBe(false);
scope.select(pane3);
expect(pane1.selected).toBe(false);
expect(pane2.selected).toBe(false);
expect(pane3.selected).toBe(true);
});
});
describe('addPane', function() {
it('should append pane', function() {
var pane1 = {}, pane2 = {};
expect(scope.panes).toEqual([]);
ctrl.addPane(pane1);
expect(scope.panes).toEqual([pane1]);
ctrl.addPane(pane2);
expect(scope.panes).toEqual([pane1, pane2]);
});
it('should select the first one', function() {
var pane1 = {}, pane2 = {};
ctrl.addPane(pane1);
expect(pane1.selected).toBe(true);
ctrl.addPane(pane2);
expect(pane1.selected).toBe(true);
});
});
});
describe('remove tabs', function() {
beforeEach(module("ui.bootstrap.tabs", "template/tabs/tabs.html", "template/tabs/pane.html"));
it('should remove title panes when elements are destroyed and change selection', inject(function($controller, $compile, $rootScope) {
var scope = $rootScope;
var elm = $compile("<tabs><pane heading='1'>Hello</pane><pane ng-repeat='i in list' heading='tab {{i}}'>content {{i}}</pane></tabs>")(scope);
scope.$apply();
function titles() {
return elm.find('ul.nav-tabs li');
}
function panes() {
return elm.find('div.tab-pane');
}
expect(titles().length).toBe(1);
expect(panes().length).toBe(1);
scope.$apply('list = [1,2,3]');
expect(titles().length).toBe(4);
expect(panes().length).toBe(4);
titles().find('a').eq(3).click();
expect(panes().eq(3)).toHaveClass('active');
expect(panes().eq(3).text().trim()).toBe('content 3');
expect(titles().eq(3)).toHaveClass('active');
expect(titles().eq(3).text().trim()).toBe('tab 3');
scope.$apply('list = [1,2]');
expect(panes().length).toBe(3);
expect(titles().length).toBe(3);
expect(panes().eq(2)).toHaveClass('active');
expect(panes().eq(2).text().trim()).toBe('content 2');
expect(titles().eq(2)).toHaveClass('active');
expect(titles().eq(2).text().trim()).toBe('tab 2');
}));
});
describe('keep stable tab order', function() {
// load the tabs code
beforeEach(module('ui.bootstrap.tabs'));
// load the templates
beforeEach(module('template/tabs/tabs.html', 'template/tabs/pane.html'));
it('should reintroduce pane to proper position', inject(function($compile, $rootScope) {
// we might move this tpl into an html file as well...
var elm = angular.element(
'<div>' +
'<tabs>' +
'<pane ng-repeat="pane in panes | filter:paneIsAvailable" active="pane.active" heading="{{pane.title}}">' +
'some content' +
'</pane>' +
'</tabs>' +
'</div>'
);
var scope = $rootScope;
scope.panes = [
{ title:"Title 1", available:true },
{ title:"Title 2", available:true },
{ title:"Title 3", available:true }
];
scope.paneIsAvailable = function(pane) {
return pane.available;
};
$compile(elm)(scope);
scope.$digest();
function titles() {
return elm.find('ul.nav-tabs li');
}
expect(titles().length).toBe(3);
scope.$apply('panes[1].available=false');
scope.$digest();
expect(titles().length).toBe(2);
scope.$apply('panes[1].available=true');
scope.$digest();
expect(titles().length).toBe(3);
expect(titles().eq(0).text().trim()).toBe("Title 1");
expect(titles().eq(1).text().trim()).toBe("Title 2");
expect(titles().eq(2).text().trim()).toBe("Title 3");
}));
it('should reintroduce pane to proper position even with garbage', inject(function($compile, $rootScope) {
// we might move this tpl into an html file as well...
var elm = angular.element(
'<div>' +
'<tabs>' +
' <!-- a comment -->' +
' <div>div that makes troubles</div>' +
'<pane heading="first">First Static</pane>' +
' <div>another div that may do evil</div>' +
'<pane ng-repeat="pane in panes | filter:paneIsAvailable" active="pane.active" heading="{{pane.title}}">some content</pane>' +
' <!-- another comment -->' +
'<pane heading="mid">Mid Static</pane>' +
' a text node' +
' <!-- another comment -->' +
' <span>yet another span that may do evil</span>' +
'<pane ng-repeat="pane in panes | filter:paneIsAvailable" active="pane.active" heading="Second {{pane.title}}">some content</pane>' +
' a text node' +
' <span>yet another span that may do evil</span>' +
' <!-- another comment -->' +
'<pane heading="last">Last Static</pane>' +
' a text node' +
' <span>yet another span that may do evil</span>' +
' <!-- another comment -->' +
'</tabs>' +
'</div>'
);
var scope = $rootScope;
scope.panes = [
{ title:"Title 1", available:true },
{ title:"Title 2", available:true },
{ title:"Title 3", available:true }
];
scope.paneIsAvailable = function(pane) {
return pane.available;
};
function titles() {
return elm.find('ul.nav-tabs li');
}
$compile(elm)(scope);
scope.$digest();
expect(titles().length).toBe(9);
scope.$apply('panes[1].available=false');
scope.$digest();
expect(titles().length).toBe(7);
scope.$apply('panes[0].available=false');
scope.$digest();
expect(titles().length).toBe(5);
scope.$apply('panes[2].available=false');
scope.$digest();
expect(titles().length).toBe(3);
scope.$apply('panes[0].available=true');
scope.$digest();
expect(titles().length).toBe(5);
scope.$apply('panes[1].available=true');
scope.$apply('panes[2].available=true');
scope.$digest();
expect(titles().length).toBe(9);
expect(titles().eq(0).text().trim()).toBe("first");
expect(titles().eq(1).text().trim()).toBe("Title 1");
expect(titles().eq(2).text().trim()).toBe("Title 2");
expect(titles().eq(3).text().trim()).toBe("Title 3");
expect(titles().eq(4).text().trim()).toBe("mid");
expect(titles().eq(5).text().trim()).toBe("Second Title 1");
expect(titles().eq(6).text().trim()).toBe("Second Title 2");
expect(titles().eq(7).text().trim()).toBe("Second Title 3");
expect(titles().eq(8).text().trim()).toBe("last");
}));
});