import { SharedLinkViewComponent } from './shared-link-view.component' ; import { TestBed , ComponentFixture , fakeAsync , tick } from '@angular/core/testing' ; import { Store } from '@ngrx/store' ; import { AppTestingModule } from '../../testing/app-testing.module' ; import { ActivatedRoute } from '@angular/router' ; import { of } from 'rxjs' ; import { NO_ERRORS_SCHEMA } from '@angular/core' ; import { SetSelectedNodesAction } from '@alfresco/aca-shared/store' ; import { AppExtensionService } from '@alfresco/aca-shared' ; describe ( 'SharedLinkViewComponent' , ( ) => { let component ; let fixture ; let appExtensionService ; let spyGetSharedLink ; const storeMock = { dispatch : jasmine . createSpy ( 'dispatch' ) , select : ( ) => of ( { } ) } ; beforeEach ( ( ) => { TestBed . configureTestingModule ( { imports : [ AppTestingModule ] , declarations : [ SharedLinkViewComponent ] , providers : [ AppExtensionService , { provide : Store , useValue : storeMock } , { provide : ActivatedRoute , useValue : { snapshot : { data : { preferencePrefix : 'prefix' } } , params : of ( { id : '123' } ) } } ] , schemas : [ NO_ERRORS_SCHEMA ] } ) ; fixture = TestBed . createComponent ( SharedLinkViewComponent ) ; component = fixture . componentInstance ; appExtensionService = TestBed . inject ( AppExtensionService ) ; appExtensionService . selection = { isEmpty : true , count : 0 , libraries : null , nodes : null } ; spyGetSharedLink = spyOn ( component [ 'sharedLinksApi' ] , 'getSharedLink' ) ; storeMock . dispatch . calls . reset ( ) ; } ) ; afterEach ( ( ) => { spyGetSharedLink . calls . reset ( ) ; } ) ; it ( 'should update store selection' , fakeAsync ( ( ) => { spyGetSharedLink . and . returnValue ( Promise . resolve ( { entry : { id : 'shared-id' } } ) ) ; fixture . detectChanges ( ) ; tick ( ) ; expect ( storeMock . dispatch ) . toHaveBeenCalledWith ( new SetSelectedNodesAction ( [ { entry : { id : 'shared-id' } } as any ] ) ) ; } ) ) ; it ( 'should not update store on error' , fakeAsync ( ( ) => { spyGetSharedLink . and . returnValue ( Promise . reject ( 'error' ) ) ; fixture . detectChanges ( ) ; tick ( ) ; expect ( storeMock . dispatch ) . not . toHaveBeenCalled ( ) ; } ) ) ; it ( 'should not update actions reference if selection is empty' , fakeAsync ( ( ) => { spyGetSharedLink . and . returnValue ( Promise . resolve ( { entry : { id : 'shared-id' } } ) ) ; fixture . detectChanges ( ) ; tick ( ) ; expect ( component . viewerToolbarActions ) . toEqual ( [ ] ) ; } ) ) ; it ( 'should update actions reference if selection is not empty' , fakeAsync ( ( ) => { appExtensionService . selection = { isEmpty : false , count : 1 , libraries : null , nodes : null } ; spyOn ( appExtensionService , 'getSharedLinkViewerToolbarActions' ) . and . callThrough ( ) ; spyGetSharedLink . and . returnValue ( Promise . resolve ( { entry : { id : 'shared-id' } } ) ) ; fixture . detectChanges ( ) ; tick ( ) ; expect ( appExtensionService . getSharedLinkViewerToolbarActions ) . toHaveBeenCalled ( ) ; } ) ) ; it ( 'should fetch link id from the active route' , fakeAsync ( ( ) => { spyGetSharedLink . and . returnValue ( Promise . resolve ( { entry : { id : 'shared-id' } } ) ) ; fixture . detectChanges ( ) ; tick ( ) ; expect ( component . sharedLinkId ) . toBe ( '123' ) ; } ) ) ; } ) ;