import { TestBed , fakeAsync , tick } from '@angular/core/testing' ; import { AppTestingModule } from '../../testing/app-testing.module' ; import { NodeEffects } from './node.effects' ; import { EffectsModule } from '@ngrx/effects' ; import { Store } from '@ngrx/store' ; import { ContentManagementService } from '../../services/content-management.service' ; import { SharedStoreModule , ShareNodeAction , SetSelectedNodesAction , UnshareNodesAction , PurgeDeletedNodesAction , RestoreDeletedNodesAction , DeleteNodesAction , UndoDeleteNodesAction , CreateFolderAction , EditFolderAction , CopyNodesAction , MoveNodesAction , UnlockWriteAction , FullscreenViewerAction , PrintFileAction , SetCurrentFolderAction , ManageAspectsAction , ManagePermissionsAction } from '@alfresco/aca-shared/store' ; import { ViewUtilService } from '@alfresco/adf-core' ; import { ViewerEffects } from './viewer.effects' ; import { Router } from '@angular/router' ; import { of } from 'rxjs' ; describe ( 'NodeEffects' , ( ) => { let store ; let contentService ; let viewUtilService ; let viewerEffects ; let router ; beforeEach ( ( ) => { TestBed . configureTestingModule ( { imports : [ AppTestingModule , SharedStoreModule , EffectsModule . forRoot ( [ NodeEffects , ViewerEffects ] ) ] , providers : [ ViewUtilService ] } ) ; store = TestBed . inject ( Store ) ; contentService = TestBed . inject ( ContentManagementService ) ; viewUtilService = TestBed . inject ( ViewUtilService ) ; viewerEffects = TestBed . inject ( ViewerEffects ) ; router = TestBed . inject ( Router ) ; } ) ; describe ( 'shareNode$' , ( ) => { it ( 'should share node from payload' , ( ) => { spyOn ( contentService , 'shareNode' ) . and . stub ( ) ; const node = { } ; store . dispatch ( new ShareNodeAction ( node ) ) ; expect ( contentService . shareNode ) . toHaveBeenCalledWith ( node ) ; } ) ; it ( 'should share node from active selection' , fakeAsync ( ( ) => { spyOn ( contentService , 'shareNode' ) . and . stub ( ) ; const node = { entry : { isFile : true } } ; store . dispatch ( new SetSelectedNodesAction ( [ node ] ) ) ; tick ( 100 ) ; store . dispatch ( new ShareNodeAction ( null ) ) ; expect ( contentService . shareNode ) . toHaveBeenCalledWith ( node ) ; } ) ) ; it ( 'should do nothing if invoking share with no data' , ( ) => { spyOn ( contentService , 'shareNode' ) . and . stub ( ) ; store . dispatch ( new ShareNodeAction ( null ) ) ; expect ( contentService . shareNode ) . not . toHaveBeenCalled ( ) ; } ) ; } ) ; describe ( 'unshareNodes$' , ( ) => { it ( 'should unshare nodes from the payload' , ( ) => { spyOn ( contentService , 'unshareNodes' ) . and . stub ( ) ; const node = { } ; store . dispatch ( new UnshareNodesAction ( [ node ] ) ) ; expect ( contentService . unshareNodes ) . toHaveBeenCalledWith ( [ node ] ) ; } ) ; it ( 'should unshare nodes from the active selection' , fakeAsync ( ( ) => { spyOn ( contentService , 'unshareNodes' ) . and . stub ( ) ; const node = { entry : { isFile : true } } ; store . dispatch ( new SetSelectedNodesAction ( [ node ] ) ) ; tick ( 100 ) ; store . dispatch ( new UnshareNodesAction ( null ) ) ; expect ( contentService . unshareNodes ) . toHaveBeenCalledWith ( [ node ] ) ; } ) ) ; it ( 'should do nothing if invoking unshare with no data' , ( ) => { spyOn ( contentService , 'unshareNodes' ) . and . stub ( ) ; store . dispatch ( new UnshareNodesAction ( null ) ) ; expect ( contentService . unshareNodes ) . not . toHaveBeenCalled ( ) ; } ) ; } ) ; describe ( 'purgeDeletedNodes$' , ( ) => { it ( 'should purge deleted nodes from the payload' , ( ) => { spyOn ( contentService , 'purgeDeletedNodes' ) . and . stub ( ) ; const node = { } ; store . dispatch ( new PurgeDeletedNodesAction ( [ node ] ) ) ; expect ( contentService . purgeDeletedNodes ) . toHaveBeenCalledWith ( [ node ] ) ; } ) ; it ( 'should purge nodes from the active selection' , fakeAsync ( ( ) => { spyOn ( contentService , 'purgeDeletedNodes' ) . and . stub ( ) ; const node = { entry : { isFile : true } } ; store . dispatch ( new SetSelectedNodesAction ( [ node ] ) ) ; tick ( 100 ) ; store . dispatch ( new PurgeDeletedNodesAction ( null ) ) ; expect ( contentService . purgeDeletedNodes ) . toHaveBeenCalledWith ( [ node ] ) ; } ) ) ; it ( 'should do nothing if invoking purge with no data' , ( ) => { spyOn ( contentService , 'purgeDeletedNodes' ) . and . stub ( ) ; store . dispatch ( new PurgeDeletedNodesAction ( null ) ) ; expect ( contentService . purgeDeletedNodes ) . not . toHaveBeenCalled ( ) ; } ) ; } ) ; describe ( 'restoreDeletedNodes$' , ( ) => { it ( 'should restore deleted nodes from the payload' , ( ) => { spyOn ( contentService , 'restoreDeletedNodes' ) . and . stub ( ) ; const node = { } ; store . dispatch ( new RestoreDeletedNodesAction ( [ node ] ) ) ; expect ( contentService . restoreDeletedNodes ) . toHaveBeenCalledWith ( [ node ] ) ; } ) ; it ( 'should restore deleted nodes from the active selection' , fakeAsync ( ( ) => { spyOn ( contentService , 'restoreDeletedNodes' ) . and . stub ( ) ; const node = { entry : { isFile : true } } ; store . dispatch ( new SetSelectedNodesAction ( [ node ] ) ) ; tick ( 100 ) ; store . dispatch ( new RestoreDeletedNodesAction ( null ) ) ; expect ( contentService . restoreDeletedNodes ) . toHaveBeenCalledWith ( [ node ] ) ; } ) ) ; it ( 'should do nothing if invoking restore with no data' , ( ) => { spyOn ( contentService , 'restoreDeletedNodes' ) . and . stub ( ) ; store . dispatch ( new RestoreDeletedNodesAction ( null ) ) ; expect ( contentService . restoreDeletedNodes ) . not . toHaveBeenCalled ( ) ; } ) ; } ) ; describe ( 'deleteNodes$' , ( ) => { it ( 'should delete nodes from the payload' , ( ) => { spyOn ( contentService , 'deleteNodes' ) . and . stub ( ) ; const node = { } ; store . dispatch ( new DeleteNodesAction ( [ node ] ) ) ; expect ( contentService . deleteNodes ) . toHaveBeenCalledWith ( [ node ] ) ; } ) ; it ( 'should delete nodes from the active selection' , fakeAsync ( ( ) => { spyOn ( contentService , 'deleteNodes' ) . and . stub ( ) ; const node = { entry : { isFile : true } } ; store . dispatch ( new SetSelectedNodesAction ( [ node ] ) ) ; tick ( 100 ) ; store . dispatch ( new DeleteNodesAction ( null ) ) ; expect ( contentService . deleteNodes ) . toHaveBeenCalledWith ( [ node ] ) ; } ) ) ; it ( 'should do nothing if invoking delete with no data' , ( ) => { spyOn ( contentService , 'deleteNodes' ) . and . stub ( ) ; store . dispatch ( new DeleteNodesAction ( null ) ) ; expect ( contentService . deleteNodes ) . not . toHaveBeenCalled ( ) ; } ) ; } ) ; describe ( 'undoDeleteNodes$' , ( ) => { it ( 'should undo deleted nodes from the payload' , ( ) => { spyOn ( contentService , 'undoDeleteNodes' ) . and . stub ( ) ; const node = { } ; store . dispatch ( new UndoDeleteNodesAction ( [ node ] ) ) ; expect ( contentService . undoDeleteNodes ) . toHaveBeenCalledWith ( [ node ] ) ; } ) ; it ( 'should do nothing if undoing deletion with no data' , ( ) => { spyOn ( contentService , 'undoDeleteNodes' ) . and . stub ( ) ; store . dispatch ( new UndoDeleteNodesAction ( [ ] ) ) ; expect ( contentService . undoDeleteNodes ) . not . toHaveBeenCalled ( ) ; } ) ; } ) ; describe ( 'createFolder$' , ( ) => { beforeEach ( ( ) => { spyOn ( contentService , 'createFolder' ) . and . stub ( ) ; } ) ; it ( 'should create folder from the payload' , ( ) => { const currentFolder = 'folder1' ; store . dispatch ( new CreateFolderAction ( currentFolder ) ) ; expect ( contentService . createFolder ) . toHaveBeenCalledWith ( currentFolder ) ; } ) ; it ( 'should create folder in the active selected one' , fakeAsync ( ( ) => { const currentFolder = { isFolder : true , id : 'folder1' } ; store . dispatch ( new SetCurrentFolderAction ( currentFolder ) ) ; tick ( 100 ) ; store . dispatch ( new CreateFolderAction ( null ) ) ; expect ( contentService . createFolder ) . toHaveBeenCalledWith ( currentFolder . id ) ; } ) ) ; } ) ; describe ( 'editFolder$' , ( ) => { it ( 'should edit folder from the payload' , ( ) => { spyOn ( contentService , 'editFolder' ) . and . stub ( ) ; const node = { entry : { isFolder : true , id : 'folder1' } } ; store . dispatch ( new EditFolderAction ( node ) ) ; expect ( contentService . editFolder ) . toHaveBeenCalledWith ( node ) ; } ) ; it ( 'should edit folder from the active selection' , fakeAsync ( ( ) => { spyOn ( contentService , 'editFolder' ) . and . stub ( ) ; const currentFolder = { entry : { isFolder : true , isFile : false , id : 'folder1' } } ; store . dispatch ( new SetSelectedNodesAction ( [ currentFolder ] ) ) ; tick ( 100 ) ; store . dispatch ( new EditFolderAction ( null ) ) ; expect ( contentService . editFolder ) . toHaveBeenCalledWith ( currentFolder ) ; } ) ) ; it ( 'should do nothing if editing folder with no selection and payload' , ( ) => { spyOn ( contentService , 'editFolder' ) . and . stub ( ) ; store . dispatch ( new EditFolderAction ( null ) ) ; expect ( contentService . editFolder ) . not . toHaveBeenCalled ( ) ; } ) ; } ) ; describe ( 'copyNodes$' , ( ) => { it ( 'should copy nodes from the payload' , ( ) => { spyOn ( contentService , 'copyNodes' ) . and . stub ( ) ; const node = { entry : { isFile : true } } ; store . dispatch ( new CopyNodesAction ( [ node ] ) ) ; expect ( contentService . copyNodes ) . toHaveBeenCalledWith ( [ node ] ) ; } ) ; it ( 'should copy nodes from the active selection' , fakeAsync ( ( ) => { spyOn ( contentService , 'copyNodes' ) . and . stub ( ) ; const node = { entry : { isFile : true } } ; store . dispatch ( new SetSelectedNodesAction ( [ node ] ) ) ; tick ( 100 ) ; store . dispatch ( new CopyNodesAction ( null ) ) ; expect ( contentService . copyNodes ) . toHaveBeenCalledWith ( [ node ] ) ; } ) ) ; it ( 'should do nothing if invoking copy with no data' , ( ) => { spyOn ( contentService , 'copyNodes' ) . and . stub ( ) ; store . dispatch ( new CopyNodesAction ( null ) ) ; expect ( contentService . copyNodes ) . not . toHaveBeenCalled ( ) ; } ) ; } ) ; describe ( 'moveNodes$' , ( ) => { it ( 'should move nodes from the payload' , ( ) => { spyOn ( contentService , 'moveNodes' ) . and . stub ( ) ; const node = { entry : { isFile : true } } ; store . dispatch ( new MoveNodesAction ( [ node ] ) ) ; expect ( contentService . moveNodes ) . toHaveBeenCalledWith ( [ node ] ) ; } ) ; it ( 'should move nodes from the active selection' , fakeAsync ( ( ) => { spyOn ( contentService , 'moveNodes' ) . and . stub ( ) ; const node = { entry : { isFile : true } } ; store . dispatch ( new SetSelectedNodesAction ( [ node ] ) ) ; tick ( 100 ) ; store . dispatch ( new MoveNodesAction ( null ) ) ; expect ( contentService . moveNodes ) . toHaveBeenCalledWith ( [ node ] ) ; } ) ) ; it ( 'should do nothing if invoking move with no data' , ( ) => { spyOn ( contentService , 'moveNodes' ) . and . stub ( ) ; store . dispatch ( new MoveNodesAction ( null ) ) ; expect ( contentService . moveNodes ) . not . toHaveBeenCalled ( ) ; } ) ; } ) ; describe ( 'managePermissions$' , ( ) => { it ( 'should manage permissions from the payload' , ( ) => { spyOn ( router , 'navigate' ) . and . stub ( ) ; const node = { entry : { isFile : true , id : 'fileId' } } ; store . dispatch ( new ManagePermissionsAction ( node ) ) ; expect ( router . navigate ) . toHaveBeenCalledWith ( [ 'personal-files/details' , 'fileId' , 'permissions' ] ) ; } ) ; it ( 'should manage permissions from the active selection' , ( ) => { spyOn ( store , 'select' ) . and . returnValue ( of ( { isEmpty : false , first : { entry : { id : 'fileId' } } } ) ) ; spyOn ( router , 'navigate' ) . and . stub ( ) ; store . dispatch ( new ManagePermissionsAction ( null ) ) ; expect ( router . navigate ) . toHaveBeenCalledWith ( [ 'personal-files/details' , 'fileId' , 'permissions' ] ) ; } ) ; it ( 'should do nothing if invoking manage permissions with no data' , ( ) => { spyOn ( store , 'select' ) . and . returnValue ( of ( null ) ) ; spyOn ( router , 'navigate' ) . and . stub ( ) ; store . dispatch ( new ManagePermissionsAction ( null ) ) ; expect ( router . navigate ) . not . toHaveBeenCalled ( ) ; } ) ; } ) ; describe ( 'printFile$' , ( ) => { it ( 'it should print node content from payload' , ( ) => { spyOn ( viewUtilService , 'printFileGeneric' ) . and . stub ( ) ; const node = { entry : { id : 'node-id' , content : { mimeType : 'text/json' } } } ; store . dispatch ( new PrintFileAction ( node ) ) ; expect ( viewUtilService . printFileGeneric ) . toHaveBeenCalledWith ( 'node-id' , 'text/json' ) ; } ) ; it ( 'it should print node content from store' , fakeAsync ( ( ) => { spyOn ( viewUtilService , 'printFileGeneric' ) . and . stub ( ) ; const node = { entry : { isFile : true , id : 'node-id' , content : { mimeType : 'text/json' } } } ; store . dispatch ( new SetSelectedNodesAction ( [ node ] ) ) ; tick ( 100 ) ; store . dispatch ( new PrintFileAction ( null ) ) ; expect ( viewUtilService . printFileGeneric ) . toHaveBeenCalledWith ( 'node-id' , 'text/json' ) ; } ) ) ; } ) ; describe ( 'fullscreenViewer$' , ( ) => { it ( 'should call fullscreen viewer' , ( ) => { spyOn ( viewerEffects , 'enterFullScreen' ) . and . stub ( ) ; store . dispatch ( new FullscreenViewerAction ( null ) ) ; expect ( viewerEffects . enterFullScreen ) . toHaveBeenCalled ( ) ; } ) ; } ) ; describe ( 'unlockWrite$' , ( ) => { it ( 'should unlock node from payload' , ( ) => { spyOn ( contentService , 'unlockNode' ) . and . stub ( ) ; const node = { entry : { id : 'node-id' } } ; store . dispatch ( new UnlockWriteAction ( node ) ) ; expect ( contentService . unlockNode ) . toHaveBeenCalledWith ( node ) ; } ) ; it ( 'should unlock node from store selection' , fakeAsync ( ( ) => { spyOn ( contentService , 'unlockNode' ) . and . stub ( ) ; const node = { entry : { isFile : true , id : 'node-id' } } ; store . dispatch ( new SetSelectedNodesAction ( [ node ] ) ) ; tick ( 100 ) ; store . dispatch ( new UnlockWriteAction ( null ) ) ; expect ( contentService . unlockNode ) . toHaveBeenCalledWith ( node ) ; } ) ) ; } ) ; describe ( 'aspectList$' , ( ) => { it ( 'should call aspect dialog' , ( ) => { const node = { entry : { isFile : true } } ; spyOn ( contentService , 'manageAspects' ) . and . stub ( ) ; store . dispatch ( new ManageAspectsAction ( node ) ) ; expect ( contentService . manageAspects ) . toHaveBeenCalled ( ) ; } ) ; it ( 'should call aspect dialog from the active file selection' , fakeAsync ( ( ) => { spyOn ( contentService , 'manageAspects' ) . and . stub ( ) ; const node = { entry : { isFile : true , id : 'file-node-id' } } ; store . dispatch ( new SetSelectedNodesAction ( [ node ] ) ) ; tick ( 100 ) ; store . dispatch ( new ManageAspectsAction ( null ) ) ; expect ( contentService . manageAspects ) . toHaveBeenCalledWith ( { entry : { isFile : true , id : 'file-node-id' } } ) ; } ) ) ; it ( 'should call aspect dialog from the active folder selection' , fakeAsync ( ( ) => { spyOn ( contentService , 'manageAspects' ) . and . stub ( ) ; const node = { entry : { isFile : false , id : 'folder-node-id' } } ; store . dispatch ( new SetSelectedNodesAction ( [ node ] ) ) ; tick ( 100 ) ; store . dispatch ( new ManageAspectsAction ( null ) ) ; expect ( contentService . manageAspects ) . toHaveBeenCalledWith ( { entry : { isFile : false , id : 'folder-node-id' } } ) ; } ) ) ; } ) ; } ) ;