From dbd76db83ede07b559d3a33ba21e38b786bb21ec Mon Sep 17 00:00:00 2001 From: Nikki Gonzales <38495263+nikkithelegendarypokemonster@users.noreply.github.com> Date: Thu, 21 Nov 2024 11:49:40 +0800 Subject: [PATCH] Menu: fix unable to unselect an menuitem (T1253750) (#28402) --- .../js/__internal/ui/menu/m_menu.ts | 2 +- .../tests/DevExpress.ui.widgets/menu.tests.js | 35 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/packages/devextreme/js/__internal/ui/menu/m_menu.ts b/packages/devextreme/js/__internal/ui/menu/m_menu.ts index 6f32392efd13..0c7cc38998e3 100644 --- a/packages/devextreme/js/__internal/ui/menu/m_menu.ts +++ b/packages/devextreme/js/__internal/ui/menu/m_menu.ts @@ -1038,7 +1038,7 @@ class Menu extends MenuBase { unselectItem(itemElement): void { this._hideSubmenu(this._visibleSubmenu); - super.selectItem(itemElement); + super.unselectItem(itemElement); } } diff --git a/packages/devextreme/testing/tests/DevExpress.ui.widgets/menu.tests.js b/packages/devextreme/testing/tests/DevExpress.ui.widgets/menu.tests.js index 7912c0d5590a..02d6eda3fbee 100644 --- a/packages/devextreme/testing/tests/DevExpress.ui.widgets/menu.tests.js +++ b/packages/devextreme/testing/tests/DevExpress.ui.widgets/menu.tests.js @@ -1179,6 +1179,41 @@ QUnit.module('Menu - selection', { assert.equal($items.find('.' + DX_MENU_ITEM_TEXT_CLASS).text(), 'item3'); }); + QUnit.test('should be able to select an item via .selectItem() (T1253750)', function(assert) { + const menu = createMenu({ + items: [ + { + text: 'menu item 1', + selectable: true, + }, + ], + }); + const item = $(menu.element).find(`.${DX_MENU_ITEM_CLASS}`).eq(0); + + menu.instance.selectItem(item[0]); + + assert.strictEqual(item.hasClass(DX_MENU_ITEM_SELECTED_CLASS), true, 'item has the selected class after initialization'); + }); + + QUnit.test('should be able to unselect currently selected item (T1253750)', function(assert) { + const menu = createMenu({ + items: [ + { + text: 'menu item 1', + selectable: true, + selected: true, + }, + ], + }); + const item = $(menu.element).find(`.${DX_MENU_ITEM_CLASS}`).eq(0); + + assert.strictEqual(item.hasClass(DX_MENU_ITEM_SELECTED_CLASS), true, 'item has the selected class after initialization'); + + menu.instance.unselectItem(item[0]); + + assert.strictEqual(item.hasClass(DX_MENU_ITEM_SELECTED_CLASS), false, 'item does not have the selected class after unselecting'); + }); + QUnit.test('Selection in different submenus', function(assert) { const items = [ { text: 'root1', items: [{ text: 'item1-1' }] },