$(function () {
'use strict'
QUnit.module('carousel plugin')
QUnit.test('should be defined on jQuery object', function (assert) {
assert.expect(1)
assert.ok($(document.body).carousel, 'carousel method is defined')
})
QUnit.module('carousel', {
beforeEach: function () {
// 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()
},
afterEach: function () {
$.fn.carousel = $.fn.bootstrapCarousel
delete $.fn.bootstrapCarousel
}
})
QUnit.test('should provide no conflict', function (assert) {
assert.expect(1)
assert.strictEqual($.fn.carousel, undefined, 'carousel was set back to undefined (orig value)')
})
QUnit.test('should throw explicit error on undefined method', function (assert) {
assert.expect(1)
var $el = $('
')
$el.bootstrapCarousel()
try {
$el.bootstrapCarousel('noMethod')
}
catch (err) {
assert.strictEqual(err.message, 'No method named "noMethod"')
}
})
QUnit.test('should return jquery collection containing the element', function (assert) {
assert.expect(2)
var $el = $('')
var $carousel = $el.bootstrapCarousel()
assert.ok($carousel instanceof $, 'returns jquery collection')
assert.strictEqual($carousel[0], $el[0], 'collection contains element')
})
QUnit.test('should type check config options', function (assert) {
assert.expect(2)
var message
var expectedMessage = 'CAROUSEL: Option "interval" provided type "string" but expected type "(number|boolean)".'
var config = {
interval: 'fat sux'
}
try {
$('').bootstrapCarousel(config)
} catch (e) {
message = e.message
}
assert.ok(message === expectedMessage, 'correct error message')
config = {
keyboard: document.createElement('div')
}
expectedMessage = 'CAROUSEL: Option "keyboard" provided type "element" but expected type "boolean".'
try {
$('').bootstrapCarousel(config)
} catch (e) {
message = e.message
}
assert.ok(message === expectedMessage, 'correct error message')
})
QUnit.test('should not fire slid when slide is prevented', function (assert) {
assert.expect(1)
var done = assert.async()
$('')
.on('slide.bs.carousel', function (e) {
e.preventDefault()
assert.ok(true, 'slide event fired')
done()
})
.on('slid.bs.carousel', function () {
assert.ok(false, 'slid event fired')
})
.bootstrapCarousel('next')
})
QUnit.test('should reset when slide is prevented', function (assert) {
assert.expect(6)
var carouselHTML = '
'
+ ''
+ ''
+ ''
+ ''
+ ''
+ '
'
+ '
'
+ ''
+ '
'
+ '
'
+ ''
+ '
'
+ '
'
+ ''
+ '
'
+ '
'
+ ''
+ ''
+ '
'
var $carousel = $(carouselHTML)
var done = assert.async()
$carousel
.one('slide.bs.carousel', function (e) {
e.preventDefault()
setTimeout(function () {
assert.ok($carousel.find('.carousel-item:eq(0)').is('.active'), 'first item still active')
assert.ok($carousel.find('.carousel-indicators li:eq(0)').is('.active'), 'first indicator still active')
$carousel.bootstrapCarousel('next')
}, 0)
})
.one('slid.bs.carousel', function () {
setTimeout(function () {
assert.ok(!$carousel.find('.carousel-item:eq(0)').is('.active'), 'first item still active')
assert.ok(!$carousel.find('.carousel-indicators li:eq(0)').is('.active'), 'first indicator still active')
assert.ok($carousel.find('.carousel-item:eq(1)').is('.active'), 'second item active')
assert.ok($carousel.find('.carousel-indicators li:eq(1)').is('.active'), 'second indicator active')
done()
}, 0)
})
.bootstrapCarousel('next')
})
QUnit.test('should fire slide event with direction', function (assert) {
assert.expect(4)
var carouselHTML = '
'
+ '
'
+ '
'
+ ''
+ '
'
+ '
First Thumbnail label
'
+ '
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.
'
+ '
'
+ '
'
+ '
'
+ ''
+ '
'
+ '
Second Thumbnail label
'
+ '
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.
'
+ '
'
+ '
'
+ '
'
+ ''
+ '
'
+ '
Third Thumbnail label
'
+ '
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.
'
var $carousel = $(carouselHTML)
var done = assert.async()
$carousel
.one('slide.bs.carousel', function (e) {
assert.ok(e.direction, 'direction present on next')
assert.strictEqual(e.direction, 'left', 'direction is left on next')
$carousel
.one('slide.bs.carousel', function (e) {
assert.ok(e.direction, 'direction present on prev')
assert.strictEqual(e.direction, 'right', 'direction is right on prev')
done()
})
.bootstrapCarousel('prev')
})
.bootstrapCarousel('next')
})
QUnit.test('should fire slid event with direction', function (assert) {
assert.expect(4)
var carouselHTML = '
'
+ '
'
+ '
'
+ ''
+ '
'
+ '
First Thumbnail label
'
+ '
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.
'
+ '
'
+ '
'
+ '
'
+ ''
+ '
'
+ '
Second Thumbnail label
'
+ '
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.
'
+ '
'
+ '
'
+ '
'
+ ''
+ '
'
+ '
Third Thumbnail label
'
+ '
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.
'
var $carousel = $(carouselHTML)
var done = assert.async()
$carousel
.one('slid.bs.carousel', function (e) {
assert.ok(e.direction, 'direction present on next')
assert.strictEqual(e.direction, 'left', 'direction is left on next')
$carousel
.one('slid.bs.carousel', function (e) {
assert.ok(e.direction, 'direction present on prev')
assert.strictEqual(e.direction, 'right', 'direction is right on prev')
done()
})
.bootstrapCarousel('prev')
})
.bootstrapCarousel('next')
})
QUnit.test('should fire slide event with relatedTarget', function (assert) {
assert.expect(2)
var template = '
'
+ '
'
+ '
'
+ ''
+ '
'
+ '
First Thumbnail label
'
+ '
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.
'
+ '
'
+ '
'
+ '
'
+ ''
+ '
'
+ '
Second Thumbnail label
'
+ '
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.
'
+ '
'
+ '
'
+ '
'
+ ''
+ '
'
+ '
Third Thumbnail label
'
+ '
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.
'
var done = assert.async()
$(template)
.on('slide.bs.carousel', function (e) {
assert.ok(e.relatedTarget, 'relatedTarget present')
assert.ok($(e.relatedTarget).hasClass('carousel-item'), 'relatedTarget has class "item"')
done()
})
.bootstrapCarousel('next')
})
QUnit.test('should fire slid event with relatedTarget', function (assert) {
assert.expect(2)
var template = '
'
+ '
'
+ '
'
+ ''
+ '
'
+ '
First Thumbnail label
'
+ '
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.
'
+ '
'
+ '
'
+ '
'
+ ''
+ '
'
+ '
Second Thumbnail label
'
+ '
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.
'
+ '
'
+ '
'
+ '
'
+ ''
+ '
'
+ '
Third Thumbnail label
'
+ '
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.
'
var done = assert.async()
$(template)
.on('slid.bs.carousel', function (e) {
assert.ok(e.relatedTarget, 'relatedTarget present')
assert.ok($(e.relatedTarget).hasClass('carousel-item'), 'relatedTarget has class "item"')
done()
})
.bootstrapCarousel('next')
})
QUnit.test('should fire slid and slide events with from and to', function (assert) {
assert.expect(4)
var template = '
'
var done = assert.async()
$(template)
.on('slid.bs.carousel', function (e) {
assert.ok(e.from !== undefined, 'from present')
assert.ok(e.to !== undefined, 'to present')
$(this).off()
done()
})
.on('slide.bs.carousel', function (e) {
assert.ok(e.from !== undefined, 'from present')
assert.ok(e.to !== undefined, 'to present')
$(this).off('slide.bs.carousel')
})
.bootstrapCarousel('next')
})
QUnit.test('should set interval from data attribute', function (assert) {
assert.expect(4)
var templateHTML = '
'
+ '
'
+ '
'
+ ''
+ '
'
+ '
First Thumbnail label
'
+ '
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.
'
+ '
'
+ '
'
+ '
'
+ ''
+ '
'
+ '
Second Thumbnail label
'
+ '
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.
'
+ '
'
+ '
'
+ '
'
+ ''
+ '
'
+ '
Third Thumbnail label
'
+ '
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.
'
var $carousel = $(templateHTML)
$carousel.attr('data-interval', 1814)
$carousel.appendTo('body')
$('[data-slide]').first().trigger('click')
assert.strictEqual($carousel.data('bs.carousel')._config.interval, 1814)
$carousel.remove()
$carousel.appendTo('body').attr('data-modal', 'foobar')
$('[data-slide]').first().trigger('click')
assert.strictEqual($carousel.data('bs.carousel')._config.interval, 1814, 'even if there is an data-modal attribute set')
$carousel.remove()
$carousel.appendTo('body')
$('[data-slide]').first().trigger('click')
$carousel.attr('data-interval', 1860)
$('[data-slide]').first().trigger('click')
assert.strictEqual($carousel.data('bs.carousel')._config.interval, 1814, 'attributes should be read only on initialization')
$carousel.remove()
$carousel.attr('data-interval', false)
$carousel.appendTo('body')
$carousel.bootstrapCarousel(1)
assert.strictEqual($carousel.data('bs.carousel')._config.interval, false, 'data attribute has higher priority than default options')
$carousel.remove()
})
QUnit.test('should skip over non-items when using item indices', function (assert) {
assert.expect(2)
var templateHTML = '
'
+ '
'
+ '
'
+ ''
+ '
'
+ ''
+ '
'
+ ''
+ '
'
+ '
'
+ '
'
+ '
'
+ '
'
var $template = $(templateHTML)
$template.bootstrapCarousel()
assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item active')
$template.bootstrapCarousel(1)
assert.strictEqual($template.find('.carousel-item')[1], $template.find('.active')[0], 'second item active')
})
QUnit.test('should skip over non-items when using next/prev methods', function (assert) {
assert.expect(2)
var templateHTML = '
'
+ '
'
+ '
'
+ ''
+ '
'
+ ''
+ '
'
+ ''
+ '
'
+ '
'
+ '
'
+ '
'
+ '
'
var $template = $(templateHTML)
$template.bootstrapCarousel()
assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item active')
$template.bootstrapCarousel('next')
assert.strictEqual($template.find('.carousel-item')[1], $template.find('.active')[0], 'second item active')
})
QUnit.test('should go to previous item if left arrow key is pressed', function (assert) {
assert.expect(2)
var templateHTML = '
'
+ '
'
+ '
'
+ ''
+ '
'
+ '
'
+ ''
+ '
'
+ '
'
+ ''
+ '
'
+ '
'
+ '
'
var $template = $(templateHTML)
$template.bootstrapCarousel()
assert.strictEqual($template.find('.carousel-item')[1], $template.find('.active')[0], 'second item active')
$template.trigger($.Event('keydown', { which: 37 }))
assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item active')
})
QUnit.test('should go to next item if right arrow key is pressed', function (assert) {
assert.expect(2)
var templateHTML = '
'
+ '
'
+ '
'
+ ''
+ '
'
+ '
'
+ ''
+ '
'
+ '
'
+ ''
+ '
'
+ '
'
+ '
'
var $template = $(templateHTML)
$template.bootstrapCarousel()
assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item active')
$template.trigger($.Event('keydown', { which: 39 }))
assert.strictEqual($template.find('.carousel-item')[1], $template.find('.active')[0], 'second item active')
})
QUnit.test('should not prevent keydown if key is not ARROW_LEFT or ARROW_RIGHT', function (assert) {
assert.expect(2)
var templateHTML = '
'
+ '
'
+ '
'
+ ''
+ '
'
+ '
'
+ '
'
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)
})
QUnit.test('should support disabling the keyboard navigation', function (assert) {
assert.expect(3)
var templateHTML = '
'
+ '
'
+ '
'
+ ''
+ '
'
+ '
'
+ ''
+ '
'
+ '
'
+ ''
+ '
'
+ '
'
+ '
'
var $template = $(templateHTML)
$template.bootstrapCarousel()
assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item active')
$template.trigger($.Event('keydown', { which: 39 }))
assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item still active after right arrow press')
$template.trigger($.Event('keydown', { which: 37 }))
assert.strictEqual($template.find('.carousel-item')[0], $template.find('.active')[0], 'first item still active after left arrow press')
})
QUnit.test('should ignore keyboard events within s and