import { ComponentFixture , TestBed } from '@angular/core/testing' ; import { CoreModule } from '@alfresco/adf-core' ; import { ToggleFavoriteLibraryComponent } from './toggle-favorite-library.component' ; import { NO_ERRORS_SCHEMA } from '@angular/core' ; import { Store } from '@ngrx/store' ; import { AppTestingModule } from '../../../testing/app-testing.module' ; import { of } from 'rxjs' ; import { Router } from '@angular/router' ; import { TranslateModule } from '@ngx-translate/core' ; import { AppHookService , ContentApiService } from '@alfresco/aca-shared' ; describe ( 'ToggleFavoriteLibraryComponent' , ( ) => { let fixture ; let component ; let appHookService ; let contentApiService ; const selection = { library : { entry : { id : 'libraryId' } } } ; const mockRouter = { url : '' } ; beforeEach ( ( ) => { TestBed . configureTestingModule ( { imports : [ TranslateModule . forRoot ( ) , CoreModule . forRoot ( ) , AppTestingModule ] , declarations : [ ToggleFavoriteLibraryComponent ] , providers : [ { provide : Router , useValue : mockRouter } , { provide : Store , useValue : { dispatch : ( ) => { } , select : ( ) => of ( selection ) } } ] , schemas : [ NO_ERRORS_SCHEMA ] } ) ; } ) ; beforeEach ( ( ) => { fixture = TestBed . createComponent ( ToggleFavoriteLibraryComponent ) ; component = fixture . componentInstance ; contentApiService = TestBed . inject ( ContentApiService ) ; appHookService = TestBed . inject ( AppHookService ) ; spyOn ( contentApiService [ 'favoritesApi' ] , 'getFavoriteSite' ) . and . returnValue ( Promise . resolve ( null ) ) ; } ) ; it ( 'should get library selection from Store' , async ( ) => { fixture . detectChanges ( ) ; await fixture . whenStable ( ) ; expect ( component . library . entry . id ) . toEqual ( selection . library . entry . id ) ; } ) ; it ( 'should mark selection as favorite when on favorite libraries route' , async ( ) => { mockRouter . url = '/favorite/libraries' ; fixture . detectChanges ( ) ; await fixture . whenStable ( ) ; expect ( component . library . isFavorite ) . toBe ( true ) ; } ) ; it ( 'should emit onToggleEvent() event' , async ( ) => { spyOn ( appHookService . favoriteLibraryToggle , 'next' ) ; fixture . detectChanges ( ) ; await fixture . whenStable ( ) ; component . onToggleEvent ( ) ; expect ( appHookService . favoriteLibraryToggle . next ) . toHaveBeenCalled ( ) ; } ) ; } ) ;