Skip to content

Commit

Permalink
ACS-8706 improve unit testing approach, remove unnecessary class attr…
Browse files Browse the repository at this point in the history
…ibutes
  • Loading branch information
g-jaskowski committed Nov 4, 2024
1 parent 9bba7ad commit f92ef17
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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: '<div acaBaseContextMenu acaContextMenuOutsideEvent (clickOutside)="onClickOutsideEvent()"></div>',
template: '<div acaContextMenuOutsideEvent (clickOutside)="onClickOutsideEvent()"></div>',
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<TestComponent>;
let element: DebugElement;
let component: TestComponent;

const contextItem = {
type: ContentActionType.button,
Expand All @@ -59,51 +57,52 @@ 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();
});

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'
});
});

it('should return action id on trackByActionId', () => {
const actionId = baseContextMenuDirective.trackByActionId(0, contextItem);
const actionId = component.trackByActionId(0, contextItem);
expect(actionId).toBe(contextItem.id);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> = new Subject<boolean>();
actions: Array<ContentActionRef> = [];
Expand Down

0 comments on commit f92ef17

Please sign in to comment.