From: Patrick H. Lauke Date: Wed, 19 Jun 2019 10:43:49 +0000 (+0100) Subject: Add missing dropdown unit test for closing with ESC (#28929) X-Git-Tag: v4.4.0~137 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F28937%2Fhead;p=thirdparty%2Fbootstrap.git Add missing dropdown unit test for closing with ESC (#28929) Backport of the unit test from https://github.com/twbs/bootstrap/pull/28928 --- diff --git a/js/tests/unit/dropdown.js b/js/tests/unit/dropdown.js index 652ec480e9..346b1a2e5a 100644 --- a/js/tests/unit/dropdown.js +++ b/js/tests/unit/dropdown.js @@ -776,6 +776,45 @@ $(function () { $triggerDropdown.trigger($.Event('click')) }) + QUnit.test('should close dropdown and set focus back to toggle when escape is pressed while focused on a dropdown item', function (assert) { + assert.expect(3) + var done = assert.async() + + var dropdownHTML = '
' + + '' + var $dropdown = $(dropdownHTML) + .appendTo('#qunit-fixture') + .find('[data-toggle="dropdown"]') + .bootstrapDropdown() + + var $item = $('#item') + var $toggle = $('#toggle') + + $dropdown + .parent('.dropdown') + .on('shown.bs.dropdown', function () { + // Forcibly focus first item + $item.focus() + assert.ok($(document.activeElement)[0] === $item[0], 'menu item initial focus set') + + // Key escape + $item.trigger('focus').trigger($.Event('keydown', { + which: 27 + })) + + assert.ok(!$dropdown.parent('.dropdown').hasClass('show'), 'dropdown menu was closed after escape') + assert.ok($(document.activeElement)[0] === $toggle[0], 'toggle has focus again once menu was closed after escape') + done() + }) + + $dropdown.trigger($.Event('click')) + }) + QUnit.test('should ignore keyboard events for s and