From 038a63b0ebce276186fae93916703dd0e70ac4a1 Mon Sep 17 00:00:00 2001 From: Heinrich Fenkart Date: Wed, 10 Sep 2014 22:55:33 +0200 Subject: Add `keyboard` option to carousel Also adds unit tests for keyboard events. Fixes #14468. --- js/tests/unit/carousel.js | 79 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) (limited to 'js/tests') diff --git a/js/tests/unit/carousel.js b/js/tests/unit/carousel.js index 64d214462..6f0b9642f 100644 --- a/js/tests/unit/carousel.js +++ b/js/tests/unit/carousel.js @@ -399,6 +399,85 @@ $(function () { strictEqual($template.find('.item')[1], $template.find('.active')[0], 'second item active') }) + test('should go to previous item if left arrow key is pressed', function () { + var templateHTML = '' + var $template = $(templateHTML) + + $template.bootstrapCarousel() + + strictEqual($template.find('.item')[1], $template.find('.active')[0], 'second item active') + + $template.trigger($.Event('keydown', { which: 37 })) + + strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item active') + }) + + test('should go to next item if right arrow key is pressed', function () { + var templateHTML = '' + var $template = $(templateHTML) + + $template.bootstrapCarousel() + + strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item active') + + $template.trigger($.Event('keydown', { which: 39 })) + + strictEqual($template.find('.item')[1], $template.find('.active')[0], 'second item active') + }) + + test('should support disabling the keyboard navigation', function () { + var templateHTML = '' + var $template = $(templateHTML) + + $template.bootstrapCarousel() + + strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item active') + + $template.trigger($.Event('keydown', { which: 39 })) + + strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item still active after right arrow press') + + $template.trigger($.Event('keydown', { which: 37 })) + + strictEqual($template.find('.item')[0], $template.find('.active')[0], 'first item still active after left arrow press') + }) + test('should only add mouseenter and mouseleave listeners when not on mobile', function () { var isMobile = 'ontouchstart' in document.documentElement var templateHTML = '