forked from angular-ui/bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarousel.spec.js
579 lines (495 loc) · 18.8 KB
/
carousel.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
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
describe('carousel', function() {
beforeEach(module('ui.bootstrap.carousel', function($compileProvider, $provide) {
angular.forEach(['ngSwipeLeft', 'ngSwipeRight'], makeMock);
function makeMock(name) {
$provide.value(name + 'Directive', []); //remove existing directive if it exists
$compileProvider.directive(name, function() {
return function(scope, element, attr) {
element.on(name, function() {
scope.$apply(attr[name]);
});
};
});
}
}));
beforeEach(module('template/carousel/carousel.html', 'template/carousel/slide.html'));
var $rootScope, $compile, $controller, $interval, $templateCache;
beforeEach(inject(function(_$rootScope_, _$compile_, _$controller_, _$interval_, _$templateCache_) {
$rootScope = _$rootScope_;
$compile = _$compile_;
$controller = _$controller_;
$interval = _$interval_;
$templateCache = _$templateCache_;
}));
describe('basics', function() {
var elm, scope;
beforeEach(function() {
scope = $rootScope.$new();
scope.slides = [
{active:false,content:'one'},
{active:false,content:'two'},
{active:false,content:'three'}
];
elm = $compile(
'<uib-carousel interval="interval" no-transition="true" no-pause="nopause">' +
'<uib-slide ng-repeat="slide in slides" active="slide.active">' +
'{{slide.content}}' +
'</uib-slide>' +
'</uib-carousel>'
)(scope);
scope.interval = 5000;
scope.nopause = undefined;
scope.$apply();
});
function testSlideActive(slideIndex) {
for (var i=0; i<scope.slides.length; i++) {
if (i == slideIndex) {
expect(scope.slides[i].active).toBe(true);
} else {
expect(scope.slides[i].active).not.toBe(true);
}
}
}
it('should allow overriding of the carousel template', function() {
$templateCache.put('foo/bar.html', '<div>foo</div>');
elm = $compile('<uib-carousel template-url="foo/bar.html"></uib-carousel>')(scope);
$rootScope.$digest();
expect(elm.html()).toBe('foo');
});
it('should allow overriding of the slide template', function() {
$templateCache.put('foo/bar.html', '<div class="slide">bar</div>');
elm = $compile(
'<uib-carousel interval="interval" no-transition="true" no-pause="nopause">' +
'<uib-slide template-url="foo/bar.html"></uib-slide>' +
'</uib-carousel>'
)(scope);
$rootScope.$digest();
var slide = elm.find('.slide');
expect(slide.html()).toBe('bar');
});
it('should set the selected slide to active = true', function() {
expect(scope.slides[0].content).toBe('one');
testSlideActive(0);
scope.$apply('slides[1].active=true');
testSlideActive(1);
});
it('should create clickable prev nav button', function() {
var navPrev = elm.find('a.left');
var navNext = elm.find('a.right');
expect(navPrev.length).toBe(1);
expect(navNext.length).toBe(1);
});
it('should display clickable slide indicators', function () {
var indicators = elm.find('ol.carousel-indicators > li');
expect(indicators.length).toBe(3);
});
it('should stop cycling slides forward when noWrap is truthy', function () {
elm = $compile(
'<uib-carousel interval="interval" no-wrap="noWrap">' +
'<uib-slide ng-repeat="slide in slides" active="slide.active">' +
'{{slide.content}}' +
'</uib-slide>' +
'</uib-carousel>'
)(scope);
scope.noWrap = true;
scope.$apply();
scope = elm.isolateScope();
spyOn(scope, 'pause');
for (var i = 0; i < scope.slides.length - 1; ++i) {
scope.next();
}
testSlideActive(scope.slides.length - 1);
scope.next();
testSlideActive(scope.slides.length - 1);
expect(scope.pause).toHaveBeenCalled();
});
it('should stop cycling slides backward when noWrap is truthy', function () {
elm = $compile(
'<uib-carousel interval="interval" no-wrap="noWrap">' +
'<uib-slide ng-repeat="slide in slides" active="slide.active">' +
'{{slide.content}}' +
'</uib-slide>' +
'</uib-carousel>'
)(scope);
scope.noWrap = true;
scope.$apply();
scope = elm.isolateScope();
spyOn(scope, 'pause');
testSlideActive(0);
scope.prev();
testSlideActive(0);
expect(scope.pause).toHaveBeenCalled();
});
it('should hide navigation when only one slide', function () {
scope.slides=[{active:false,content:'one'}];
scope.$apply();
elm = $compile(
'<uib-carousel interval="interval" no-transition="true">' +
'<uib-slide ng-repeat="slide in slides" active="slide.active">' +
'{{slide.content}}' +
'</uib-slide>' +
'</uib-carousel>'
)(scope);
var indicators = elm.find('ol.carousel-indicators > li');
expect(indicators.length).toBe(0);
var navNext = elm.find('a.right');
expect(navNext.length).toBe(0);
var navPrev = elm.find('a.left');
expect(navPrev.length).toBe(0);
});
it('should show navigation when there are 3 slides', function () {
var indicators = elm.find('ol.carousel-indicators > li');
expect(indicators.length).not.toBe(0);
var navNext = elm.find('a.right');
expect(navNext.length).not.toBe(0);
var navPrev = elm.find('a.left');
expect(navPrev.length).not.toBe(0);
});
it('should go to next when clicking next button', function() {
var navNext = elm.find('a.right');
testSlideActive(0);
navNext.click();
testSlideActive(1);
navNext.click();
testSlideActive(2);
navNext.click();
testSlideActive(0);
});
it('should go to prev when clicking prev button', function() {
var navPrev = elm.find('a.left');
testSlideActive(0);
navPrev.click();
testSlideActive(2);
navPrev.click();
testSlideActive(1);
navPrev.click();
testSlideActive(0);
});
describe('swiping', function() {
it('should go next on swipeLeft', function() {
testSlideActive(0);
elm.triggerHandler('ngSwipeLeft');
testSlideActive(1);
});
it('should go prev on swipeRight', function() {
testSlideActive(0);
elm.triggerHandler('ngSwipeRight');
testSlideActive(2);
});
});
it('should select a slide when clicking on slide indicators', function () {
var indicators = elm.find('ol.carousel-indicators > li');
indicators.eq(1).click();
testSlideActive(1);
});
it('shouldnt go forward if interval is NaN or negative or has no slides', function() {
testSlideActive(0);
var previousInterval = scope.interval;
scope.$apply('interval = -1');
$interval.flush(previousInterval);
testSlideActive(0);
scope.$apply('interval = 1000');
$interval.flush(1000);
testSlideActive(1);
scope.$apply('interval = false');
$interval.flush(1000);
testSlideActive(1);
scope.$apply('interval = 1000');
$interval.flush(1000);
testSlideActive(2);
scope.$apply('slides = []');
$interval.flush(1000);
testSlideActive(2);
});
it('should bind the content to slides', function() {
var contents = elm.find('div.item');
expect(contents.length).toBe(3);
expect(contents.eq(0).text()).toBe('one');
expect(contents.eq(1).text()).toBe('two');
expect(contents.eq(2).text()).toBe('three');
scope.$apply(function() {
scope.slides[0].content = 'what';
scope.slides[1].content = 'no';
scope.slides[2].content = 'maybe';
});
expect(contents.eq(0).text()).toBe('what');
expect(contents.eq(1).text()).toBe('no');
expect(contents.eq(2).text()).toBe('maybe');
});
it('should be playing by default and cycle through slides', function() {
testSlideActive(0);
$interval.flush(scope.interval);
testSlideActive(1);
$interval.flush(scope.interval);
testSlideActive(2);
$interval.flush(scope.interval);
testSlideActive(0);
});
it('should pause and play on mouseover', function() {
testSlideActive(0);
$interval.flush(scope.interval);
testSlideActive(1);
elm.trigger('mouseenter');
testSlideActive(1);
$interval.flush(scope.interval);
testSlideActive(1);
elm.trigger('mouseleave');
$interval.flush(scope.interval);
testSlideActive(2);
});
it('should not pause on mouseover if noPause', function() {
scope.$apply('nopause = true');
testSlideActive(0);
elm.trigger('mouseenter');
$interval.flush(scope.interval);
testSlideActive(1);
elm.trigger('mouseleave');
$interval.flush(scope.interval);
testSlideActive(2);
});
it('should remove slide from dom and change active slide', function() {
scope.$apply('slides[2].active = true');
testSlideActive(2);
scope.$apply('slides.splice(0,1)');
expect(elm.find('div.item').length).toBe(2);
testSlideActive(1);
$interval.flush(scope.interval);
testSlideActive(0);
scope.$apply('slides.splice(1,1)');
expect(elm.find('div.item').length).toBe(1);
testSlideActive(0);
});
it('should change dom when you reassign ng-repeat slides array', function() {
scope.slides=[{content:'new1'},{content:'new2'},{content:'new3'}];
scope.$apply();
var contents = elm.find('div.item');
expect(contents.length).toBe(3);
expect(contents.eq(0).text()).toBe('new1');
expect(contents.eq(1).text()).toBe('new2');
expect(contents.eq(2).text()).toBe('new3');
});
it('should not change if next is clicked while transitioning', function() {
var carouselScope = elm.children().scope();
var next = elm.find('a.right');
testSlideActive(0);
carouselScope.$currentTransition = true;
next.click();
testSlideActive(0);
carouselScope.$currentTransition = null;
next.click();
testSlideActive(1);
});
it('issue 1414 - should not continue running timers after scope is destroyed', function() {
testSlideActive(0);
$interval.flush(scope.interval);
testSlideActive(1);
$interval.flush(scope.interval);
testSlideActive(2);
$interval.flush(scope.interval);
testSlideActive(0);
spyOn($interval, 'cancel').and.callThrough();
scope.$destroy();
expect($interval.cancel).toHaveBeenCalled();
});
describe('slide order', function() {
beforeEach(function() {
scope.slides = [
{active:false,content:'one', id:1},
{active:false,content:'two', id:2},
{active:false,content:'three', id:3}
];
elm = $compile(
'<uib-carousel interval="interval" no-transition="true" no-pause="nopause">' +
'<uib-slide ng-repeat="slide in slides | orderBy: \'id\' " active="slide.active" index="$index">' +
'{{slide.content}}' +
'</uib-slide>' +
'</uib-carousel>'
)(scope);
scope.$apply();
scope.slides[0].id = 3;
scope.slides[1].id = 1;
scope.slides[2].id = 2;
scope.$apply();
});
it('should change dom when an order of the slides was changed', function() {
testSlideActive(0);
var contents = elm.find('div.item');
expect(contents.length).toBe(3);
expect(contents.eq(0).text()).toBe('two');
expect(contents.eq(1).text()).toBe('three');
expect(contents.eq(2).text()).toBe('one');
});
it('should select next after order change', function() {
testSlideActive(0);
var next = elm.find('a.right');
next.click();
testSlideActive(1);
});
it('should select prev after order change', function() {
testSlideActive(0);
var prev = elm.find('a.left');
prev.click();
testSlideActive(2);
});
it('should add slide in the specified position', function() {
testSlideActive(0);
scope.slides[2].id = 4;
scope.slides.push({active:false,content:'four', id:2});
scope.$apply();
var contents = elm.find('div.item');
expect(contents.length).toBe(4);
expect(contents.eq(0).text()).toBe('two');
expect(contents.eq(1).text()).toBe('four');
expect(contents.eq(2).text()).toBe('one');
expect(contents.eq(3).text()).toBe('three');
});
it('should remove slide after order change', function() {
testSlideActive(0);
scope.slides.splice(1, 1);
scope.$apply();
var contents = elm.find('div.item');
expect(contents.length).toBe(2);
expect(contents.eq(0).text()).toBe('three');
expect(contents.eq(1).text()).toBe('one');
});
});
});
describe('controller', function() {
var scope, ctrl;
//create an array of slides and add to the scope
var slides = [{'content':1},{'content':2},{'content':3},{'content':4}];
beforeEach(function() {
scope = $rootScope.$new();
ctrl = $controller('UibCarouselController', {$scope: scope, $element: angular.element('<div></div>')});
for(var i = 0;i < slides.length;i++){
ctrl.addSlide(slides[i]);
}
});
describe('addSlide', function() {
it('should set first slide to active = true and the rest to false', function() {
angular.forEach(ctrl.slides, function(slide, i) {
if (i !== 0) {
expect(slide.active).not.toBe(true);
} else {
expect(slide.active).toBe(true);
}
});
});
it('should add new slide and change active to true if active is true on the added slide', function() {
var newSlide = {active: true};
expect(ctrl.slides.length).toBe(4);
ctrl.addSlide(newSlide);
expect(ctrl.slides.length).toBe(5);
expect(ctrl.slides[4].active).toBe(true);
expect(ctrl.slides[0].active).toBe(false);
});
it('should add a new slide and not change the active slide', function() {
var newSlide = {active: false};
expect(ctrl.slides.length).toBe(4);
ctrl.addSlide(newSlide);
expect(ctrl.slides.length).toBe(5);
expect(ctrl.slides[4].active).toBe(false);
expect(ctrl.slides[0].active).toBe(true);
});
it('should remove slide and change active slide if needed', function() {
expect(ctrl.slides.length).toBe(4);
ctrl.removeSlide(ctrl.slides[0]);
expect(ctrl.slides.length).toBe(3);
expect(ctrl.currentSlide).toBe(ctrl.slides[0]);
ctrl.select(ctrl.slides[2]);
ctrl.removeSlide(ctrl.slides[2]);
expect(ctrl.slides.length).toBe(2);
expect(ctrl.currentSlide).toBe(ctrl.slides[1]);
ctrl.removeSlide(ctrl.slides[0]);
expect(ctrl.slides.length).toBe(1);
expect(ctrl.currentSlide).toBe(ctrl.slides[0]);
});
it('issue 1414 - should not continue running timers after scope is destroyed', function() {
spyOn(scope, 'next').and.callThrough();
scope.interval = 2000;
scope.$digest();
$interval.flush(scope.interval);
expect(scope.next.calls.count()).toBe(1);
scope.$destroy();
$interval.flush(scope.interval);
expect(scope.next.calls.count()).toBe(1);
});
});
it('should be exposed in the template', inject(function($templateCache) {
$templateCache.put('template/carousel/carousel.html', '<div>{{carousel.text}}</div>');
var scope = $rootScope.$new();
var elm = $compile('<uib-carousel interval="bar" no-transition="false" no-pause="true"></uib-carousel>')(scope);
$rootScope.$digest();
var ctrl = elm.controller('uibCarousel');
expect(ctrl).toBeDefined();
ctrl.text = 'foo';
$rootScope.$digest();
expect(elm.html()).toBe('foo');
}));
});
it('should expose a custom model in the carousel slide', function() {
var scope = $rootScope.$new();
scope.slides = [
{active:false,content:'one'},
{active:false,content:'two'},
{active:false,content:'three'}
];
var elm = $compile(
'<uib-carousel interval="interval" no-transition="true" no-pause="nopause">' +
'<uib-slide ng-repeat="slide in slides" active="slide.active" actual="slide">' +
'{{slide.content}}' +
'</uib-slide>' +
'</uib-carousel>'
)(scope);
$rootScope.$digest();
var ctrl = elm.controller('uibCarousel');
expect(angular.equals(ctrl.slides.map(function(slide) {
return slide.actual;
}), scope.slides)).toBe(true);
});
});
describe('carousel deprecation', function() {
beforeEach(module('ui.bootstrap.carousel'));
beforeEach(module('template/carousel/carousel.html', 'template/carousel/slide.html'));
it('should suppress warning', function() {
module(function($provide) {
$provide.value('$carouselSuppressWarning', true);
});
inject(function($compile, $log, $rootScope) {
spyOn($log, 'warn');
var element = '<carousel interval="interval" no-transition="true" no-pause="nopause">' +
'<slide ng-repeat="slide in slides" active="slide.active">' +
'{{slide.content}}' +
'</slide>' +
'</carousel>';
element = $compile(element)($rootScope);
$rootScope.$digest();
expect($log.warn.calls.count()).toBe(0);
});
});
it('should give warning by default', inject(function($compile, $log, $rootScope) {
spyOn($log, 'warn');
var element = '<carousel interval="interval" no-transition="true" no-pause="nopause">' +
'<slide ng-repeat="slide in slides" active="slide.active">' +
'{{slide.content}}' +
'</slide>' +
'</carousel>';
element = $compile(element)($rootScope);
$rootScope.$digest();
expect($log.warn.calls.count()).toBe(2);
expect($log.warn.calls.argsFor(0)).toEqual(['CarouselController is now deprecated. Use UibCarouselController instead.']);
expect($log.warn.calls.argsFor(1)).toEqual(['carousel is now deprecated. Use uib-carousel instead.']);
}));
it('should give warning by default for slider', inject(function($compile, $log, $rootScope) {
spyOn($log, 'warn');
var element = '<carousel interval="interval" no-transition="true" no-pause="nopause">' +
'<slide></slide>' +
'</carousel>';
element = $compile(element)($rootScope);
$rootScope.$digest();
expect($log.warn.calls.count()).toBe(3);
expect($log.warn.calls.argsFor(0)).toEqual(['CarouselController is now deprecated. Use UibCarouselController instead.']);
expect($log.warn.calls.argsFor(1)).toEqual(['slide is now deprecated. Use uib-slide instead.']);
expect($log.warn.calls.argsFor(2)).toEqual(['carousel is now deprecated. Use uib-carousel instead.']);
}));
});