From b3d1b4e9f4aa7d78a668b4b89231ca5388e2429c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Roucheray?= Date: Fri, 8 Mar 2024 16:45:08 +0100 Subject: [PATCH] fix: Prevent several instances of a sub menu when opening with the mouse --- CHANGELOG.md | 4 ++ .../context-menu-content.component.spec.ts | 45 +++++++++++++++++++ .../context-menu-content.component.ts | 2 +- 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fefb80c..59e2e72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## [Unreleased] +### Fix + +- Prevent several instances of a sub menu when opening with the mouse + ## [17.0.0] - 2023-11-10 ### BREAKING CHANGES diff --git a/libs/ngx-contextmenu/src/lib/components/context-menu-content/context-menu-content.component.spec.ts b/libs/ngx-contextmenu/src/lib/components/context-menu-content/context-menu-content.component.spec.ts index 4f1a1e9..42f69e0 100644 --- a/libs/ngx-contextmenu/src/lib/components/context-menu-content/context-menu-content.component.spec.ts +++ b/libs/ngx-contextmenu/src/lib/components/context-menu-content/context-menu-content.component.spec.ts @@ -903,4 +903,49 @@ describe('Component: ContextMenuContentComponent', () => { ).not.toHaveBeenCalled(); }); }); + + describe('#openSubMenu', () => { + beforeEach(() => { + configureTestingModule(); + component.contextMenuContentItems = new QueryList(); + }); + + it('should show subMenu', () => { + const subMenu = + TestBed.createComponent(ContextMenuComponent).componentInstance; + const event = new MouseEvent('click'); + + spyOn(subMenu, 'show'); + component.openSubMenu(subMenu, event); + + expect(subMenu.show).toHaveBeenCalledWith({ + anchoredTo: 'position', + x: 0, + y: 0, + value: undefined, + }); + }); + + it('should not show subMenu if already opened', () => { + const subMenu = + TestBed.createComponent(ContextMenuComponent).componentInstance; + const event = new MouseEvent('click'); + spyOnProperty(subMenu, 'isOpen', 'get').and.returnValue(true); + spyOn(subMenu, 'show'); + component.openSubMenu(subMenu, event); + + expect(subMenu.show).not.toHaveBeenCalled(); + }); + + it('should not throw if subMenu does not exist', () => { + const subMenu = + TestBed.createComponent(ContextMenuComponent).componentInstance; + const event = new MouseEvent('click'); + + spyOn(subMenu, 'show'); + expect(() => { + component.openSubMenu(undefined, event); + }).not.toThrow(); + }); + }); }); diff --git a/libs/ngx-contextmenu/src/lib/components/context-menu-content/context-menu-content.component.ts b/libs/ngx-contextmenu/src/lib/components/context-menu-content/context-menu-content.component.ts index 12fc982..e4db2e7 100644 --- a/libs/ngx-contextmenu/src/lib/components/context-menu-content/context-menu-content.component.ts +++ b/libs/ngx-contextmenu/src/lib/components/context-menu-content/context-menu-content.component.ts @@ -242,7 +242,7 @@ export class ContextMenuContentComponent subMenu: ContextMenuComponent | undefined, event: MouseEvent | KeyboardEvent ): void { - if (!subMenu) { + if (!subMenu || subMenu.isOpen) { return; }