0
0
mirror of https://github.com/twbs/bootstrap.git synced 2024-11-29 11:24:18 +01:00
Bootstrap/js/tests/unit/carousel.js

402 lines
15 KiB
JavaScript
Raw Normal View History

$(function () {
2014-03-17 08:12:55 +01:00
'use strict';
module('carousel plugin')
2014-04-21 22:08:16 +02:00
test('should be defined on jQuery object', function () {
2014-02-13 08:55:12 +01:00
ok($(document.body).carousel, 'carousel method is defined')
})
module('carousel', {
2014-03-17 08:12:55 +01:00
setup: 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()
},
2014-03-17 08:12:55 +01:00
teardown: function () {
$.fn.carousel = $.fn.bootstrapCarousel
delete $.fn.bootstrapCarousel
}
})
test('should provide no conflict', function () {
2014-06-18 21:33:49 +02:00
strictEqual($.fn.carousel, undefined, 'carousel was set back to undefined (orig value)')
})
2014-06-18 21:33:49 +02:00
test('should return jquery collection containing the element', function () {
var $el = $('<div/>')
var $carousel = $el.bootstrapCarousel()
ok($carousel instanceof $, 'returns jquery collection')
strictEqual($carousel[0], $el[0], 'collection contains element')
2014-02-13 08:55:12 +01:00
})
2014-06-18 21:33:49 +02:00
test('should not fire slid when slide is prevented', function () {
2014-02-13 08:55:12 +01:00
stop()
$('<div class="carousel"/>')
.on('slide.bs.carousel', function (e) {
2014-02-17 20:56:46 +01:00
e.preventDefault()
2014-06-18 21:33:49 +02:00
ok(true, 'slide event fired')
2014-02-17 20:56:46 +01:00
start()
2014-02-13 08:55:12 +01:00
})
.on('slid.bs.carousel', function () {
2014-06-18 21:33:49 +02:00
ok(false, 'slid event fired')
})
.bootstrapCarousel('next')
2014-02-13 08:55:12 +01:00
})
2014-02-13 08:55:12 +01:00
test('should reset when slide is prevented', function () {
2014-07-06 11:56:12 +02:00
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="item active">'
+ '<div class="carousel-caption"/>'
+ '</div>'
+ '<div class="item">'
+ '<div class="carousel-caption"/>'
+ '</div>'
+ '<div class="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>'
2014-06-18 21:33:49 +02:00
var $carousel = $(carouselHTML)
2014-02-13 08:55:12 +01:00
stop()
2014-06-18 21:33:49 +02:00
$carousel
.one('slide.bs.carousel', function (e) {
e.preventDefault()
setTimeout(function () {
ok($carousel.find('.item:eq(0)').is('.active'), 'first item still active')
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 () {
ok(!$carousel.find('.item:eq(0)').is('.active'), 'first item still active')
ok(!$carousel.find('.carousel-indicators li:eq(0)').is('.active'), 'first indicator still active')
ok($carousel.find('.item:eq(1)').is('.active'), 'second item active')
ok($carousel.find('.carousel-indicators li:eq(1)').is('.active'), 'second indicator active')
start()
}, 0)
})
.bootstrapCarousel('next')
2014-02-13 08:55:12 +01:00
})
2014-02-13 08:55:12 +01:00
test('should fire slide event with direction', function () {
2014-07-06 11:56:12 +02:00
var carouselHTML = '<div id="myCarousel" class="carousel slide">'
+ '<div class="carousel-inner">'
+ '<div class="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="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="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>'
2014-06-18 21:33:49 +02:00
var $carousel = $(carouselHTML)
2014-02-13 08:55:12 +01:00
stop()
2014-06-18 21:33:49 +02:00
$carousel
.one('slide.bs.carousel', function (e) {
ok(e.direction, 'direction present on next')
strictEqual(e.direction, 'left', 'direction is left on next')
$carousel
.one('slide.bs.carousel', function (e) {
ok(e.direction, 'direction present on prev')
strictEqual(e.direction, 'right', 'direction is right on prev')
start()
})
.bootstrapCarousel('prev')
})
.bootstrapCarousel('next')
2014-02-13 08:55:12 +01:00
})
test('should fire slid event with direction', function () {
2014-07-06 11:56:12 +02:00
var carouselHTML = '<div id="myCarousel" class="carousel slide">'
+ '<div class="carousel-inner">'
+ '<div class="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="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="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>'
2014-06-18 21:33:49 +02:00
var $carousel = $(carouselHTML)
stop()
2014-06-18 21:33:49 +02:00
$carousel
.one('slid.bs.carousel', function (e) {
ok(e.direction, 'direction present on next')
strictEqual(e.direction, 'left', 'direction is left on next')
$carousel
.one('slid.bs.carousel', function (e) {
ok(e.direction, 'direction present on prev')
strictEqual(e.direction, 'right', 'direction is right on prev')
start()
})
.bootstrapCarousel('prev')
})
.bootstrapCarousel('next')
})
2014-02-13 08:55:12 +01:00
test('should fire slide event with relatedTarget', function () {
2014-07-06 11:56:12 +02:00
var template = '<div id="myCarousel" class="carousel slide">'
+ '<div class="carousel-inner">'
+ '<div class="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="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="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>'
2014-06-18 21:33:49 +02:00
2014-02-13 08:55:12 +01:00
stop()
2014-06-18 21:33:49 +02:00
2014-02-13 08:55:12 +01:00
$(template)
.on('slide.bs.carousel', function (e) {
2014-06-18 21:33:49 +02:00
ok(e.relatedTarget, 'relatedTarget present')
ok($(e.relatedTarget).hasClass('item'), 'relatedTarget has class "item"')
2014-02-17 20:56:46 +01:00
start()
2012-10-16 08:49:01 +02:00
})
.bootstrapCarousel('next')
2014-02-13 08:55:12 +01:00
})
test('should fire slid event with relatedTarget', function () {
2014-07-06 11:56:12 +02:00
var template = '<div id="myCarousel" class="carousel slide">'
+ '<div class="carousel-inner">'
+ '<div class="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="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="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>'
2014-06-18 21:33:49 +02:00
stop()
2014-06-18 21:33:49 +02:00
$(template)
.on('slid.bs.carousel', function (e) {
2014-06-18 21:33:49 +02:00
ok(e.relatedTarget, 'relatedTarget present')
ok($(e.relatedTarget).hasClass('item'), 'relatedTarget has class "item"')
start()
})
.bootstrapCarousel('next')
})
2014-06-18 21:33:49 +02:00
test('should set interval from data attribute', function () {
2014-07-06 11:56:12 +02:00
var templateHTML = '<div id="myCarousel" class="carousel slide">'
+ '<div class="carousel-inner">'
+ '<div class="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="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="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>'
2014-06-18 21:33:49 +02:00
var $carousel = $(templateHTML)
$carousel.attr('data-interval', 1814)
2012-10-16 08:49:01 +02:00
2014-06-18 21:33:49 +02:00
$carousel.appendTo('body')
2014-02-17 20:56:46 +01:00
$('[data-slide]').first().click()
2014-06-18 21:33:49 +02:00
equal($carousel.data('bs.carousel').options.interval, 1814)
$carousel.remove()
2012-10-15 20:55:32 +02:00
2014-06-18 21:33:49 +02:00
$carousel.appendTo('body').attr('data-modal', 'foobar')
2014-02-17 20:56:46 +01:00
$('[data-slide]').first().click()
2014-06-18 21:33:49 +02:00
equal($carousel.data('bs.carousel').options.interval, 1814, 'even if there is an data-modal attribute set')
$carousel.remove()
2012-10-16 08:49:01 +02:00
2014-06-18 21:33:49 +02:00
$carousel.appendTo('body')
2014-02-17 20:56:46 +01:00
$('[data-slide]').first().click()
2014-06-18 21:33:49 +02:00
$carousel.attr('data-interval', 1860)
2014-02-17 20:56:46 +01:00
$('[data-slide]').first().click()
2014-06-18 21:33:49 +02:00
equal($carousel.data('bs.carousel').options.interval, 1814, 'attributes should be read only on initialization')
$carousel.remove()
$carousel.attr('data-interval', false)
$carousel.appendTo('body')
$carousel.bootstrapCarousel(1)
strictEqual($carousel.data('bs.carousel').options.interval, false, 'data attribute has higher priority than default options')
$carousel.remove()
2014-02-13 08:55:12 +01:00
})
test('should skip over non-items when using item indices', function () {
2014-07-06 11:56:12 +02:00
var templateHTML = '<div id="myCarousel" class="carousel" data-interval="1814">'
+ '<div class="carousel-inner">'
+ '<div class="item active">'
+ '<img alt="">'
+ '</div>'
+ '<script type="text/x-metamorph" id="thingy"/>'
+ '<div class="item">'
+ '<img alt="">'
+ '</div>'
+ '<div class="item">'
+ '</div>'
+ '</div>'
+ '</div>'
2014-06-18 21:33:49 +02:00
var $template = $(templateHTML)
$template.bootstrapCarousel()
2014-06-18 21:33:49 +02:00
strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item active')
$template.bootstrapCarousel(1)
2014-06-18 21:33:49 +02:00
strictEqual($template.find('.item')[1], $template.find('.active')[0], 'second item active')
})
test('should skip over non-items when using next/prev methods', function () {
var templateHTML = '<div id="myCarousel" class="carousel" data-interval="1814">'
+ '<div class="carousel-inner">'
+ '<div class="item active">'
+ '<img alt="">'
+ '</div>'
+ '<script type="text/x-metamorph" id="thingy"/>'
+ '<div class="item">'
+ '<img alt="">'
+ '</div>'
+ '<div class="item">'
+ '</div>'
+ '</div>'
+ '</div>'
var $template = $(templateHTML)
$template.bootstrapCarousel()
strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item active')
$template.bootstrapCarousel('next')
strictEqual($template.find('.item')[1], $template.find('.active')[0], 'second item active')
})
})