carousel.js 48.8 KB
Newer Older
1
$(function () {
2
  'use strict'
3

XhmikosR's avatar
XhmikosR committed
4
  window.Carousel = typeof bootstrap === 'undefined' ? Carousel : bootstrap.Carousel
5

Johann-S's avatar
Johann-S committed
6
  var originWinPointerEvent = window.PointerEvent
Johann-S's avatar
Johann-S committed
7
  window.MSPointerEvent = null
Johann-S's avatar
Johann-S committed
8
9
10
11
12
13
14
15
16
17
18
19
  var supportPointerEvent = Boolean(window.PointerEvent || window.MSPointerEvent)

  function clearPointerEvents() {
    window.PointerEvent = null
  }

  function restorePointerEvents() {
    window.PointerEvent = originWinPointerEvent
  }

  var stylesCarousel = [
    '<style>',
patrickhlauke's avatar
patrickhlauke committed
20
    '  .carousel.pointer-event { -ms-touch-action: none; touch-action: none; }',
Johann-S's avatar
Johann-S committed
21
22
23
    '</style>'
  ].join('')

fat's avatar
fat committed
24
  QUnit.module('carousel plugin')
25

fat's avatar
fat committed
26
27
28
  QUnit.test('should be defined on jQuery object', function (assert) {
    assert.expect(1)
    assert.ok($(document.body).carousel, 'carousel method is defined')
XhmikosR's avatar
XhmikosR committed
29
  })
30

fat's avatar
fat committed
31
32
  QUnit.module('carousel', {
    beforeEach: function () {
33
34
35
      // Run all tests in noConflict mode -- it's the only way to ensure that the plugin works in noConflict mode
      $.fn.bootstrapCarousel = $.fn.carousel.noConflict()
    },
fat's avatar
fat committed
36
    afterEach: function () {
Johann-S's avatar
Johann-S committed
37
      $('.carousel').bootstrapCarousel('dispose')
38
39
      $.fn.carousel = $.fn.bootstrapCarousel
      delete $.fn.bootstrapCarousel
40
      $('#qunit-fixture').html('')
41
42
43
    }
  })

fat's avatar
fat committed
44
45
  QUnit.test('should provide no conflict', function (assert) {
    assert.expect(1)
XhmikosR's avatar
XhmikosR committed
46
    assert.strictEqual(typeof $.fn.carousel, 'undefined', 'carousel was set back to undefined (orig value)')
47
48
  })

49
  QUnit.test('should return the version', function (assert) {
50
51
52
53
54
55
56
57
58
59
60
61
    assert.expect(1)
    assert.strictEqual(typeof Carousel.VERSION, 'string')
  })

  QUnit.test('should return default parameters', function (assert) {
    assert.expect(1)

    var defaultConfig = Carousel.Default

    assert.strictEqual(defaultConfig.touch, true)
  })

62
63
64
65
66
67
  QUnit.test('should throw explicit error on undefined method', function (assert) {
    assert.expect(1)
    var $el = $('<div/>')
    $el.bootstrapCarousel()
    try {
      $el.bootstrapCarousel('noMethod')
XhmikosR's avatar
XhmikosR committed
68
69
    } catch (error) {
      assert.strictEqual(error.message, 'No method named "noMethod"')
70
71
72
    }
  })

fat's avatar
fat committed
73
74
  QUnit.test('should return jquery collection containing the element', function (assert) {
    assert.expect(2)
75
76
    var $el = $('<div/>')
    var $carousel = $el.bootstrapCarousel()
fat's avatar
fat committed
77
78
    assert.ok($carousel instanceof $, 'returns jquery collection')
    assert.strictEqual($carousel[0], $el[0], 'collection contains element')
XhmikosR's avatar
XhmikosR committed
79
  })
80

fat's avatar
fat committed
81
  QUnit.test('should type check config options', function (assert) {
82
83
    assert.expect(2)

fat's avatar
fat committed
84
85
86
87
88
89
90
91
    var message
    var expectedMessage = 'CAROUSEL: Option "interval" provided type "string" but expected type "(number|boolean)".'
    var config = {
      interval: 'fat sux'
    }

    try {
      $('<div/>').bootstrapCarousel(config)
XhmikosR's avatar
XhmikosR committed
92
93
    } catch (error) {
      message = error.message
fat's avatar
fat committed
94
95
96
97
98
    }

    assert.ok(message === expectedMessage, 'correct error message')

    config = {
99
      keyboard: document.createElement('div')
fat's avatar
fat committed
100
101
102
103
104
    }
    expectedMessage = 'CAROUSEL: Option "keyboard" provided type "element" but expected type "boolean".'

    try {
      $('<div/>').bootstrapCarousel(config)
XhmikosR's avatar
XhmikosR committed
105
106
    } catch (error) {
      message = error.message
fat's avatar
fat committed
107
108
109
110
111
    }

    assert.ok(message === expectedMessage, 'correct error message')
  })

fat's avatar
fat committed
112
113
  QUnit.test('should not fire slid when slide is prevented', function (assert) {
    assert.expect(1)
114
    var done = assert.async()
Johann-S's avatar
Johann-S committed
115
116
117
    var $carousel = $('<div class="carousel"/>')
    $carousel.appendTo('#qunit-fixture')

118
    $carousel[0].addEventListener('slide.bs.carousel', function (e) {
Johann-S's avatar
Johann-S committed
119
120
121
122
      e.preventDefault()
      assert.ok(true, 'slide event fired')
      done()
    })
123
    $carousel[0].addEventListener('slid.bs.carousel', function () {
Johann-S's avatar
Johann-S committed
124
125
126
      assert.ok(false, 'slid event fired')
    })
    $carousel.bootstrapCarousel('next')
XhmikosR's avatar
XhmikosR committed
127
  })
128

fat's avatar
fat committed
129
130
  QUnit.test('should reset when slide is prevented', function (assert) {
    assert.expect(6)
XhmikosR's avatar
XhmikosR committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
    var carouselHTML = '<div id="carousel-example-generic" class="carousel slide">' +
        '<ol class="carousel-indicators">' +
        '<li data-target="#carousel-example-generic" data-slide-to="0" class="active"/>' +
        '<li data-target="#carousel-example-generic" data-slide-to="1"/>' +
        '<li data-target="#carousel-example-generic" data-slide-to="2"/>' +
        '</ol>' +
        '<div class="carousel-inner">' +
        '<div class="carousel-item active">' +
        '<div class="carousel-caption"/>' +
        '</div>' +
        '<div class="carousel-item">' +
        '<div class="carousel-caption"/>' +
        '</div>' +
        '<div class="carousel-item">' +
        '<div class="carousel-caption"/>' +
        '</div>' +
        '</div>' +
        '<a class="left carousel-control" href="#carousel-example-generic" data-slide="prev"/>' +
        '<a class="right carousel-control" href="#carousel-example-generic" data-slide="next"/>' +
        '</div>'
151
    var $carousel = $(carouselHTML)
Johann-S's avatar
Johann-S committed
152
    $carousel.appendTo('#qunit-fixture')
153

154
    var done = assert.async()
155
156
157
158
159
160
161
162
163
    function onSlide(e) {
      e.preventDefault()
      setTimeout(function () {
        assert.ok($carousel.find('.carousel-item:nth-child(1)').is('.active'), 'first item still active')
        assert.ok($carousel.find('.carousel-indicators li:nth-child(1)').is('.active'), 'first indicator still active')
        $carousel.bootstrapCarousel('next')
      }, 0)
      $carousel[0].removeEventListener('slide.bs.carousel', onSlide)
    }
XhmikosR's avatar
XhmikosR committed
164

165
166
167
168
169
170
171
172
173
174
175
176
    $carousel[0].addEventListener('slide.bs.carousel', onSlide)

    function onSlid() {
      setTimeout(function () {
        assert.ok(!$carousel.find('.carousel-item:nth-child(1)').is('.active'), 'first item still active')
        assert.ok(!$carousel.find('.carousel-indicators li:nth-child(1)').is('.active'), 'first indicator still active')
        assert.ok($carousel.find('.carousel-item:nth-child(2)').is('.active'), 'second item active')
        assert.ok($carousel.find('.carousel-indicators li:nth-child(2)').is('.active'), 'second indicator active')
        done()
      }, 0)
      $carousel[0].removeEventListener('slid.bs.carousel', onSlid)
    }
XhmikosR's avatar
XhmikosR committed
177

178
    $carousel[0].addEventListener('slid.bs.carousel', onSlid)
Johann-S's avatar
Johann-S committed
179
180

    $carousel.bootstrapCarousel('next')
XhmikosR's avatar
XhmikosR committed
181
  })
182

fat's avatar
fat committed
183
184
  QUnit.test('should fire slide event with direction', function (assert) {
    assert.expect(4)
XhmikosR's avatar
XhmikosR committed
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
    var carouselHTML = '<div id="myCarousel" class="carousel slide">' +
        '<div class="carousel-inner">' +
        '<div class="carousel-item active">' +
        '<img alt="">' +
        '<div class="carousel-caption">' +
        '<h4>First Thumbnail label</h4>' +
        '<p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec ' +
        'id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ' +
        'ultricies vehicula ut id elit.</p>' +
        '</div>' +
        '</div>' +
        '<div class="carousel-item">' +
        '<img alt="">' +
        '<div class="carousel-caption">' +
        '<h4>Second Thumbnail label</h4>' +
        '<p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec ' +
        'id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ' +
        'ultricies vehicula ut id elit.</p>' +
        '</div>' +
        '</div>' +
        '<div class="carousel-item">' +
        '<img alt="">' +
        '<div class="carousel-caption">' +
        '<h4>Third Thumbnail label</h4>' +
        '<p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec ' +
        'id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ' +
        'ultricies vehicula ut id elit.</p>' +
        '</div>' +
        '</div>' +
        '</div>' +
        '<a class="left carousel-control" href="#myCarousel" data-slide="prev">&lsaquo;</a>' +
        '<a class="right carousel-control" href="#myCarousel" data-slide="next">&rsaquo;</a>' +
        '</div>'
218
    var $carousel = $(carouselHTML)
Johann-S's avatar
Johann-S committed
219
    $carousel.appendTo('#qunit-fixture')
220

221
    var done = assert.async()
222

223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
    function onSlide(e) {
      assert.ok(e.direction, 'direction present on next')
      assert.strictEqual(e.direction, 'left', 'direction is left on next')

      $carousel[0].addEventListener('slide.bs.carousel', onSlide2)
      $carousel[0].removeEventListener('slide.bs.carousel', onSlide)
      $carousel.bootstrapCarousel('prev')
    }

    function onSlide2(e) {
      assert.ok(e.direction, 'direction present on prev')
      assert.strictEqual(e.direction, 'right', 'direction is right on prev')
      done()
    }

    $carousel[0].addEventListener('slide.bs.carousel', onSlide)
Johann-S's avatar
Johann-S committed
239
    $carousel.bootstrapCarousel('next')
XhmikosR's avatar
XhmikosR committed
240
  })
241

fat's avatar
fat committed
242
243
  QUnit.test('should fire slid event with direction', function (assert) {
    assert.expect(4)
XhmikosR's avatar
XhmikosR committed
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
    var carouselHTML = '<div id="myCarousel" class="carousel slide">' +
        '<div class="carousel-inner">' +
        '<div class="carousel-item active">' +
        '<img alt="">' +
        '<div class="carousel-caption">' +
        '<h4>First Thumbnail label</h4>' +
        '<p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec ' +
        'id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ' +
        'ultricies vehicula ut id elit.</p>' +
        '</div>' +
        '</div>' +
        '<div class="carousel-item">' +
        '<img alt="">' +
        '<div class="carousel-caption">' +
        '<h4>Second Thumbnail label</h4>' +
        '<p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec ' +
        'id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ' +
        'ultricies vehicula ut id elit.</p>' +
        '</div>' +
        '</div>' +
        '<div class="carousel-item">' +
        '<img alt="">' +
        '<div class="carousel-caption">' +
        '<h4>Third Thumbnail label</h4>' +
        '<p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec ' +
        'id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ' +
        'ultricies vehicula ut id elit.</p>' +
        '</div>' +
        '</div>' +
        '</div>' +
        '<a class="left carousel-control" href="#myCarousel" data-slide="prev">&lsaquo;</a>' +
        '<a class="right carousel-control" href="#myCarousel" data-slide="next">&rsaquo;</a>' +
        '</div>'
277
    var $carousel = $(carouselHTML)
Johann-S's avatar
Johann-S committed
278
    $carousel.appendTo('#qunit-fixture')
279

280
    var done = assert.async()
281

282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
    function onSlid(e) {
      assert.ok(e.direction, 'direction present on next')
      assert.strictEqual(e.direction, 'left', 'direction is left on next')

      $carousel[0].addEventListener('slid.bs.carousel', onSlid2)
      $carousel[0].removeEventListener('slid.bs.carousel', onSlid)
      $carousel.bootstrapCarousel('prev')
    }

    function onSlid2(e) {
      assert.ok(e.direction, 'direction present on prev')
      assert.strictEqual(e.direction, 'right', 'direction is right on prev')
      $carousel[0].removeEventListener('slid.bs.carousel', onSlid2)
      done()
    }

    $carousel[0].addEventListener('slid.bs.carousel', onSlid)
Johann-S's avatar
Johann-S committed
299
    $carousel.bootstrapCarousel('next')
300
301
  })

fat's avatar
fat committed
302
303
  QUnit.test('should fire slide event with relatedTarget', function (assert) {
    assert.expect(2)
XhmikosR's avatar
XhmikosR committed
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
    var template = '<div id="myCarousel" class="carousel slide">' +
        '<div class="carousel-inner">' +
        '<div class="carousel-item active">' +
        '<img alt="">' +
        '<div class="carousel-caption">' +
        '<h4>First Thumbnail label</h4>' +
        '<p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec ' +
        'id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ' +
        'ultricies vehicula ut id elit.</p>' +
        '</div>' +
        '</div>' +
        '<div class="carousel-item">' +
        '<img alt="">' +
        '<div class="carousel-caption">' +
        '<h4>Second Thumbnail label</h4>' +
        '<p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec ' +
        'id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ' +
        'ultricies vehicula ut id elit.</p>' +
        '</div>' +
        '</div>' +
        '<div class="carousel-item">' +
        '<img alt="">' +
        '<div class="carousel-caption">' +
        '<h4>Third Thumbnail label</h4>' +
        '<p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec ' +
        'id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ' +
        'ultricies vehicula ut id elit.</p>' +
        '</div>' +
        '</div>' +
        '</div>' +
        '<a class="left carousel-control" href="#myCarousel" data-slide="prev">&lsaquo;</a>' +
        '<a class="right carousel-control" href="#myCarousel" data-slide="next">&rsaquo;</a>' +
        '</div>'
337

338
    var done = assert.async()
Johann-S's avatar
Johann-S committed
339
340
    var $carousel = $(template)
    $carousel.appendTo('#qunit-fixture')
341

342
343
344
345
346
347
    function onSlide(e) {
      assert.ok(e.relatedTarget, 'relatedTarget present')
      assert.ok($(e.relatedTarget).hasClass('carousel-item'), 'relatedTarget has class "item"')
      $carousel[0].removeEventListener('slide.bs.carousel', onSlide)
      done()
    }
Johann-S's avatar
Johann-S committed
348

349
    $carousel[0].addEventListener('slide.bs.carousel', onSlide)
Johann-S's avatar
Johann-S committed
350
    $carousel.bootstrapCarousel('next')
XhmikosR's avatar
XhmikosR committed
351
  })
352

fat's avatar
fat committed
353
354
  QUnit.test('should fire slid event with relatedTarget', function (assert) {
    assert.expect(2)
XhmikosR's avatar
XhmikosR committed
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
    var template = '<div id="myCarousel" class="carousel slide">' +
        '<div class="carousel-inner">' +
        '<div class="carousel-item active">' +
        '<img alt="">' +
        '<div class="carousel-caption">' +
        '<h4>First Thumbnail label</h4>' +
        '<p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec ' +
        'id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ' +
        'ultricies vehicula ut id elit.</p>' +
        '</div>' +
        '</div>' +
        '<div class="carousel-item">' +
        '<img alt="">' +
        '<div class="carousel-caption">' +
        '<h4>Second Thumbnail label</h4>' +
        '<p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec ' +
        'id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ' +
        'ultricies vehicula ut id elit.</p>' +
        '</div>' +
        '</div>' +
        '<div class="carousel-item">' +
        '<img alt="">' +
        '<div class="carousel-caption">' +
        '<h4>Third Thumbnail label</h4>' +
        '<p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec ' +
        'id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ' +
        'ultricies vehicula ut id elit.</p>' +
        '</div>' +
        '</div>' +
        '</div>' +
        '<a class="left carousel-control" href="#myCarousel" data-slide="prev">&lsaquo;</a>' +
        '<a class="right carousel-control" href="#myCarousel" data-slide="next">&rsaquo;</a>' +
        '</div>'
388

389
    var done = assert.async()
Johann-S's avatar
Johann-S committed
390
391
    var $carousel = $(template)
    $carousel.appendTo('#qunit-fixture')
392

393
394
395
396
397
    $carousel[0].addEventListener('slid.bs.carousel', function (e) {
      assert.ok(e.relatedTarget, 'relatedTarget present')
      assert.ok($(e.relatedTarget).hasClass('carousel-item'), 'relatedTarget has class "item"')
      done()
    })
Johann-S's avatar
Johann-S committed
398
399

    $carousel.bootstrapCarousel('next')
400
  })
401

402
403
  QUnit.test('should fire slid and slide events with from and to', function (assert) {
    assert.expect(4)
XhmikosR's avatar
XhmikosR committed
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
    var template = '<div id="myCarousel" class="carousel slide">' +
        '<div class="carousel-inner">' +
        '<div class="carousel-item active">' +
        '<img alt="">' +
        '<div class="carousel-caption">' +
        '<h4>First Thumbnail label</h4>' +
        '</div>' +
        '</div>' +
        '<div class="carousel-item">' +
        '<img alt="">' +
        '<div class="carousel-caption">' +
        '<h4>Second Thumbnail label</h4>' +
        '</div>' +
        '</div>' +
        '<div class="carousel-item">' +
        '<img alt="">' +
        '<div class="carousel-caption">' +
        '<h4>Third Thumbnail label</h4>' +
        '</div>' +
        '</div>' +
        '</div>' +
        '<a class="left carousel-control" href="#myCarousel" data-slide="prev">&lsaquo;</a>' +
        '<a class="right carousel-control" href="#myCarousel" data-slide="next">&rsaquo;</a>' +
        '</div>'
428
429

    var done = assert.async()
Johann-S's avatar
Johann-S committed
430
431
    var $carousel = $(template)

432
433
434
435
436
    $carousel[0].addEventListener('slid.bs.carousel', function (e) {
      assert.ok(typeof e.from !== 'undefined', 'from present')
      assert.ok(typeof e.to !== 'undefined', 'to present')
      done()
    })
Johann-S's avatar
Johann-S committed
437

438
439
440
441
    $carousel[0].addEventListener('slide.bs.carousel', function (e) {
      assert.ok(typeof e.from !== 'undefined', 'from present')
      assert.ok(typeof e.to !== 'undefined', 'to present')
    })
Johann-S's avatar
Johann-S committed
442
443

    $carousel.bootstrapCarousel('next')
444
445
  })

fat's avatar
fat committed
446
447
  QUnit.test('should set interval from data attribute', function (assert) {
    assert.expect(4)
XhmikosR's avatar
XhmikosR committed
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
    var templateHTML = '<div id="myCarousel" class="carousel slide">' +
        '<div class="carousel-inner">' +
        '<div class="carousel-item active">' +
        '<img alt="">' +
        '<div class="carousel-caption">' +
        '<h4>First Thumbnail label</h4>' +
        '<p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec ' +
        'id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ' +
        'ultricies vehicula ut id elit.</p>' +
        '</div>' +
        '</div>' +
        '<div class="carousel-item">' +
        '<img alt="">' +
        '<div class="carousel-caption">' +
        '<h4>Second Thumbnail label</h4>' +
        '<p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec ' +
        'id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ' +
        'ultricies vehicula ut id elit.</p>' +
        '</div>' +
        '</div>' +
        '<div class="carousel-item">' +
        '<img alt="">' +
        '<div class="carousel-caption">' +
        '<h4>Third Thumbnail label</h4>' +
        '<p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec ' +
        'id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ' +
        'ultricies vehicula ut id elit.</p>' +
        '</div>' +
        '</div>' +
        '</div>' +
        '<a class="left carousel-control" href="#myCarousel" data-slide="prev">&lsaquo;</a>' +
        '<a class="right carousel-control" href="#myCarousel" data-slide="next">&rsaquo;</a>' +
        '</div>'
481
482
    var $carousel = $(templateHTML)
    $carousel.attr('data-interval', 1814)
Dmitriy Budnik's avatar
Dmitriy Budnik committed
483

484
    $carousel.appendTo('body')
485
    $('[data-slide]').first()[0].dispatchEvent(new Event('click'))
486
    assert.strictEqual(Carousel._getInstance($carousel[0])._config.interval, 1814)
487
    $carousel.remove()
dmitriybudnik's avatar
dmitriybudnik committed
488

489
    $carousel.appendTo('body').attr('data-modal', 'foobar')
490
    $('[data-slide]').first()[0].dispatchEvent(new Event('click'))
491
    assert.strictEqual(Carousel._getInstance($carousel[0])._config.interval, 1814, 'even if there is an data-modal attribute set')
492
    $carousel.remove()
Dmitriy Budnik's avatar
Dmitriy Budnik committed
493

494
    $carousel.appendTo('body')
495
    $('[data-slide]').first()[0].dispatchEvent(new Event('click'))
496
    $carousel.attr('data-interval', 1860)
497
498

    $('[data-slide]').first()[0].dispatchEvent(new Event('click'))
499
    assert.strictEqual(Carousel._getInstance($carousel[0])._config.interval, 1814, 'attributes should be read only on initialization')
Johann-S's avatar
Johann-S committed
500
    $carousel.bootstrapCarousel('dispose')
501
502
503
504
505
    $carousel.remove()

    $carousel.attr('data-interval', false)
    $carousel.appendTo('body')
    $carousel.bootstrapCarousel(1)
506
    assert.strictEqual(Carousel._getInstance($carousel[0])._config.interval, false, 'data attribute has higher priority than default options')
507
    $carousel.remove()
XhmikosR's avatar
XhmikosR committed
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
  QUnit.test('should set interval from data attribute on individual carousel-item', function (assert) {
    assert.expect(2)
    var templateHTML = '<div id="myCarousel" class="carousel slide" data-interval="1814">' +
        '<div class="carousel-inner">' +
        '<div class="carousel-item active" data-interval="2814">' +
        '<img alt="">' +
        '<div class="carousel-caption">' +
        '<h4>First Thumbnail label</h4>' +
        '<p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec ' +
        'id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ' +
        'ultricies vehicula ut id elit.</p>' +
        '</div>' +
        '</div>' +
        '<div class="carousel-item" data-interval="3814">' +
        '<img alt="">' +
        '<div class="carousel-caption">' +
        '<h4>Second Thumbnail label</h4>' +
        '<p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec ' +
        'id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ' +
        'ultricies vehicula ut id elit.</p>' +
        '</div>' +
        '</div>' +
        '<div class="carousel-item">' +
        '<img alt="">' +
        '<div class="carousel-caption">' +
        '<h4>Third Thumbnail label</h4>' +
        '<p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec ' +
        'id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ' +
        'ultricies vehicula ut id elit.</p>' +
        '</div>' +
        '</div>' +
        '</div>' +
        '<a class="left carousel-control" href="#myCarousel" data-slide="prev">&lsaquo;</a>' +
        '<a class="right carousel-control" href="#myCarousel" data-slide="next">&rsaquo;</a>' +
        '</div>'

Johann-S's avatar
Johann-S committed
546
    var $carousel = $(templateHTML).appendTo('#qunit-fixture')
547
    $carousel.bootstrapCarousel(1)
Johann-S's avatar
Johann-S committed
548
549
550
    var carousel = Carousel._getInstance($carousel[0])
    assert.strictEqual(carousel._config.interval, 3814)
    carousel.dispose()
551
552
    $carousel.remove()

Johann-S's avatar
Johann-S committed
553
    $carousel = $carousel.appendTo('#qunit-fixture')
554
    $carousel.bootstrapCarousel(2)
Johann-S's avatar
Johann-S committed
555
556
557
    carousel = Carousel._getInstance($carousel[0])

    assert.strictEqual(carousel._config.interval, 1814, 'reverts to default interval if no data-interval is set')
558
559
560
    $carousel.remove()
  })

fat's avatar
fat committed
561
562
  QUnit.test('should skip over non-items when using item indices', function (assert) {
    assert.expect(2)
XhmikosR's avatar
XhmikosR committed
563
564
565
566
567
568
569
570
571
572
573
574
575
    var templateHTML = '<div id="myCarousel" class="carousel" data-interval="1814">' +
        '<div class="carousel-inner">' +
        '<div class="carousel-item active">' +
        '<img alt="">' +
        '</div>' +
        '<script type="text/x-metamorph" id="thingy"/>' +
        '<div class="carousel-item">' +
        '<img alt="">' +
        '</div>' +
        '<div class="carousel-item">' +
        '</div>' +
        '</div>' +
        '</div>'
576
    var $template = $(templateHTML)
577

578
    $template.bootstrapCarousel()
579

fat's avatar
fat committed
580
    assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item active')
581

582
    $template.bootstrapCarousel(1)
583

fat's avatar
fat committed
584
    assert.strictEqual($template.find('.carousel-item')[1], $template.find('.active')[0], 'second item active')
585
  })
586

fat's avatar
fat committed
587
588
  QUnit.test('should skip over non-items when using next/prev methods', function (assert) {
    assert.expect(2)
XhmikosR's avatar
XhmikosR committed
589
590
591
592
593
594
595
596
597
598
599
600
601
    var templateHTML = '<div id="myCarousel" class="carousel" data-interval="1814">' +
        '<div class="carousel-inner">' +
        '<div class="carousel-item active">' +
        '<img alt="">' +
        '</div>' +
        '<script type="text/x-metamorph" id="thingy"/>' +
        '<div class="carousel-item">' +
        '<img alt="">' +
        '</div>' +
        '<div class="carousel-item">' +
        '</div>' +
        '</div>' +
        '</div>'
602
603
604
605
    var $template = $(templateHTML)

    $template.bootstrapCarousel()

fat's avatar
fat committed
606
    assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item active')
607
608
609

    $template.bootstrapCarousel('next')

fat's avatar
fat committed
610
    assert.strictEqual($template.find('.carousel-item')[1], $template.find('.active')[0], 'second item active')
611
  })
612

fat's avatar
fat committed
613
614
  QUnit.test('should go to previous item if left arrow key is pressed', function (assert) {
    assert.expect(2)
XhmikosR's avatar
XhmikosR committed
615
616
617
618
619
620
621
622
623
624
625
626
627
    var templateHTML = '<div id="myCarousel" class="carousel" data-interval="false">' +
        '<div class="carousel-inner">' +
        '<div id="first" class="carousel-item">' +
        '<img alt="">' +
        '</div>' +
        '<div id="second" class="carousel-item active">' +
        '<img alt="">' +
        '</div>' +
        '<div id="third" class="carousel-item">' +
        '<img alt="">' +
        '</div>' +
        '</div>' +
        '</div>'
628
629
630
631
    var $template = $(templateHTML)

    $template.bootstrapCarousel()

fat's avatar
fat committed
632
    assert.strictEqual($template.find('.carousel-item')[1], $template.find('.active')[0], 'second item active')
633

634
635
636
    var keyDown = new Event('keydown')
    keyDown.which = 37
    $template[0].dispatchEvent(keyDown)
637

fat's avatar
fat committed
638
    assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item active')
639
640
  })

fat's avatar
fat committed
641
642
  QUnit.test('should go to next item if right arrow key is pressed', function (assert) {
    assert.expect(2)
XhmikosR's avatar
XhmikosR committed
643
644
645
646
647
648
649
650
651
652
653
654
655
    var templateHTML = '<div id="myCarousel" class="carousel" data-interval="false">' +
        '<div class="carousel-inner">' +
        '<div id="first" class="carousel-item active">' +
        '<img alt="">' +
        '</div>' +
        '<div id="second" class="carousel-item">' +
        '<img alt="">' +
        '</div>' +
        '<div id="third" class="carousel-item">' +
        '<img alt="">' +
        '</div>' +
        '</div>' +
        '</div>'
656
657
658
659
    var $template = $(templateHTML)

    $template.bootstrapCarousel()

fat's avatar
fat committed
660
    assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item active')
661

662
663
664
    var keyDown = new Event('keydown')
    keyDown.which = 39
    $template[0].dispatchEvent(keyDown)
665

fat's avatar
fat committed
666
    assert.strictEqual($template.find('.carousel-item')[1], $template.find('.active')[0], 'second item active')
667
668
  })

669
670
  QUnit.test('should not prevent keydown if key is not ARROW_LEFT or ARROW_RIGHT', function (assert) {
    assert.expect(2)
XhmikosR's avatar
XhmikosR committed
671
672
673
674
675
676
677
    var templateHTML = '<div id="myCarousel" class="carousel" data-interval="false">' +
        '<div class="carousel-inner">' +
        '<div id="first" class="carousel-item active">' +
        '<img alt="">' +
        '</div>' +
        '</div>' +
        '</div>'
678
679
680
681
682
    var $template = $(templateHTML)

    $template.bootstrapCarousel()
    var done = assert.async()

683
    function handlerKeydown(event) {
Johann-S's avatar
Johann-S committed
684
      assert.strictEqual(event.defaultPrevented, false)
685
686
      $template[0].removeEventListener('keydown', handlerKeydown)
    }
XhmikosR's avatar
XhmikosR committed
687

688
    $template[0].addEventListener('keydown', handlerKeydown)
689

Johann-S's avatar
Johann-S committed
690
    // arrow down
691
692
693
    var keyDown = new Event('keydown')
    keyDown.which = 40
    $template[0].dispatchEvent(keyDown)
694

695
    function handlerKeydown2(event) {
Johann-S's avatar
Johann-S committed
696
      assert.strictEqual(event.defaultPrevented, false)
697
      $template[0].addEventListener('keydown', handlerKeydown2)
698
      done()
699
    }
XhmikosR's avatar
XhmikosR committed
700

701
    $template[0].addEventListener('keydown', handlerKeydown2)
702

Johann-S's avatar
Johann-S committed
703
    // arrow up
704
705
706
    var keyDown2 = new Event('keydown')
    keyDown2.which = 38
    $template[0].dispatchEvent(keyDown2)
707
708
  })

fat's avatar
fat committed
709
710
  QUnit.test('should support disabling the keyboard navigation', function (assert) {
    assert.expect(3)
XhmikosR's avatar
XhmikosR committed
711
712
713
714
715
716
717
718
719
720
721
722
723
    var templateHTML = '<div id="myCarousel" class="carousel" data-interval="false" data-keyboard="false">' +
        '<div class="carousel-inner">' +
        '<div id="first" class="carousel-item active">' +
        '<img alt="">' +
        '</div>' +
        '<div id="second" class="carousel-item">' +
        '<img alt="">' +
        '</div>' +
        '<div id="third" class="carousel-item">' +
        '<img alt="">' +
        '</div>' +
        '</div>' +
        '</div>'
724
725
726
727
    var $template = $(templateHTML)

    $template.bootstrapCarousel()

fat's avatar
fat committed
728
    assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item active')
729

XhmikosR's avatar
XhmikosR committed
730
731
732
    $template.trigger($.Event('keydown', {
      which: 39
    }))
733

fat's avatar
fat committed
734
    assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item still active after right arrow press')
735

XhmikosR's avatar
XhmikosR committed
736
737
738
    $template.trigger($.Event('keydown', {
      which: 37
    }))
739

fat's avatar
fat committed
740
    assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item still active after left arrow press')
741
742
  })

fat's avatar
fat committed
743
744
  QUnit.test('should ignore keyboard events within <input>s and <textarea>s', function (assert) {
    assert.expect(7)
XhmikosR's avatar
XhmikosR committed
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
    var templateHTML = '<div id="myCarousel" class="carousel" data-interval="false">' +
        '<div class="carousel-inner">' +
        '<div id="first" class="carousel-item active">' +
        '<img alt="">' +
        '<input type="text" id="in-put">' +
        '<textarea id="text-area"></textarea>' +
        '</div>' +
        '<div id="second" class="carousel-item">' +
        '<img alt="">' +
        '</div>' +
        '<div id="third" class="carousel-item">' +
        '<img alt="">' +
        '</div>' +
        '</div>' +
        '</div>'
760
761
762
763
    var $template = $(templateHTML)
    var $input = $template.find('#in-put')
    var $textarea = $template.find('#text-area')

fat's avatar
fat committed
764
765
    assert.strictEqual($input.length, 1, 'found <input>')
    assert.strictEqual($textarea.length, 1, 'found <textarea>')
766
767
768

    $template.bootstrapCarousel()

fat's avatar
fat committed
769
    assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item active')
770

XhmikosR's avatar
XhmikosR committed
771
772
773
    $input.trigger($.Event('keydown', {
      which: 39
    }))
fat's avatar
fat committed
774
    assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item still active after right arrow press in <input>')
775

XhmikosR's avatar
XhmikosR committed
776
777
778
    $input.trigger($.Event('keydown', {
      which: 37
    }))
fat's avatar
fat committed
779
    assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item still active after left arrow press in <input>')
780

XhmikosR's avatar
XhmikosR committed
781
782
783
    $textarea.trigger($.Event('keydown', {
      which: 39
    }))
fat's avatar
fat committed
784
    assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item still active after right arrow press in <textarea>')
785

XhmikosR's avatar
XhmikosR committed
786
787
788
    $textarea.trigger($.Event('keydown', {
      which: 37
    }))
fat's avatar
fat committed
789
    assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item still active after left arrow press in <textarea>')
790
791
  })

fat's avatar
fat committed
792
793
  QUnit.test('should wrap around from end to start when wrap option is true', function (assert) {
    assert.expect(3)
XhmikosR's avatar
XhmikosR committed
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
    var carouselHTML = '<div id="carousel-example-generic" class="carousel slide" data-wrap="true">' +
        '<ol class="carousel-indicators">' +
        '<li data-target="#carousel-example-generic" data-slide-to="0" class="active"/>' +
        '<li data-target="#carousel-example-generic" data-slide-to="1"/>' +
        '<li data-target="#carousel-example-generic" data-slide-to="2"/>' +
        '</ol>' +
        '<div class="carousel-inner">' +
        '<div class="carousel-item active" id="one">' +
        '<div class="carousel-caption"/>' +
        '</div>' +
        '<div class="carousel-item" id="two">' +
        '<div class="carousel-caption"/>' +
        '</div>' +
        '<div class="carousel-item" id="three">' +
        '<div class="carousel-caption"/>' +
        '</div>' +
        '</div>' +
        '<a class="left carousel-control" href="#carousel-example-generic" data-slide="prev"/>' +
        '<a class="right carousel-control" href="#carousel-example-generic" data-slide="next"/>' +
        '</div>'
814
    var $carousel = $(carouselHTML)
815
    var done = assert.async()
XhmikosR's avatar
XhmikosR committed
816
817
818
    var getActiveId = function () {
      return $carousel.find('.carousel-item.active').attr('id')
    }
819

820
821
822
823
824
825
826
    $carousel[0].addEventListener('slid.bs.carousel', function () {
      var activeId = getActiveId()
      if (activeId === 'two') {
        assert.strictEqual(activeId, 'two', 'carousel slid from 1st to 2nd slide')
        $carousel.bootstrapCarousel('next')
        return
      }
Johann-S's avatar
Johann-S committed
827

828
829
      if (activeId === 'three') {
        assert.strictEqual(activeId, 'three', 'carousel slid from 2nd to 3rd slide')
Johann-S's avatar
Johann-S committed
830
        $carousel.bootstrapCarousel('next')
831
832
833
834
835
836
837
        return
      }

      if (activeId === 'one') {
        assert.strictEqual(activeId, 'one', 'carousel wrapped around and slid from 3rd to 1st slide')
        done()
      }
Johann-S's avatar
Johann-S committed
838
839
    })
    $carousel.bootstrapCarousel('next')
840
841
  })

fat's avatar
fat committed
842
843
  QUnit.test('should wrap around from start to end when wrap option is true', function (assert) {
    assert.expect(1)
XhmikosR's avatar
XhmikosR committed
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
    var carouselHTML = '<div id="carousel-example-generic" class="carousel slide" data-wrap="true">' +
        '<ol class="carousel-indicators">' +
        '<li data-target="#carousel-example-generic" data-slide-to="0" class="active"/>' +
        '<li data-target="#carousel-example-generic" data-slide-to="1"/>' +
        '<li data-target="#carousel-example-generic" data-slide-to="2"/>' +
        '</ol>' +
        '<div class="carousel-inner">' +
        '<div class="carousel-item active" id="one">' +
        '<div class="carousel-caption"/>' +
        '</div>' +
        '<div class="carousel-item" id="two">' +
        '<div class="carousel-caption"/>' +
        '</div>' +
        '<div class="carousel-item" id="three">' +
        '<div class="carousel-caption"/>' +
        '</div>' +
        '</div>' +
        '<a class="left carousel-control" href="#carousel-example-generic" data-slide="prev"/>' +
        '<a class="right carousel-control" href="#carousel-example-generic" data-slide="next"/>' +
        '</div>'
864
865
    var $carousel = $(carouselHTML)

866
    var done = assert.async()
867

868
    $carousel[0].addEventListener('slid.bs.carousel', function () {
Johann-S's avatar
Johann-S committed
869
870
871
872
      assert.strictEqual($carousel.find('.carousel-item.active').attr('id'), 'three', 'carousel wrapped around and slid from 1st to 3rd slide')
      done()
    })
    $carousel.bootstrapCarousel('prev')
873
874
  })

fat's avatar
fat committed
875
876
  QUnit.test('should stay at the end when the next method is called and wrap is false', function (assert) {
    assert.expect(3)
XhmikosR's avatar
XhmikosR committed
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
    var carouselHTML = '<div id="carousel-example-generic" class="carousel slide" data-wrap="false">' +
        '<ol class="carousel-indicators">' +
        '<li data-target="#carousel-example-generic" data-slide-to="0" class="active"/>' +
        '<li data-target="#carousel-example-generic" data-slide-to="1"/>' +
        '<li data-target="#carousel-example-generic" data-slide-to="2"/>' +
        '</ol>' +
        '<div class="carousel-inner">' +
        '<div class="carousel-item active" id="one">' +
        '<div class="carousel-caption"/>' +
        '</div>' +
        '<div class="carousel-item" id="two">' +
        '<div class="carousel-caption"/>' +
        '</div>' +
        '<div class="carousel-item" id="three">' +
        '<div class="carousel-caption"/>' +
        '</div>' +
        '</div>' +
        '<a class="left carousel-control" href="#carousel-example-generic" data-slide="prev"/>' +
        '<a class="right carousel-control" href="#carousel-example-generic" data-slide="next"/>' +
        '</div>'
897
    var $carousel = $(carouselHTML).appendTo('#qunit-fixture')
XhmikosR's avatar
XhmikosR committed
898
899
900
    var getActiveId = function () {
      return $carousel.find('.carousel-item.active').attr('id')
    }
901

902
    var done = assert.async()
903
904
905
906
907
908
909
    $carousel[0].addEventListener('slid.bs.carousel', function () {
      var activeId = getActiveId()
      if (activeId === 'two') {
        assert.strictEqual(activeId, 'two', 'carousel slid from 1st to 2nd slide')
        $carousel.bootstrapCarousel('next')
        return
      }
910

911
912
      if (activeId === 'three') {
        assert.strictEqual(activeId, 'three', 'carousel slid from 2nd to 3rd slide')
Johann-S's avatar
Johann-S committed
913
914
915
        $carousel.bootstrapCarousel('next')
        assert.strictEqual(getActiveId(), 'three', 'carousel did not wrap around and stayed on 3rd slide')
        done()
916
      }
Johann-S's avatar
Johann-S committed
917
918
    })
    $carousel.bootstrapCarousel('next')
919
920
  })

fat's avatar
fat committed
921
922
  QUnit.test('should stay at the start when the prev method is called and wrap is false', function (assert) {
    assert.expect(1)
XhmikosR's avatar
XhmikosR committed
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
    var carouselHTML = '<div id="carousel-example-generic" class="carousel slide" data-wrap="false">' +
        '<ol class="carousel-indicators">' +
        '<li data-target="#carousel-example-generic" data-slide-to="0" class="active"/>' +
        '<li data-target="#carousel-example-generic" data-slide-to="1"/>' +
        '<li data-target="#carousel-example-generic" data-slide-to="2"/>' +
        '</ol>' +
        '<div class="carousel-inner">' +
        '<div class="carousel-item active" id="one">' +
        '<div class="carousel-caption"/>' +
        '</div>' +
        '<div class="carousel-item" id="two">' +
        '<div class="carousel-caption"/>' +
        '</div>' +
        '<div class="carousel-item" id="three">' +
        '<div class="carousel-caption"/>' +
        '</div>' +
        '</div>' +
        '<a class="left carousel-control" href="#carousel-example-generic" data-slide="prev"/>' +
        '<a class="right carousel-control" href="#carousel-example-generic" data-slide="next"/>' +
        '</div>'
943
944
    var $carousel = $(carouselHTML)

945
    $carousel[0].addEventListener('slid.bs.carousel', function () {
Johann-S's avatar
Johann-S committed
946
947
948
      assert.ok(false, 'carousel slid when it should not have slid')
    })
    $carousel.bootstrapCarousel('prev')
fat's avatar
fat committed
949
    assert.strictEqual($carousel.find('.carousel-item.active').attr('id'), 'one', 'carousel did not wrap around and stayed on 1st slide')
950
  })
951
952
953

  QUnit.test('should not prevent keydown for inputs and textareas', function (assert) {
    assert.expect(2)
XhmikosR's avatar
XhmikosR committed
954
955
956
957
958
959
960
961
962
963
    var templateHTML = '<div id="myCarousel" class="carousel" data-interval="false">' +
        '<div class="carousel-inner">' +
          '<div id="first" class="carousel-item">' +
            '<input type="text" id="inputText" />' +
          '</div>' +
          '<div id="second" class="carousel-item active">' +
            '<textarea id="txtArea"></textarea>' +
          '</div>' +
        '</div>' +
        '</div>'
964
965
966
967
968
969
970
    var $template = $(templateHTML)
    var done = assert.async()
    $template.appendTo('#qunit-fixture')
    var $inputText = $template.find('#inputText')
    var $textArea = $template.find('#txtArea')
    $template.bootstrapCarousel()

XhmikosR's avatar
XhmikosR committed
971
972
973
    var eventKeyDown = $.Event('keydown', {
      which: 65
    }) // 65 for "a"
974
975
976
977
978
979
980
981
982
983
984
    $inputText.on('keydown', function (event) {
      assert.strictEqual(event.isDefaultPrevented(), false)
    })
    $inputText.trigger(eventKeyDown)

    $textArea.on('keydown', function (event) {
      assert.strictEqual(event.isDefaultPrevented(), false)
      done()
    })
    $textArea.trigger(eventKeyDown)
  })
985

986
  QUnit.test('should not go to the next item when the carousel is not visible', function (assert) {
987
988
    assert.expect(2)
    var done = assert.async()
XhmikosR's avatar
XhmikosR committed
989
990
991
992
993
994
995
996
997
998
999
1000
    var html = '<div id="myCarousel" class="carousel slide" data-interval="50" style="display: none;">' +
             '  <div class="carousel-inner">' +
             '    <div id="firstItem" class="carousel-item active">' +
             '      <img alt="">' +
             '    </div>' +
             '    <div class="carousel-item">' +
             '      <img alt="">' +
             '    </div>' +
             '    <div class="carousel-item">' +
             '      <img alt="">' +
             '    </div>' +
             '  <a class="left carousel-control" href="#myCarousel" data-slide="prev">&lsaquo;</a>' +
For faster browsing, not all history is shown. View entire blame