From f92ef17d833ff8d5fe4005c4a8374e804455e7f4 Mon Sep 17 00:00:00 2001 From: g-jaskowski Date: Mon, 4 Nov 2024 09:56:21 +0100 Subject: [PATCH] ACS-8706 improve unit testing approach, remove unnecessary class attributes --- .../base-context-menu.directive.spec.ts | 31 +++++++++---------- .../base-context-menu.directive.ts | 5 +-- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/projects/aca-content/src/lib/components/context-menu/base-context-menu.directive.spec.ts b/projects/aca-content/src/lib/components/context-menu/base-context-menu.directive.spec.ts index 242d04f3a2..16711b9d2c 100644 --- a/projects/aca-content/src/lib/components/context-menu/base-context-menu.directive.spec.ts +++ b/projects/aca-content/src/lib/components/context-menu/base-context-menu.directive.spec.ts @@ -27,26 +27,24 @@ import { ContentActionType } from '@alfresco/adf-extensions'; import { AppExtensionService } from '@alfresco/aca-shared'; import { BaseContextMenuDirective } from './base-context-menu.directive'; import { ComponentFixture, TestBed } from '@angular/core/testing'; -import { Component, DebugElement } from '@angular/core'; -import { By } from '@angular/platform-browser'; +import { Component } from '@angular/core'; import { AppTestingModule } from '../../testing/app-testing.module'; import { OutsideEventDirective } from './context-menu-outside-event.directive'; +import { By } from '@angular/platform-browser'; @Component({ selector: 'app-test-component', - template: '
', + template: '
', standalone: true, - imports: [BaseContextMenuDirective, OutsideEventDirective] + imports: [OutsideEventDirective] }) class TestComponent extends BaseContextMenuDirective {} describe('BaseContextMenuComponent', () => { let contextMenuOverlayRef: ContextMenuOverlayRef; let extensionsService: AppExtensionService; - let baseContextMenuDirective: BaseContextMenuDirective; - let clickOutsideEventDirective: OutsideEventDirective; let fixture: ComponentFixture; - let element: DebugElement; + let component: TestComponent; const contextItem = { type: ContentActionType.button, @@ -59,35 +57,36 @@ describe('BaseContextMenuComponent', () => { beforeEach(() => { void TestBed.configureTestingModule({ - imports: [AppTestingModule, TestComponent, BaseContextMenuDirective, OutsideEventDirective], + imports: [AppTestingModule, TestComponent], providers: [ { provide: ContextMenuOverlayRef, useValue: { close: jasmine.createSpy('close') } - } + }, + BaseContextMenuDirective, + OutsideEventDirective ] }).compileComponents(); fixture = TestBed.createComponent(TestComponent); - element = fixture.debugElement.query(By.directive(BaseContextMenuDirective)); + component = fixture.componentInstance; contextMenuOverlayRef = TestBed.inject(ContextMenuOverlayRef); extensionsService = TestBed.inject(AppExtensionService); - baseContextMenuDirective = element.injector.get(BaseContextMenuDirective); - clickOutsideEventDirective = element.injector.get(OutsideEventDirective); fixture.detectChanges(); }); it('should close context menu on Escape event', () => { - element.nativeElement.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape' })); + fixture.nativeElement.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape' })); expect(contextMenuOverlayRef.close).toHaveBeenCalled(); }); it('should close context menu on click outside event', () => { - clickOutsideEventDirective.clickOutside.emit(); + fixture.debugElement.query(By.directive(OutsideEventDirective)).injector.get(OutsideEventDirective).clickOutside.emit(); + fixture.detectChanges(); expect(contextMenuOverlayRef.close).toHaveBeenCalled(); }); @@ -95,7 +94,7 @@ describe('BaseContextMenuComponent', () => { it('should run action with provided action id and correct payload', () => { spyOn(extensionsService, 'runActionById'); - baseContextMenuDirective.runAction(contextItem); + component.runAction(contextItem); expect(extensionsService.runActionById).toHaveBeenCalledWith(contextItem.actions.click, { focusedElementOnCloseSelector: '.adf-context-menu-source' @@ -103,7 +102,7 @@ describe('BaseContextMenuComponent', () => { }); it('should return action id on trackByActionId', () => { - const actionId = baseContextMenuDirective.trackByActionId(0, contextItem); + const actionId = component.trackByActionId(0, contextItem); expect(actionId).toBe(contextItem.id); }); }); diff --git a/projects/aca-content/src/lib/components/context-menu/base-context-menu.directive.ts b/projects/aca-content/src/lib/components/context-menu/base-context-menu.directive.ts index c643514582..133b5dbbfa 100644 --- a/projects/aca-content/src/lib/components/context-menu/base-context-menu.directive.ts +++ b/projects/aca-content/src/lib/components/context-menu/base-context-menu.directive.ts @@ -31,10 +31,7 @@ import { CONTEXT_MENU_DIRECTION } from './direction.token'; import { Direction } from '@angular/cdk/bidi'; import { AppExtensionService } from '@alfresco/aca-shared'; -@Directive({ - selector: '[acaBaseContextMenu]', - standalone: true -}) +@Directive() export class BaseContextMenuDirective implements OnDestroy { protected onDestroy$: Subject = new Subject(); actions: Array = [];