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

fat's avatar
fat committed
4
  QUnit.module('carousel plugin')
5

fat's avatar
fat committed
6
7
8
  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
9
  })
10

fat's avatar
fat committed
11
12
  QUnit.module('carousel', {
    beforeEach: function () {
13
14
15
      // 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
16
    afterEach: function () {
17
18
19
20
21
      $.fn.carousel = $.fn.bootstrapCarousel
      delete $.fn.bootstrapCarousel
    }
  })

fat's avatar
fat committed
22
23
  QUnit.test('should provide no conflict', function (assert) {
    assert.expect(1)
XhmikosR's avatar
XhmikosR committed
24
    assert.strictEqual(typeof $.fn.carousel, 'undefined', 'carousel was set back to undefined (orig value)')
25
26
  })

27
28
29
30
31
32
33
34
35
36
37
38
  QUnit.test('should throw explicit error on undefined method', function (assert) {
    assert.expect(1)
    var $el = $('<div/>')
    $el.bootstrapCarousel()
    try {
      $el.bootstrapCarousel('noMethod')
    }
    catch (err) {
      assert.strictEqual(err.message, 'No method named "noMethod"')
    }
  })

fat's avatar
fat committed
39
40
  QUnit.test('should return jquery collection containing the element', function (assert) {
    assert.expect(2)
41
42
    var $el = $('<div/>')
    var $carousel = $el.bootstrapCarousel()
fat's avatar
fat committed
43
44
    assert.ok($carousel instanceof $, 'returns jquery collection')
    assert.strictEqual($carousel[0], $el[0], 'collection contains element')
XhmikosR's avatar
XhmikosR committed
45
  })
46

fat's avatar
fat committed
47
  QUnit.test('should type check config options', function (assert) {
48
49
    assert.expect(2)

fat's avatar
fat committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    var message
    var expectedMessage = 'CAROUSEL: Option "interval" provided type "string" but expected type "(number|boolean)".'
    var config = {
      interval: 'fat sux'
    }

    try {
      $('<div/>').bootstrapCarousel(config)
    } catch (e) {
      message = e.message
    }

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

    config = {
65
      keyboard: document.createElement('div')
fat's avatar
fat committed
66
67
68
69
70
71
72
73
74
75
76
77
78
    }
    expectedMessage = 'CAROUSEL: Option "keyboard" provided type "element" but expected type "boolean".'

    try {
      $('<div/>').bootstrapCarousel(config)
    } catch (e) {
      message = e.message
    }

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


fat's avatar
fat committed
79
80
  QUnit.test('should not fire slid when slide is prevented', function (assert) {
    assert.expect(1)
81
    var done = assert.async()
XhmikosR's avatar
XhmikosR committed
82
83
    $('<div class="carousel"/>')
      .on('slide.bs.carousel', function (e) {
84
        e.preventDefault()
fat's avatar
fat committed
85
        assert.ok(true, 'slide event fired')
86
        done()
XhmikosR's avatar
XhmikosR committed
87
88
      })
      .on('slid.bs.carousel', function () {
fat's avatar
fat committed
89
        assert.ok(false, 'slid event fired')
90
      })
91
      .bootstrapCarousel('next')
XhmikosR's avatar
XhmikosR committed
92
  })
93

fat's avatar
fat committed
94
95
  QUnit.test('should reset when slide is prevented', function (assert) {
    assert.expect(6)
Heinrich Fenkart's avatar
Heinrich Fenkart committed
96
97
98
99
100
101
102
    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">'
fat's avatar
fat committed
103
        + '<div class="carousel-item active">'
Heinrich Fenkart's avatar
Heinrich Fenkart committed
104
105
        + '<div class="carousel-caption"/>'
        + '</div>'
fat's avatar
fat committed
106
        + '<div class="carousel-item">'
Heinrich Fenkart's avatar
Heinrich Fenkart committed
107
108
        + '<div class="carousel-caption"/>'
        + '</div>'
fat's avatar
fat committed
109
        + '<div class="carousel-item">'
Heinrich Fenkart's avatar
Heinrich Fenkart committed
110
111
112
113
114
115
        + '<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>'
116
117
    var $carousel = $(carouselHTML)

118
    var done = assert.async()
119
120
121
122
    $carousel
      .one('slide.bs.carousel', function (e) {
        e.preventDefault()
        setTimeout(function () {
fat's avatar
fat committed
123
          assert.ok($carousel.find('.carousel-item:eq(0)').is('.active'), 'first item still active')
fat's avatar
fat committed
124
          assert.ok($carousel.find('.carousel-indicators li:eq(0)').is('.active'), 'first indicator still active')
125
126
127
128
129
          $carousel.bootstrapCarousel('next')
        }, 0)
      })
      .one('slid.bs.carousel', function () {
        setTimeout(function () {
fat's avatar
fat committed
130
          assert.ok(!$carousel.find('.carousel-item:eq(0)').is('.active'), 'first item still active')
fat's avatar
fat committed
131
          assert.ok(!$carousel.find('.carousel-indicators li:eq(0)').is('.active'), 'first indicator still active')
fat's avatar
fat committed
132
          assert.ok($carousel.find('.carousel-item:eq(1)').is('.active'), 'second item active')
fat's avatar
fat committed
133
          assert.ok($carousel.find('.carousel-indicators li:eq(1)').is('.active'), 'second indicator active')
134
          done()
135
136
137
        }, 0)
      })
      .bootstrapCarousel('next')
XhmikosR's avatar
XhmikosR committed
138
  })
139

fat's avatar
fat committed
140
141
  QUnit.test('should fire slide event with direction', function (assert) {
    assert.expect(4)
Heinrich Fenkart's avatar
Heinrich Fenkart committed
142
143
    var carouselHTML = '<div id="myCarousel" class="carousel slide">'
        + '<div class="carousel-inner">'
fat's avatar
fat committed
144
        + '<div class="carousel-item active">'
Heinrich Fenkart's avatar
Heinrich Fenkart committed
145
146
147
148
149
150
151
152
        + '<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>'
fat's avatar
fat committed
153
        + '<div class="carousel-item">'
Heinrich Fenkart's avatar
Heinrich Fenkart committed
154
155
156
157
158
159
160
161
        + '<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>'
fat's avatar
fat committed
162
        + '<div class="carousel-item">'
Heinrich Fenkart's avatar
Heinrich Fenkart committed
163
164
165
166
167
168
169
170
171
172
173
174
        + '<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>'
175
176
    var $carousel = $(carouselHTML)

177
    var done = assert.async()
178
179
180

    $carousel
      .one('slide.bs.carousel', function (e) {
fat's avatar
fat committed
181
182
        assert.ok(e.direction, 'direction present on next')
        assert.strictEqual(e.direction, 'left', 'direction is left on next')
183
184
185

        $carousel
          .one('slide.bs.carousel', function (e) {
fat's avatar
fat committed
186
187
            assert.ok(e.direction, 'direction present on prev')
            assert.strictEqual(e.direction, 'right', 'direction is right on prev')
188
            done()
189
190
191
192
          })
          .bootstrapCarousel('prev')
      })
      .bootstrapCarousel('next')
XhmikosR's avatar
XhmikosR committed
193
  })
194

fat's avatar
fat committed
195
196
  QUnit.test('should fire slid event with direction', function (assert) {
    assert.expect(4)
Heinrich Fenkart's avatar
Heinrich Fenkart committed
197
198
    var carouselHTML = '<div id="myCarousel" class="carousel slide">'
        + '<div class="carousel-inner">'
fat's avatar
fat committed
199
        + '<div class="carousel-item active">'
Heinrich Fenkart's avatar
Heinrich Fenkart committed
200
201
202
203
204
205
206
207
        + '<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>'
fat's avatar
fat committed
208
        + '<div class="carousel-item">'
Heinrich Fenkart's avatar
Heinrich Fenkart committed
209
210
211
212
213
214
215
216
        + '<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>'
fat's avatar
fat committed
217
        + '<div class="carousel-item">'
Heinrich Fenkart's avatar
Heinrich Fenkart committed
218
219
220
221
222
223
224
225
226
227
228
229
        + '<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>'
230
231
    var $carousel = $(carouselHTML)

232
    var done = assert.async()
233
234
235

    $carousel
      .one('slid.bs.carousel', function (e) {
fat's avatar
fat committed
236
237
        assert.ok(e.direction, 'direction present on next')
        assert.strictEqual(e.direction, 'left', 'direction is left on next')
238
239
240

        $carousel
          .one('slid.bs.carousel', function (e) {
fat's avatar
fat committed
241
242
            assert.ok(e.direction, 'direction present on prev')
            assert.strictEqual(e.direction, 'right', 'direction is right on prev')
243
            done()
244
245
246
247
          })
          .bootstrapCarousel('prev')
      })
      .bootstrapCarousel('next')
248
249
  })

fat's avatar
fat committed
250
251
  QUnit.test('should fire slide event with relatedTarget', function (assert) {
    assert.expect(2)
Heinrich Fenkart's avatar
Heinrich Fenkart committed
252
253
    var template = '<div id="myCarousel" class="carousel slide">'
        + '<div class="carousel-inner">'
fat's avatar
fat committed
254
        + '<div class="carousel-item active">'
Heinrich Fenkart's avatar
Heinrich Fenkart committed
255
256
257
258
259
260
261
262
        + '<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>'
fat's avatar
fat committed
263
        + '<div class="carousel-item">'
Heinrich Fenkart's avatar
Heinrich Fenkart committed
264
265
266
267
268
269
270
271
        + '<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>'
fat's avatar
fat committed
272
        + '<div class="carousel-item">'
Heinrich Fenkart's avatar
Heinrich Fenkart committed
273
274
275
276
277
278
279
280
281
282
283
284
        + '<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>'
285

286
    var done = assert.async()
287

XhmikosR's avatar
XhmikosR committed
288
289
    $(template)
      .on('slide.bs.carousel', function (e) {
fat's avatar
fat committed
290
        assert.ok(e.relatedTarget, 'relatedTarget present')
fat's avatar
fat committed
291
        assert.ok($(e.relatedTarget).hasClass('carousel-item'), 'relatedTarget has class "item"')
292
        done()
Dmitriy Budnik's avatar
Dmitriy Budnik committed
293
      })
294
      .bootstrapCarousel('next')
XhmikosR's avatar
XhmikosR committed
295
  })
296

fat's avatar
fat committed
297
298
  QUnit.test('should fire slid event with relatedTarget', function (assert) {
    assert.expect(2)
Heinrich Fenkart's avatar
Heinrich Fenkart committed
299
300
    var template = '<div id="myCarousel" class="carousel slide">'
        + '<div class="carousel-inner">'
fat's avatar
fat committed
301
        + '<div class="carousel-item active">'
Heinrich Fenkart's avatar
Heinrich Fenkart committed
302
303
304
305
306
307
308
309
        + '<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>'
fat's avatar
fat committed
310
        + '<div class="carousel-item">'
Heinrich Fenkart's avatar
Heinrich Fenkart committed
311
312
313
314
315
316
317
318
        + '<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>'
fat's avatar
fat committed
319
        + '<div class="carousel-item">'
Heinrich Fenkart's avatar
Heinrich Fenkart committed
320
321
322
323
324
325
326
327
328
329
330
331
        + '<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>'
332

333
    var done = assert.async()
334

335
336
    $(template)
      .on('slid.bs.carousel', function (e) {
fat's avatar
fat committed
337
        assert.ok(e.relatedTarget, 'relatedTarget present')
fat's avatar
fat committed
338
        assert.ok($(e.relatedTarget).hasClass('carousel-item'), 'relatedTarget has class "item"')
339
        done()
340
      })
341
      .bootstrapCarousel('next')
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
  QUnit.test('should fire slid and slide events with from and to', function (assert) {
    assert.expect(4)
    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>'

    var done = assert.async()
    $(template)
      .on('slid.bs.carousel', function (e) {
XhmikosR's avatar
XhmikosR committed
374
375
        assert.ok(typeof e.from !== 'undefined', 'from present')
        assert.ok(typeof e.to !== 'undefined', 'to present')
376
377
378
379
        $(this).off()
        done()
      })
      .on('slide.bs.carousel', function (e) {
XhmikosR's avatar
XhmikosR committed
380
381
        assert.ok(typeof e.from !== 'undefined', 'from present')
        assert.ok(typeof e.to !== 'undefined', 'to present')
382
383
384
385
386
        $(this).off('slide.bs.carousel')
      })
      .bootstrapCarousel('next')
  })

fat's avatar
fat committed
387
388
  QUnit.test('should set interval from data attribute', function (assert) {
    assert.expect(4)
Heinrich Fenkart's avatar
Heinrich Fenkart committed
389
390
    var templateHTML = '<div id="myCarousel" class="carousel slide">'
        + '<div class="carousel-inner">'
fat's avatar
fat committed
391
        + '<div class="carousel-item active">'
Heinrich Fenkart's avatar
Heinrich Fenkart committed
392
393
394
395
396
397
398
399
        + '<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>'
fat's avatar
fat committed
400
        + '<div class="carousel-item">'
Heinrich Fenkart's avatar
Heinrich Fenkart committed
401
402
403
404
405
406
407
408
        + '<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>'
fat's avatar
fat committed
409
        + '<div class="carousel-item">'
Heinrich Fenkart's avatar
Heinrich Fenkart committed
410
411
412
413
414
415
416
417
418
419
420
421
        + '<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>'
422
423
    var $carousel = $(templateHTML)
    $carousel.attr('data-interval', 1814)
Dmitriy Budnik's avatar
Dmitriy Budnik committed
424

425
    $carousel.appendTo('body')
fat's avatar
fat committed
426
    $('[data-slide]').first().trigger('click')
fat's avatar
fat committed
427
    assert.strictEqual($carousel.data('bs.carousel')._config.interval, 1814)
428
    $carousel.remove()
dmitriybudnik's avatar
dmitriybudnik committed
429

430
    $carousel.appendTo('body').attr('data-modal', 'foobar')
fat's avatar
fat committed
431
    $('[data-slide]').first().trigger('click')
fat's avatar
fat committed
432
    assert.strictEqual($carousel.data('bs.carousel')._config.interval, 1814, 'even if there is an data-modal attribute set')
433
    $carousel.remove()
Dmitriy Budnik's avatar
Dmitriy Budnik committed
434

435
    $carousel.appendTo('body')
fat's avatar
fat committed
436
    $('[data-slide]').first().trigger('click')
437
    $carousel.attr('data-interval', 1860)
fat's avatar
fat committed
438
    $('[data-slide]').first().trigger('click')
fat's avatar
fat committed
439
    assert.strictEqual($carousel.data('bs.carousel')._config.interval, 1814, 'attributes should be read only on initialization')
440
441
442
443
444
    $carousel.remove()

    $carousel.attr('data-interval', false)
    $carousel.appendTo('body')
    $carousel.bootstrapCarousel(1)
fat's avatar
fat committed
445
    assert.strictEqual($carousel.data('bs.carousel')._config.interval, false, 'data attribute has higher priority than default options')
446
    $carousel.remove()
XhmikosR's avatar
XhmikosR committed
447
  })
448

fat's avatar
fat committed
449
450
  QUnit.test('should skip over non-items when using item indices', function (assert) {
    assert.expect(2)
Heinrich Fenkart's avatar
Heinrich Fenkart committed
451
452
    var templateHTML = '<div id="myCarousel" class="carousel" data-interval="1814">'
        + '<div class="carousel-inner">'
fat's avatar
fat committed
453
        + '<div class="carousel-item active">'
Heinrich Fenkart's avatar
Heinrich Fenkart committed
454
455
456
        + '<img alt="">'
        + '</div>'
        + '<script type="text/x-metamorph" id="thingy"/>'
fat's avatar
fat committed
457
        + '<div class="carousel-item">'
Heinrich Fenkart's avatar
Heinrich Fenkart committed
458
459
        + '<img alt="">'
        + '</div>'
fat's avatar
fat committed
460
        + '<div class="carousel-item">'
Heinrich Fenkart's avatar
Heinrich Fenkart committed
461
462
463
        + '</div>'
        + '</div>'
        + '</div>'
464
    var $template = $(templateHTML)
465

466
    $template.bootstrapCarousel()
467

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

470
    $template.bootstrapCarousel(1)
471

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

fat's avatar
fat committed
475
476
  QUnit.test('should skip over non-items when using next/prev methods', function (assert) {
    assert.expect(2)
477
478
    var templateHTML = '<div id="myCarousel" class="carousel" data-interval="1814">'
        + '<div class="carousel-inner">'
fat's avatar
fat committed
479
        + '<div class="carousel-item active">'
480
481
482
        + '<img alt="">'
        + '</div>'
        + '<script type="text/x-metamorph" id="thingy"/>'
fat's avatar
fat committed
483
        + '<div class="carousel-item">'
484
485
        + '<img alt="">'
        + '</div>'
fat's avatar
fat committed
486
        + '<div class="carousel-item">'
487
488
489
490
491
492
493
        + '</div>'
        + '</div>'
        + '</div>'
    var $template = $(templateHTML)

    $template.bootstrapCarousel()

fat's avatar
fat committed
494
    assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item active')
495
496
497

    $template.bootstrapCarousel('next')

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

fat's avatar
fat committed
501
502
  QUnit.test('should go to previous item if left arrow key is pressed', function (assert) {
    assert.expect(2)
503
504
    var templateHTML = '<div id="myCarousel" class="carousel" data-interval="false">'
        + '<div class="carousel-inner">'
fat's avatar
fat committed
505
        + '<div id="first" class="carousel-item">'
506
507
        + '<img alt="">'
        + '</div>'
fat's avatar
fat committed
508
        + '<div id="second" class="carousel-item active">'
509
510
        + '<img alt="">'
        + '</div>'
fat's avatar
fat committed
511
        + '<div id="third" class="carousel-item">'
512
513
514
515
516
517
518
519
        + '<img alt="">'
        + '</div>'
        + '</div>'
        + '</div>'
    var $template = $(templateHTML)

    $template.bootstrapCarousel()

fat's avatar
fat committed
520
    assert.strictEqual($template.find('.carousel-item')[1], $template.find('.active')[0], 'second item active')
521
522
523

    $template.trigger($.Event('keydown', { which: 37 }))

fat's avatar
fat committed
524
    assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item active')
525
526
  })

fat's avatar
fat committed
527
528
  QUnit.test('should go to next item if right arrow key is pressed', function (assert) {
    assert.expect(2)
529
530
    var templateHTML = '<div id="myCarousel" class="carousel" data-interval="false">'
        + '<div class="carousel-inner">'
fat's avatar
fat committed
531
        + '<div id="first" class="carousel-item active">'
532
533
        + '<img alt="">'
        + '</div>'
fat's avatar
fat committed
534
        + '<div id="second" class="carousel-item">'
535
536
        + '<img alt="">'
        + '</div>'
fat's avatar
fat committed
537
        + '<div id="third" class="carousel-item">'
538
539
540
541
542
543
544
545
        + '<img alt="">'
        + '</div>'
        + '</div>'
        + '</div>'
    var $template = $(templateHTML)

    $template.bootstrapCarousel()

fat's avatar
fat committed
546
    assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item active')
547
548
549

    $template.trigger($.Event('keydown', { which: 39 }))

fat's avatar
fat committed
550
    assert.strictEqual($template.find('.carousel-item')[1], $template.find('.active')[0], 'second item active')
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
580
581
582
583
  QUnit.test('should not prevent keydown if key is not ARROW_LEFT or ARROW_RIGHT', function (assert) {
    assert.expect(2)
    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>'
    var $template = $(templateHTML)

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

    var eventArrowDown = $.Event('keydown', { which: 40 })
    var eventArrowUp   = $.Event('keydown', { which: 38 })

    $template.one('keydown', function (event) {
      assert.strictEqual(event.isDefaultPrevented(), false)
    })

    $template.trigger(eventArrowDown)

    $template.one('keydown', function (event) {
      assert.strictEqual(event.isDefaultPrevented(), false)
      done()
    })

    $template.trigger(eventArrowUp)
  })

fat's avatar
fat committed
584
585
  QUnit.test('should support disabling the keyboard navigation', function (assert) {
    assert.expect(3)
586
587
    var templateHTML = '<div id="myCarousel" class="carousel" data-interval="false" data-keyboard="false">'
        + '<div class="carousel-inner">'
fat's avatar
fat committed
588
        + '<div id="first" class="carousel-item active">'
589
590
        + '<img alt="">'
        + '</div>'
fat's avatar
fat committed
591
        + '<div id="second" class="carousel-item">'
592
593
        + '<img alt="">'
        + '</div>'
fat's avatar
fat committed
594
        + '<div id="third" class="carousel-item">'
595
596
597
598
599
600
601
602
        + '<img alt="">'
        + '</div>'
        + '</div>'
        + '</div>'
    var $template = $(templateHTML)

    $template.bootstrapCarousel()

fat's avatar
fat committed
603
    assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item active')
604
605
606

    $template.trigger($.Event('keydown', { which: 39 }))

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

    $template.trigger($.Event('keydown', { which: 37 }))

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

fat's avatar
fat committed
614
615
  QUnit.test('should ignore keyboard events within <input>s and <textarea>s', function (assert) {
    assert.expect(7)
616
617
    var templateHTML = '<div id="myCarousel" class="carousel" data-interval="false">'
        + '<div class="carousel-inner">'
fat's avatar
fat committed
618
        + '<div id="first" class="carousel-item active">'
619
620
621
622
        + '<img alt="">'
        + '<input type="text" id="in-put">'
        + '<textarea id="text-area"></textarea>'
        + '</div>'
fat's avatar
fat committed
623
        + '<div id="second" class="carousel-item">'
624
625
        + '<img alt="">'
        + '</div>'
fat's avatar
fat committed
626
        + '<div id="third" class="carousel-item">'
627
628
629
630
631
632
633
634
        + '<img alt="">'
        + '</div>'
        + '</div>'
        + '</div>'
    var $template = $(templateHTML)
    var $input = $template.find('#in-put')
    var $textarea = $template.find('#text-area')

fat's avatar
fat committed
635
636
    assert.strictEqual($input.length, 1, 'found <input>')
    assert.strictEqual($textarea.length, 1, 'found <textarea>')
637
638
639

    $template.bootstrapCarousel()

fat's avatar
fat committed
640
    assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item active')
641
642
643


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

    $input.trigger($.Event('keydown', { which: 37 }))
fat's avatar
fat committed
647
    assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item still active after left arrow press in <input>')
648
649
650


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

    $textarea.trigger($.Event('keydown', { which: 37 }))
fat's avatar
fat committed
654
    assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item still active after left arrow press in <textarea>')
655
656
  })

fat's avatar
fat committed
657
658
  QUnit.test('should wrap around from end to start when wrap option is true', function (assert) {
    assert.expect(3)
659
660
661
662
663
664
665
    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">'
fat's avatar
fat committed
666
        + '<div class="carousel-item active" id="one">'
667
668
        + '<div class="carousel-caption"/>'
        + '</div>'
fat's avatar
fat committed
669
        + '<div class="carousel-item" id="two">'
670
671
        + '<div class="carousel-caption"/>'
        + '</div>'
fat's avatar
fat committed
672
        + '<div class="carousel-item" id="three">'
673
674
675
676
677
678
679
        + '<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>'
    var $carousel = $(carouselHTML)
fat's avatar
fat committed
680
    var getActiveId = function () { return $carousel.find('.carousel-item.active').attr('id') }
681

682
    var done = assert.async()
683
684
685

    $carousel
      .one('slid.bs.carousel', function () {
fat's avatar
fat committed
686
        assert.strictEqual(getActiveId(), 'two', 'carousel slid from 1st to 2nd slide')
687
688
        $carousel
          .one('slid.bs.carousel', function () {
fat's avatar
fat committed
689
            assert.strictEqual(getActiveId(), 'three', 'carousel slid from 2nd to 3rd slide')
690
691
            $carousel
              .one('slid.bs.carousel', function () {
fat's avatar
fat committed
692
                assert.strictEqual(getActiveId(), 'one', 'carousel wrapped around and slid from 3rd to 1st slide')
693
                done()
694
695
696
697
698
699
700
701
              })
              .bootstrapCarousel('next')
          })
          .bootstrapCarousel('next')
      })
      .bootstrapCarousel('next')
  })

fat's avatar
fat committed
702
703
  QUnit.test('should wrap around from start to end when wrap option is true', function (assert) {
    assert.expect(1)
704
705
706
707
708
709
710
    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">'
fat's avatar
fat committed
711
        + '<div class="carousel-item active" id="one">'
712
713
        + '<div class="carousel-caption"/>'
        + '</div>'
fat's avatar
fat committed
714
        + '<div class="carousel-item" id="two">'
715
716
        + '<div class="carousel-caption"/>'
        + '</div>'
fat's avatar
fat committed
717
        + '<div class="carousel-item" id="three">'
718
719
720
721
722
723
724
725
        + '<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>'
    var $carousel = $(carouselHTML)

726
    var done = assert.async()
727
728
729

    $carousel
      .on('slid.bs.carousel', function () {
fat's avatar
fat committed
730
        assert.strictEqual($carousel.find('.carousel-item.active').attr('id'), 'three', 'carousel wrapped around and slid from 1st to 3rd slide')
731
        done()
732
733
734
735
      })
      .bootstrapCarousel('prev')
  })

fat's avatar
fat committed
736
737
  QUnit.test('should stay at the end when the next method is called and wrap is false', function (assert) {
    assert.expect(3)
738
739
740
741
742
743
744
    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">'
fat's avatar
fat committed
745
        + '<div class="carousel-item active" id="one">'
746
747
        + '<div class="carousel-caption"/>'
        + '</div>'
fat's avatar
fat committed
748
        + '<div class="carousel-item" id="two">'
749
750
        + '<div class="carousel-caption"/>'
        + '</div>'
fat's avatar
fat committed
751
        + '<div class="carousel-item" id="three">'
752
753
754
755
756
757
758
        + '<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>'
    var $carousel = $(carouselHTML)
fat's avatar
fat committed
759
    var getActiveId = function () { return $carousel.find('.carousel-item.active').attr('id') }
760

761
    var done = assert.async()
762
763
764

    $carousel
      .one('slid.bs.carousel', function () {
fat's avatar
fat committed
765
        assert.strictEqual(getActiveId(), 'two', 'carousel slid from 1st to 2nd slide')
766
767
        $carousel
          .one('slid.bs.carousel', function () {
fat's avatar
fat committed
768
            assert.strictEqual(getActiveId(), 'three', 'carousel slid from 2nd to 3rd slide')
769
770
            $carousel
              .one('slid.bs.carousel', function () {
fat's avatar
fat committed
771
                assert.ok(false, 'carousel slid when it should not have slid')
772
773
              })
              .bootstrapCarousel('next')
fat's avatar
fat committed
774
            assert.strictEqual(getActiveId(), 'three', 'carousel did not wrap around and stayed on 3rd slide')
775
            done()
776
777
778
779
780
781
          })
          .bootstrapCarousel('next')
      })
      .bootstrapCarousel('next')
  })

fat's avatar
fat committed
782
783
  QUnit.test('should stay at the start when the prev method is called and wrap is false', function (assert) {
    assert.expect(1)
784
785
786
787
788
789
790
    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">'
fat's avatar
fat committed
791
        + '<div class="carousel-item active" id="one">'
792
793
        + '<div class="carousel-caption"/>'
        + '</div>'
fat's avatar
fat committed
794
        + '<div class="carousel-item" id="two">'
795
796
        + '<div class="carousel-caption"/>'
        + '</div>'
fat's avatar
fat committed
797
        + '<div class="carousel-item" id="three">'
798
799
800
801
802
803
804
805
806
807
        + '<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>'
    var $carousel = $(carouselHTML)

    $carousel
      .on('slid.bs.carousel', function () {
fat's avatar
fat committed
808
        assert.ok(false, 'carousel slid when it should not have slid')
809
810
      })
      .bootstrapCarousel('prev')
fat's avatar
fat committed
811
    assert.strictEqual($carousel.find('.carousel-item.active').attr('id'), 'one', 'carousel did not wrap around and stayed on 1st slide')
812
  })
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844

  QUnit.test('should not prevent keydown for inputs and textareas', function (assert) {
    assert.expect(2)
    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>'
    var $template = $(templateHTML)
    var done = assert.async()
    $template.appendTo('#qunit-fixture')
    var $inputText = $template.find('#inputText')
    var $textArea = $template.find('#txtArea')
    $template.bootstrapCarousel()

    var eventKeyDown = $.Event('keydown', { which: 65 }) // 65 for "a"
    $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)
  })
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881

  QUnit.test('Should not go to the next item when the carousel is not visible', function (assert) {
    assert.expect(2)
    var done = assert.async()
    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>'
             + '  <a class="right carousel-control" href="#myCarousel" data-slide="next">&rsaquo;</a>'
             + '</div>'
    var $html = $(html)
    $html
      .appendTo('#qunit-fixture')
      .bootstrapCarousel()

    var $firstItem = $('#firstItem')
    setTimeout(function () {
      assert.ok($firstItem.hasClass('active'))
      $html
        .bootstrapCarousel('dispose')
        .attr('style', 'visibility: hidden;')
        .bootstrapCarousel()

      setTimeout(function () {
        assert.ok($firstItem.hasClass('active'))
        done()
      }, 80)
    }, 80)
  })
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920

  QUnit.test('Should not go to the next item when the parent of the carousel is not visible', function (assert) {
    assert.expect(2)
    var done = assert.async()
    var html = '<div id="parent" style="display: none;">'
             + '  <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>'
             + '    <a class="right carousel-control" href="#myCarousel" data-slide="next">&rsaquo;</a>'
             + '  </div>'
             + '</div>'
    var $html = $(html)
    $html.appendTo('#qunit-fixture')
    var $parent = $html.find('#parent')
    var $carousel = $html.find('#myCarousel')
    $carousel.bootstrapCarousel()
    var $firstItem = $('#firstItem')

    setTimeout(function () {
      assert.ok($firstItem.hasClass('active'))
      $carousel.bootstrapCarousel('dispose')
      $parent.attr('style', 'visibility: hidden;')
      $carousel.bootstrapCarousel()

      setTimeout(function () {
        assert.ok($firstItem.hasClass('active'))
        done()
      }, 80)
    }, 80)
  })
921
})