From 5f44e01dab9158dc4fc259bafc8c986f69487307 Mon Sep 17 00:00:00 2001 From: "Patrick H. Lauke" Date: Wed, 19 Jun 2019 11:43:49 +0100 Subject: [PATCH] Add missing dropdown unit test for closing with ESC (#28929) Backport of the unit test from https://github.com/twbs/bootstrap/pull/28928 --- js/tests/unit/dropdown.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) 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