import { TestBed , ComponentFixture } from '@angular/core/testing' ; import { AppTestingModule } from '../../testing/app-testing.module' ; import { ContextMenuItemComponent } from './context-menu-item.component' ; import { ContextMenuModule } from './context-menu.module' ; import { TranslateModule , TranslateLoader , TranslateFakeLoader } from '@ngx-translate/core' ; import { AppExtensionService } from '@alfresco/aca-shared' ; describe ( 'ContextMenuComponent' , ( ) => { let fixture ; let component ; let extensionsService ; let contextItem ; beforeEach ( ( ) => { TestBed . configureTestingModule ( { imports : [ AppTestingModule , ContextMenuModule , TranslateModule . forRoot ( { loader : { provide : TranslateLoader , useClass : TranslateFakeLoader } } ) ] , providers : [ AppExtensionService ] } ) ; fixture = TestBed . createComponent ( ContextMenuItemComponent ) ; component = fixture . componentInstance ; extensionsService = TestBed . inject ( AppExtensionService ) ; contextItem = { type : 'button' , id : 'action-button' , title : 'Test Button' , actions : { click : 'TEST_EVENT' } } ; } ) ; afterEach ( ( ) => { fixture . destroy ( ) ; } ) ; it ( 'should render defined menu actions items' , ( ) => { component . actionRef = contextItem ; fixture . detectChanges ( ) ; const buttonElement = fixture . nativeElement . querySelector ( 'button' ) ; expect ( buttonElement . querySelector ( 'span' ) . innerText . trim ( ) ) . toBe ( contextItem . title ) ; } ) ; it ( 'should not run action when entry has no click attribute defined' , ( ) => { spyOn ( extensionsService , 'runActionById' ) ; contextItem . actions = { } ; component . actionRef = contextItem ; fixture . detectChanges ( ) ; fixture . nativeElement . querySelector ( '#action-button' ) . dispatchEvent ( new MouseEvent ( 'click' ) ) ; expect ( extensionsService . runActionById ) . not . toHaveBeenCalled ( ) ; } ) ; it ( 'should run action with provided action id' , ( ) => { spyOn ( extensionsService , 'runActionById' ) ; component . actionRef = contextItem ; fixture . detectChanges ( ) ; fixture . nativeElement . querySelector ( '#action-button' ) . dispatchEvent ( new MouseEvent ( 'click' ) ) ; expect ( extensionsService . runActionById ) . toHaveBeenCalledWith ( contextItem . actions . click ) ; } ) ; } ) ;