import { SearchQueryBuilderService } from '@alfresco/adf-content-services' ; import { SearchSortingDefinition } from '@alfresco/adf-content-services/lib/search/models/search-sorting-definition.interface' ; import { ComponentFixture , TestBed } from '@angular/core/testing' ; import { AppTestingModule } from '../../../testing/app-testing.module' ; import { SearchActionMenuComponent } from './search-action-menu.component' ; const mockSortingData = [ { ascending : false , field : 'fieldA' , key : 'keyA' , label : 'LabelA' , type : 'A' } , { ascending : true , field : 'fieldB' , key : 'keyB' , label : 'Zorro' , type : 'Z' } ] ; describe ( 'SearchActionMenuComponent' , ( ) => { let fixture ; let component ; let queryService ; beforeEach ( ( ) => { TestBed . configureTestingModule ( { imports : [ AppTestingModule ] , declarations : [ SearchActionMenuComponent ] , providers : [ SearchQueryBuilderService ] } ) ; fixture = TestBed . createComponent ( SearchActionMenuComponent ) ; queryService = TestBed . inject ( SearchQueryBuilderService ) ; component = fixture . componentInstance ; } ) ; it ( 'should emit sortingSelected event when asc sorting option is selected' , async ( ) => { spyOn ( queryService , 'getSortingOptions' ) . and . returnValue ( mockSortingData ) ; const expectedOption = { ascending : true , field : 'fieldA' , key : 'keyA' , label : 'LabelA' , type : 'A' } ; spyOn ( component . sortingSelected , 'emit' ) . and . callThrough ( ) ; fixture . detectChanges ( ) ; await fixture . whenStable ( ) ; const actionMenuButton = fixture . nativeElement . querySelector ( '#aca-button-action-menu' ) ; actionMenuButton . dispatchEvent ( new Event ( 'click' ) ) ; await fixture . whenStable ( ) ; fixture . detectChanges ( ) ; const sortingMenuButton = document . querySelector ( '#aca-button-sorting-menu' ) ; sortingMenuButton . dispatchEvent ( new Event ( 'click' ) ) ; await fixture . whenStable ( ) ; fixture . detectChanges ( ) ; const fieldAMenuButton = document . querySelector ( '#keyA-sorting-option' ) ; fieldAMenuButton . dispatchEvent ( new Event ( 'click' ) ) ; await fixture . whenStable ( ) ; fixture . detectChanges ( ) ; const directionButton = document . querySelector ( '#keyA-sorting-option-asc' ) ; directionButton . dispatchEvent ( new Event ( 'click' ) ) ; await fixture . whenStable ( ) ; fixture . detectChanges ( ) ; expect ( component . sortingSelected . emit ) . toHaveBeenCalledWith ( expectedOption ) ; } ) ; it ( 'should emit sortingSelected event when desc sorting option is selected' , async ( ) => { spyOn ( queryService , 'getSortingOptions' ) . and . returnValue ( mockSortingData ) ; const expectedOption = { ascending : false , field : 'fieldB' , key : 'keyB' , label : 'Zorro' , type : 'Z' } ; spyOn ( component . sortingSelected , 'emit' ) . and . callThrough ( ) ; fixture . detectChanges ( ) ; await fixture . whenStable ( ) ; const actionMenuButton = fixture . nativeElement . querySelector ( '#aca-button-action-menu' ) ; actionMenuButton . dispatchEvent ( new Event ( 'click' ) ) ; await fixture . whenStable ( ) ; fixture . detectChanges ( ) ; const sortingMenuButton = document . querySelector ( '#aca-button-sorting-menu' ) ; sortingMenuButton . dispatchEvent ( new Event ( 'click' ) ) ; await fixture . whenStable ( ) ; fixture . detectChanges ( ) ; const fieldAMenuButton = document . querySelector ( '#keyB-sorting-option' ) ; fieldAMenuButton . dispatchEvent ( new Event ( 'click' ) ) ; await fixture . whenStable ( ) ; fixture . detectChanges ( ) ; const directionButton = document . querySelector ( '#keyB-sorting-option-desc' ) ; directionButton . dispatchEvent ( new Event ( 'click' ) ) ; await fixture . whenStable ( ) ; fixture . detectChanges ( ) ; expect ( component . sortingSelected . emit ) . toHaveBeenCalledWith ( expectedOption ) ; } ) ; } ) ;