import { ComponentFixture , TestBed } from '@angular/core/testing' ; import { MainActionComponent } from './main-action.component' ; import { TranslationService , TranslationMock } from '@alfresco/adf-core' ; import { AppExtensionService } from '@alfresco/aca-shared' ; import { of } from 'rxjs' ; import { ACTION_CLICK , ACTION_TITLE , getContentActionRef } from '../../testing/content-action-ref' ; import { AppExtensionServiceMock } from '../../testing/app-extension-service-mock' ; import { CommonModule } from '@angular/common' ; import { MatButtonModule } from '@angular/material/button' ; import { TranslateModule } from '@ngx-translate/core' ; describe ( 'MainActionComponent' , ( ) => { let mainActionComponent ; const buttonQuery = '[data-automation-id="app-main-action-button"]' ; const iconQuery = '[data-automation-id="app-main-action-icon"]' ; let fixture ; let appExtensionService ; beforeEach ( async ( ) => { TestBed . configureTestingModule ( { imports : [ CommonModule , MatButtonModule , TranslateModule . forRoot ( ) ] , providers : [ { provide : TranslationService , useClass : TranslationMock } , { provide : AppExtensionService , useClass : AppExtensionServiceMock } ] } ) . compileComponents ( ) ; appExtensionService = TestBed . inject ( AppExtensionService ) ; fixture = TestBed . createComponent ( MainActionComponent ) ; mainActionComponent = fixture . componentInstance ; } ) ; describe ( 'component is in expanded mode' , ( ) => { beforeEach ( async ( ) => { mainActionComponent . expanded = true ; fixture . detectChanges ( ) ; } ) ; it ( 'should display button if main action is configured' , ( ) => { const buttonMainAction = fixture . debugElement . nativeElement . querySelector ( buttonQuery ) ; const iconMainAction = fixture . debugElement . nativeElement . querySelector ( iconQuery ) ; expect ( iconMainAction ) . toBeFalsy ( ) ; expect ( buttonMainAction . textContent . trim ( ) ) . toBe ( ACTION_TITLE ) ; } ) ; it ( 'should not display button if main action is not configured' , ( ) => { spyOn ( appExtensionService , 'getMainAction' ) . and . returnValue ( of ( undefined ) ) ; mainActionComponent . ngOnInit ( ) ; fixture . detectChanges ( ) ; const button = fixture . debugElement . nativeElement . querySelector ( buttonQuery ) ; expect ( button ) . toBeFalsy ( ) ; } ) ; it ( 'should call extension action' , ( ) => { const runExtensionActionSpy = spyOn ( appExtensionService , 'runActionById' ) ; const button = fixture . debugElement . nativeElement . querySelector ( buttonQuery ) ; button . click ( ) ; expect ( runExtensionActionSpy ) . toHaveBeenCalledWith ( ACTION_CLICK ) ; } ) ; it ( 'should not call button if main action is disabled' , ( ) => { const disabledMainActionRef = getContentActionRef ( ) ; disabledMainActionRef . disabled = true ; spyOn ( appExtensionService , 'getMainAction' ) . and . returnValue ( of ( disabledMainActionRef ) ) ; const runAction = spyOn ( mainActionComponent , 'runAction' ) ; mainActionComponent . ngOnInit ( ) ; fixture . detectChanges ( ) ; const button = fixture . debugElement . nativeElement . querySelector ( buttonQuery ) ; button . click ( ) ; expect ( runAction ) . not . toHaveBeenCalled ( ) ; } ) ; } ) ; describe ( 'component is displayed as icon' , ( ) => { beforeEach ( async ( ) => { mainActionComponent . expanded = false ; fixture . detectChanges ( ) ; } ) ; it ( 'should display icon if main action is configured' , ( ) => { const buttonMainAction = fixture . debugElement . nativeElement . querySelector ( buttonQuery ) ; const iconMainAction = fixture . debugElement . nativeElement . querySelector ( iconQuery ) ; expect ( buttonMainAction ) . toBeFalsy ( ) ; expect ( iconMainAction ) . toBeTruthy ( ) ; } ) ; it ( 'should not display icon if main action is not configured' , ( ) => { spyOn ( appExtensionService , 'getMainAction' ) . and . returnValue ( of ( undefined ) ) ; mainActionComponent . ngOnInit ( ) ; fixture . detectChanges ( ) ; const mainAction = fixture . debugElement . nativeElement . querySelector ( iconQuery ) ; expect ( mainAction ) . toBeFalsy ( ) ; } ) ; it ( 'should call extension action' , ( ) => { const runExtensionActionSpy = spyOn ( appExtensionService , 'runActionById' ) ; const mainAction = fixture . debugElement . nativeElement . querySelector ( iconQuery ) ; mainAction . click ( ) ; expect ( runExtensionActionSpy ) . toHaveBeenCalledWith ( ACTION_CLICK ) ; } ) ; it ( 'should not call icon if main action is disabled' , ( ) => { const disabledMainActionRef = getContentActionRef ( ) ; disabledMainActionRef . disabled = true ; spyOn ( appExtensionService , 'getMainAction' ) . and . returnValue ( of ( disabledMainActionRef ) ) ; const runAction = spyOn ( mainActionComponent , 'runAction' ) ; mainActionComponent . ngOnInit ( ) ; fixture . detectChanges ( ) ; const button = fixture . debugElement . nativeElement . querySelector ( iconQuery ) ; button . click ( ) ; expect ( runAction ) . not . toHaveBeenCalled ( ) ; } ) ; } ) ; } ) ;