import * as app from './app.rules' ; import { TestRuleContext } from './test-rule-context' ; import { NodeEntry } from '@alfresco/js-api' ; describe ( 'app.evaluators' , ( ) => { describe ( 'canDownloadSelection' , ( ) => { it ( 'should return [false] if selection is empty' , ( ) => { const context = new TestRuleContext ( ) ; expect ( context . selection . isEmpty ) . toBe ( true ) ; expect ( app . canDownloadSelection ( context ) ) . toBe ( false ) ; } ) ; it ( 'should return [false] for the trashcan entries' , ( ) => { const context = new TestRuleContext ( ) ; context . selection . isEmpty = false ; context . navigation = { url : '/trashcan' } ; expect ( app . canDownloadSelection ( context ) ) . toBe ( false ) ; } ) ; it ( 'should allow downloading files' , ( ) => { const context = new TestRuleContext ( ) ; context . selection . isEmpty = false ; context . selection . nodes = [ { entry : { isFile : true } } as NodeEntry ] ; expect ( app . canDownloadSelection ( context ) ) . toBe ( true ) ; } ) ; it ( 'should allow downloading folders' , ( ) => { const context = new TestRuleContext ( ) ; context . selection . isEmpty = false ; context . selection . nodes = [ { entry : { isFolder : true } } as NodeEntry ] ; expect ( app . canDownloadSelection ( context ) ) . toBe ( true ) ; } ) ; it ( 'should now allow downloading unknown selection' , ( ) => { const context = new TestRuleContext ( ) ; context . selection . isEmpty = false ; context . selection . nodes = [ { entry : { } } as NodeEntry ] ; expect ( app . canDownloadSelection ( context ) ) . toBe ( false ) ; } ) ; } ) ; describe ( 'isWriteLocked' , ( ) => { it ( 'should return [true] if lock type is set' , ( ) => { const context = { selection : { file : { entry : { properties : { 'cm:lockType' : 'WRITE_LOCK' } } } } } ; expect ( app . isWriteLocked ( context ) ) . toBe ( true ) ; } ) ; it ( 'should return [false] if lock type is not set' , ( ) => { const context = { selection : { file : { entry : { properties : { } } } } } ; expect ( app . isWriteLocked ( context ) ) . toBe ( false ) ; } ) ; it ( 'should return [false] if selection not present' , ( ) => { const context = { } ; expect ( app . isWriteLocked ( context ) ) . toBe ( false ) ; } ) ; } ) ; describe ( 'hasLockedFiles' , ( ) => { it ( 'should return [false] if selection not present' , ( ) => { const context = { } ; expect ( app . hasLockedFiles ( context ) ) . toBe ( false ) ; } ) ; it ( 'should return [false] if nodes not present' , ( ) => { const context = { selection : { nodes : null } } ; expect ( app . hasLockedFiles ( context ) ) . toBe ( false ) ; } ) ; it ( 'should return [false] if no files selected' , ( ) => { const context = { selection : { nodes : [ { entry : { isFile : false } } , { entry : { isFile : false } } ] } } ; expect ( app . hasLockedFiles ( context ) ) . toBe ( false ) ; } ) ; it ( 'should return [true] when one of files is locked' , ( ) => { const context = { selection : { nodes : [ { entry : { isFile : true , isLocked : true } } , { entry : { isFile : true , isLocked : false } } ] } } ; expect ( app . hasLockedFiles ( context ) ) . toBe ( true ) ; } ) ; it ( 'should return [true] when one of files has readonly lock' , ( ) => { const context = { selection : { nodes : [ { entry : { isFile : true , isLocked : false } } , { entry : { isFile : true , isLocked : false , properties : { 'cm:lockType' : 'READ_ONLY_LOCK' } } } ] } } ; expect ( app . hasLockedFiles ( context ) ) . toBe ( true ) ; } ) ; } ) ; describe ( 'canUpdateSelectedNode' , ( ) => { it ( 'should return [false] if selection not preset' , ( ) => { const context = { } ; expect ( app . canUpdateSelectedNode ( context ) ) . toBe ( false ) ; } ) ; it ( 'should return [false] if selection is empty' , ( ) => { const context = { selection : { isEmpty : true } } ; expect ( app . canUpdateSelectedNode ( context ) ) . toBe ( false ) ; } ) ; it ( 'should return [false] if first selection is not a file' , ( ) => { const context = { permissions : { check : ( ) => false } , selection : { isEmpty : false , first : { entry : { isFile : false } } } } ; expect ( app . canUpdateSelectedNode ( context ) ) . toBe ( false ) ; } ) ; it ( 'should return [false] if the file is locked' , ( ) => { const context = { permissions : { check : ( ) => true } , selection : { isEmpty : false , nodes : [ { entry : { isFile : true , isLocked : true } } ] , first : { entry : { isFile : true , isLocked : true } } } } ; expect ( app . canUpdateSelectedNode ( context ) ) . toBe ( false ) ; } ) ; it ( 'should evaluate allowable operation for the file' , ( ) => { const context = { permissions : { check : ( ) => true } , selection : { isEmpty : false , nodes : [ { entry : { isFile : true , allowableOperationsOnTarget : [ ] } } ] , first : { entry : { isFile : true , allowableOperationsOnTarget : [ ] } } } } ; expect ( app . canUpdateSelectedNode ( context ) ) . toBe ( true ) ; } ) ; } ) ; describe ( 'canUploadVersion' , ( ) => { it ( 'should return [true] if user has locked it previously' , ( ) => { const context = { navigation : { url : '/personal-files' } , profile : { id : 'user1' } , selection : { file : { entry : { properties : { 'cm:lockType' : 'WRITE_LOCK' , 'cm:lockOwner' : { id : 'user1' } } } } } } ; expect ( app . canUploadVersion ( context ) ) . toBe ( true ) ; } ) ; it ( 'should return [false] if other user has locked it previously' , ( ) => { const context = { navigation : { url : '/personal-files' } , profile : { id : 'user2' } , selection : { file : { entry : { properties : { 'cm:lockType' : 'WRITE_LOCK' , 'cm:lockOwner' : { id : 'user1' } } } } } } ; expect ( app . canUploadVersion ( context ) ) . toBe ( false ) ; } ) ; it ( 'should check the [update] operation when no write lock present' , ( ) => { let checked = false ; const context = { navigation : { url : '/personal-files' } , permissions : { check : ( ) => ( checked = true ) } , selection : { file : { } , isEmpty : false , nodes : [ { entry : { isFile : true } } ] , first : { entry : { isFile : true } } } } ; expect ( app . canUploadVersion ( context ) ) . toBe ( true ) ; expect ( checked ) . toBe ( true ) ; } ) ; it ( 'should return [true] if route is `/favorites`' , ( ) => { const context = { selection : { file : { } } , navigation : { url : '/favorites' } } ; expect ( app . canUploadVersion ( context ) ) . toBe ( true ) ; } ) ; it ( 'should return [true] if route is `/favorites`' , ( ) => { const context = { selection : { file : { } } , navigation : { url : '/favorites' } } ; expect ( app . canUploadVersion ( context ) ) . toBe ( true ) ; } ) ; it ( 'should return [true] if route is `/shared`' , ( ) => { const context = { selection : { file : { } } , navigation : { url : '/shared' } } ; expect ( app . canUploadVersion ( context ) ) . toBe ( true ) ; } ) ; } ) ; describe ( 'isShared' , ( ) => { it ( 'should return true if route is shared files and single selection' , ( ) => { const context = { selection : { file : { } } , navigation : { url : '/shared' } } ; expect ( app . isShared ( context ) ) . toBe ( true ) ; } ) ; it ( 'should return false if route is shared files and multiple selection' , ( ) => { const context = { selection : { file : null } , navigation : { url : '/shared' } } ; expect ( app . isShared ( context ) ) . toBe ( false ) ; } ) ; it ( 'should return false if route is trashcan route' , ( ) => { const context = { selection : { file : { } } , navigation : { url : '/trashcan' } } ; expect ( app . isShared ( context ) ) . toBe ( false ) ; } ) ; it ( 'should return false if selection is not shared' , ( ) => { const context = { selection : { file : { entry : { properties : { } } } } , navigation : { url : '/other' } } ; expect ( app . isShared ( context ) ) . toBe ( false ) ; } ) ; it ( 'should return true if selection is shared' , ( ) => { const context = { selection : { file : { entry : { properties : { 'qshare:sharedId' : 'some-id' } } } } , navigation : { url : '/other' } } ; expect ( app . isShared ( context ) ) . toBe ( true ) ; } ) ; } ) ; describe ( 'canShowLogout' , ( ) => { it ( 'should return false when `withCredentials` property is true' , ( ) => { const context = { withCredentials : true } ; expect ( app . canShowLogout ( context ) ) . toBe ( false ) ; } ) ; it ( 'should return true when `withCredentials` property is false' , ( ) => { const context = { withCredentials : false } ; expect ( app . canShowLogout ( context ) ) . toBe ( true ) ; } ) ; } ) ; describe ( 'isLibraryManager' , ( ) => { it ( 'should return true when role is SiteManager' , ( ) => { const context = { selection : { library : { entry : { role : 'SiteManager' } } } } ; expect ( app . isLibraryManager ( context ) ) . toBe ( true ) ; } ) ; it ( 'should return false when role is different than SiteManager' , ( ) => { const context = { selection : { library : { entry : { role : 'SiteCollaborator' } } } } ; expect ( app . isLibraryManager ( context ) ) . toBe ( false ) ; } ) ; } ) ; } ) ;