$(function () { 'use strict' window.Carousel = typeof bootstrap !== 'undefined' ? bootstrap.Carousel : Carousel var originWinPointerEvent = window.PointerEvent window.MSPointerEvent = null var supportPointerEvent = Boolean(window.PointerEvent || window.MSPointerEvent) function clearPointerEvents() { window.PointerEvent = null } function restorePointerEvents() { window.PointerEvent = originWinPointerEvent } var stylesCarousel = [ '' ].join('') 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 () { $('.carousel').bootstrapCarousel('dispose') $.fn.carousel = $.fn.bootstrapCarousel delete $.fn.bootstrapCarousel $('#qunit-fixture').html('') } }) QUnit.test('should provide no conflict', function (assert) { assert.expect(1) assert.strictEqual(typeof $.fn.carousel, 'undefined', 'carousel was set back to undefined (orig value)') }) QUnit.test('should return version', function (assert) { 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) }) 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 (err) { message = err.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 (err) { message = err.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() var $carousel = $('') $carousel.appendTo('#qunit-fixture') $carousel[0].addEventListener('slide.bs.carousel', function (e) { e.preventDefault() assert.ok(true, 'slide event fired') done() }) $carousel[0].addEventListener('slid.bs.carousel', function () { assert.ok(false, 'slid event fired') }) $carousel.bootstrapCarousel('next') }) QUnit.test('should reset when slide is prevented', function (assert) { assert.expect(6) var carouselHTML = ' ' var $carousel = $(carouselHTML) $carousel.appendTo('#qunit-fixture') var done = assert.async() 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) } $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) } $carousel[0].addEventListener('slid.bs.carousel', onSlid) $carousel.bootstrapCarousel('next') }) QUnit.test('should fire slide event with direction', function (assert) { assert.expect(4) var carouselHTML = ' ' var $carousel = $(carouselHTML) $carousel.appendTo('#qunit-fixture') var done = assert.async() 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) $carousel.bootstrapCarousel('next') }) QUnit.test('should fire slid event with direction', function (assert) { assert.expect(4) var carouselHTML = ' ' var $carousel = $(carouselHTML) $carousel.appendTo('#qunit-fixture') var done = assert.async() 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) $carousel.bootstrapCarousel('next') }) QUnit.test('should fire slide event with relatedTarget', function (assert) { assert.expect(2) var template = ' ' var done = assert.async() var $carousel = $(template) $carousel.appendTo('#qunit-fixture') 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() } $carousel[0].addEventListener('slide.bs.carousel', onSlide) $carousel.bootstrapCarousel('next') }) QUnit.test('should fire slid event with relatedTarget', function (assert) { assert.expect(2) var template = ' ' var done = assert.async() var $carousel = $(template) $carousel.appendTo('#qunit-fixture') $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() }) $carousel.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() var $carousel = $(template) $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() }) $carousel[0].addEventListener('slide.bs.carousel', function (e) { assert.ok(typeof e.from !== 'undefined', 'from present') assert.ok(typeof e.to !== 'undefined', 'to present') }) $carousel.bootstrapCarousel('next') }) QUnit.test('should set interval from data attribute', function (assert) { assert.expect(4) var templateHTML = ' ' var $carousel = $(templateHTML) $carousel.attr('data-interval', 1814) $carousel.appendTo('body') $('[data-slide]').first()[0].dispatchEvent(new Event('click')) assert.strictEqual(Carousel._getInstance($carousel[0])._config.interval, 1814) $carousel.remove() $carousel.appendTo('body').attr('data-modal', 'foobar') $('[data-slide]').first()[0].dispatchEvent(new Event('click')) assert.strictEqual(Carousel._getInstance($carousel[0])._config.interval, 1814, 'even if there is an data-modal attribute set') $carousel.remove() $carousel.appendTo('body') $('[data-slide]').first()[0].dispatchEvent(new Event('click')) $carousel.attr('data-interval', 1860) $('[data-slide]').first()[0].dispatchEvent(new Event('click')) assert.strictEqual(Carousel._getInstance($carousel[0])._config.interval, 1814, 'attributes should be read only on initialization') $carousel.bootstrapCarousel('dispose') $carousel.remove() $carousel.attr('data-interval', false) $carousel.appendTo('body') $carousel.bootstrapCarousel(1) assert.strictEqual(Carousel._getInstance($carousel[0])._config.interval, false, 'data attribute has higher priority than default options') $carousel.remove() }) QUnit.test('should set interval from data attribute on individual carousel-item', function (assert) { assert.expect(2) var templateHTML = ' ' var $carousel = $(templateHTML).appendTo('#qunit-fixture') $carousel.bootstrapCarousel(1) var carousel = Carousel._getInstance($carousel[0]) assert.strictEqual(carousel._config.interval, 3814) carousel.dispose() $carousel.remove() $carousel = $carousel.appendTo('#qunit-fixture') $carousel.bootstrapCarousel(2) carousel = Carousel._getInstance($carousel[0]) assert.strictEqual(carousel._config.interval, 1814, 'reverts to default interval if no data-interval is set') $carousel.remove() }) QUnit.test('should skip over non-items when using item indices', function (assert) { assert.expect(2) var templateHTML = '