diff --git a/src/main/webapp/app/shared/metis/answer-post/answer-post.component.ts b/src/main/webapp/app/shared/metis/answer-post/answer-post.component.ts index e7d539291bc1..0b8e696e1ba4 100644 --- a/src/main/webapp/app/shared/metis/answer-post/answer-post.component.ts +++ b/src/main/webapp/app/shared/metis/answer-post/answer-post.component.ts @@ -6,6 +6,7 @@ import { HostListener, Inject, Input, + OnDestroy, Output, Renderer2, ViewChild, @@ -34,7 +35,7 @@ import { AnswerPostReactionsBarComponent } from 'app/shared/metis/posting-reacti ]), ], }) -export class AnswerPostComponent extends PostingDirective { +export class AnswerPostComponent extends PostingDirective implements OnDestroy { @Input() lastReadDate?: dayjs.Dayjs; @Input() isLastAnswer: boolean; @Output() openPostingCreateEditModal = new EventEmitter(); @@ -101,15 +102,18 @@ export class AnswerPostComponent extends PostingDirective { onRightClick(event: MouseEvent) { const targetElement = event.target as HTMLElement; - const isPointerCursor = window.getComputedStyle(targetElement).cursor === 'pointer'; + let isPointerCursor = false; + try { + isPointerCursor = window.getComputedStyle(targetElement).cursor === 'pointer'; + } catch (error) { + console.error('Failed to compute style:', error); + } if (!isPointerCursor) { event.preventDefault(); - if (AnswerPostComponent.activeDropdownPost && AnswerPostComponent.activeDropdownPost !== this) { - AnswerPostComponent.activeDropdownPost.showDropdown = false; - AnswerPostComponent.activeDropdownPost.enableBodyScroll(); - AnswerPostComponent.activeDropdownPost.changeDetector.detectChanges(); + if (AnswerPostComponent.activeDropdownPost !== this) { + AnswerPostComponent.cleanupActiveDropdown(); } AnswerPostComponent.activeDropdownPost = this; @@ -133,4 +137,19 @@ export class AnswerPostComponent extends PostingDirective { this.dropdownPosition.x = screenWidth - dropdownWidth - 10; } } + + private static cleanupActiveDropdown(): void { + if (AnswerPostComponent.activeDropdownPost) { + AnswerPostComponent.activeDropdownPost.showDropdown = false; + AnswerPostComponent.activeDropdownPost.enableBodyScroll(); + AnswerPostComponent.activeDropdownPost.changeDetector.detectChanges(); + AnswerPostComponent.activeDropdownPost = null; + } + } + + ngOnDestroy(): void { + if (AnswerPostComponent.activeDropdownPost === this) { + AnswerPostComponent.cleanupActiveDropdown(); + } + } } diff --git a/src/test/javascript/spec/component/shared/metis/answer-post/answer-post.component.spec.ts b/src/test/javascript/spec/component/shared/metis/answer-post/answer-post.component.spec.ts index a3d3692660bf..89600e6f7f8e 100644 --- a/src/test/javascript/spec/component/shared/metis/answer-post/answer-post.component.spec.ts +++ b/src/test/javascript/spec/component/shared/metis/answer-post/answer-post.component.spec.ts @@ -50,6 +50,10 @@ describe('AnswerPostComponent', () => { }); }); + afterEach(() => { + jest.restoreAllMocks(); + }); + it('should contain an answer post header when isConsecutive is false', () => { runInInjectionContext(fixture.debugElement.injector, () => { component.isConsecutive = input(false); @@ -209,13 +213,6 @@ describe('AnswerPostComponent', () => { expect(preventDefaultSpy).toHaveBeenCalledTimes(preventDefaultCalled ? 1 : 0); expect(component.showDropdown).toBe(showDropdown); expect(component.dropdownPosition).toEqual(dropdownPosition); - -afterEach(() => { - jest.restoreAllMocks(); -}); - -testCases.forEach(({ cursor, preventDefaultCalled, showDropdown, dropdownPosition }) => { - // ... test implementation ... -}); + }); }); });