import { ToggleEditOfflineComponent } from './toggle-edit-offline.component' ; import { CoreModule } from '@alfresco/adf-core' ; import { ComponentFixture , TestBed } from '@angular/core/testing' ; import { of } from 'rxjs' ; import { Store } from '@ngrx/store' ; import { NodeEntry } from '@alfresco/js-api' ; import { DownloadNodesAction , EditOfflineAction , SnackbarErrorAction } from '@alfresco/aca-shared/store' ; import { TranslateModule } from '@ngx-translate/core' ; describe ( 'ToggleEditOfflineComponent' , ( ) => { let fixture ; let component ; let store ; let dispatchSpy ; let selectSpy ; let selection ; beforeEach ( ( ) => { TestBed . configureTestingModule ( { imports : [ TranslateModule . forRoot ( ) , CoreModule . forRoot ( ) ] , declarations : [ ToggleEditOfflineComponent ] , providers : [ { provide : Store , useValue : { select : ( ) => { } , dispatch : ( ) => { } } } ] } ) ; fixture = TestBed . createComponent ( ToggleEditOfflineComponent ) ; component = fixture . componentInstance ; spyOn ( component , 'unlockNode' ) . and . returnValue ( Promise . resolve ( null ) ) ; spyOn ( component , 'lockNode' ) . and . returnValue ( Promise . resolve ( null ) ) ; store = TestBed . inject ( Store ) ; dispatchSpy = spyOn ( store , 'dispatch' ) ; selectSpy = spyOn ( store , 'select' ) ; selection = { file : { entry : { name : 'test' , properties : { } , isLocked : false } } } ; } ) ; it ( 'should initialized with data from store' , ( ) => { selectSpy . and . returnValue ( of ( selection ) ) ; fixture . detectChanges ( ) ; expect ( component . selection ) . toEqual ( selection . file as any ) ; } ) ; it ( 'should download content when node is locked' , async ( ) => { selectSpy . and . returnValue ( of ( selection ) ) ; fixture . detectChanges ( ) ; selection . file . entry . isLocked = false ; await component . onClick ( ) ; fixture . detectChanges ( ) ; expect ( dispatchSpy . calls . argsFor ( 0 ) ) . toEqual ( [ new DownloadNodesAction ( [ selection . file as NodeEntry ] ) ] ) ; } ) ; it ( 'should not download content if node is not locked' , ( ) => { selectSpy . and . returnValue ( of ( selection ) ) ; fixture . detectChanges ( ) ; component . onClick ( ) ; fixture . detectChanges ( ) ; expect ( dispatchSpy . calls . argsFor ( 0 ) ) . not . toEqual ( [ new DownloadNodesAction ( [ selection . file as NodeEntry ] ) ] ) ; } ) ; it ( 'should dispatch EditOfflineAction action' , async ( ) => { selectSpy . and . returnValue ( of ( selection ) ) ; selection . file . entry . isLocked = true ; fixture . detectChanges ( ) ; await component . onClick ( ) ; fixture . detectChanges ( ) ; expect ( dispatchSpy . calls . argsFor ( 0 ) ) . toEqual ( [ new EditOfflineAction ( selection . file as NodeEntry ) ] ) ; } ) ; it ( 'should raise notification on lock error' , ( ) => { selectSpy . and . returnValue ( of ( selection ) ) ; fixture . detectChanges ( ) ; component . onLockError ( ) ; fixture . detectChanges ( ) ; expect ( dispatchSpy . calls . argsFor ( 0 ) ) . toEqual ( [ new SnackbarErrorAction ( 'APP.MESSAGES.ERRORS.LOCK_NODE' , { fileName : 'test' } ) ] ) ; } ) ; it ( 'should raise notification on unlock error' , ( ) => { selectSpy . and . returnValue ( of ( selection ) ) ; fixture . detectChanges ( ) ; component . onUnlockError ( ) ; fixture . detectChanges ( ) ; expect ( dispatchSpy . calls . argsFor ( 0 ) ) . toEqual ( [ new SnackbarErrorAction ( 'APP.MESSAGES.ERRORS.UNLOCK_NODE' , { fileName : 'test' } ) ] ) ; } ) ; } ) ;