import SharingSessionStatusEnum from './SharingSessionStatusEnum' ; import SharingSession from './SharingSession' ; import SharingSessionService from '.' ; import RoomIDService from '../../server/RoomIDService' ; import ConnectedDevicesService from '../ConnectedDevicesService' ; import PeerConnectionHelperRendererService from '../PeerConnectionHelperRendererService' ; jest . mock ( '../PeerConnectionHelperRendererService' , ( ) => { return jest . fn ( ) . mockImplementation ( ( ) => { return { createPeerConnectionHelper : ( ) => { return { webContents : { on : ( ) => { } , toggleDevTools : ( ) => { } , } , } ; } , } ; } ) ; } ) ; jest . useFakeTimers ( ) ; describe ( 'SharingSessionService unit tests' , ( ) => { let sharingSessionService ; beforeEach ( ( ) => { sharingSessionService = new SharingSessionService ( new RoomIDService ( ) , new ConnectedDevicesService ( ) , new PeerConnectionHelperRendererService ( '' ) ) ; } ) ; afterEach ( ( ) => { jest . clearAllMocks ( ) ; jest . restoreAllMocks ( ) ; } ) ; describe ( 'when new SharingSessionService() is created' , ( ) => { it ( 'should have empty sharingSessions Map' , ( ) => { expect ( sharingSessionService . sharingSessions . size ) . toBe ( 0 ) ; } ) ; it ( 'should have waitingForConnectionSharingSession set to null' , ( ) => { expect ( sharingSessionService . waitingForConnectionSharingSession ) . toBe ( null ) ; } ) ; it ( 'should have pollForInactiveSessions be called' , ( ) => { const backup = SharingSessionService . prototype . pollForInactiveSessions ; const mockPollForInactiveSessions = jest . fn ( ) ; jest . spyOn ( SharingSessionService . prototype , 'pollForInactiveSessions' ) . mockImplementation ( mockPollForInactiveSessions ) ; new SharingSessionService ( new RoomIDService ( ) , new ConnectedDevicesService ( ) , new PeerConnectionHelperRendererService ( '' ) ) ; jest . advanceTimersByTime ( 1000 * 60 * 60 * 30 ) ; expect ( mockPollForInactiveSessions ) . toBeCalled ( ) ; SharingSessionService . prototype . pollForInactiveSessions = backup ; } ) ; } ) ; describe ( 'when createNewSharingSession is called' , ( ) => { it ( 'should have sharingSessions Map with size equal to 1' , async ( ) => { await sharingSessionService . createNewSharingSession ( '' ) ; expect ( sharingSessionService . sharingSessions . size ) . toBe ( 1 ) ; } ) ; it ( 'should have returned SharingSession object' , async ( ) => { expect ( await sharingSessionService . createNewSharingSession ( '' ) ) . toBeInstanceOf ( SharingSession ) ; } ) ; } ) ; describe ( 'when pollForInactiveSessions is called' , ( ) => { it ( 'should have removed SharingSession with status ERROR from sharingSessions Map' , async ( ) => { const testSharingSession = await sharingSessionService . createNewSharingSession ( '' ) ; testSharingSession . status = SharingSessionStatusEnum . ERROR ; sharingSessionService . pollForInactiveSessions ( ) ; expect ( sharingSessionService . sharingSessions . size ) . toBe ( 0 ) ; } ) ; } ) ; describe ( 'when createWaitingForConnectionSharingSession is called' , ( ) => { it ( 'should call waitWhileUserIsNotCreated' , async ( ) => { sharingSessionService . waitWhileUserIsNotCreated = jest . fn ( ) . mockImplementation ( ( ) => { return new Promise ( ( resolve ) => resolve ( undefined ) ) ; } ) ; await sharingSessionService . createWaitingForConnectionSharingSession ( '' ) ; expect ( sharingSessionService . waitWhileUserIsNotCreated ) . toBeCalled ( ) ; } ) ; describe ( 'when user created' , ( ) => { it ( 'should call createNewSharingSession with roomID' , async ( ) => { const testRoomID = '12342341' ; sharingSessionService . waitWhileUserIsNotCreated = jest . fn ( ) . mockImplementation ( ( ) => { return new Promise ( ( resolve ) => resolve ( undefined ) ) ; } ) ; sharingSessionService . createNewSharingSession = jest . fn ( ) ; await sharingSessionService . createWaitingForConnectionSharingSession ( testRoomID ) ; expect ( sharingSessionService . createNewSharingSession ) . toBeCalledWith ( testRoomID ) ; } ) ; it ( 'should resolve with waitingForConnectionSharingSession' , async ( ) => { const testSharingSession = new Promise < SharingSession > ( ( resolve ) => resolve ( ( { ab : 'ba' } as unknown ) as SharingSession ) ) ; sharingSessionService . waitWhileUserIsNotCreated = jest . fn ( ) . mockImplementation ( ( ) => { return new Promise ( ( resolve ) => resolve ( undefined ) ) ; } ) ; sharingSessionService . createNewSharingSession = ( ) => testSharingSession ; const res = await sharingSessionService . createWaitingForConnectionSharingSession ( '234' ) ; expect ( res ) . toBe ( await testSharingSession ) ; } ) ; } ) ; } ) ; describe ( 'when changeSharingSessionStatusToSharing is called' , ( ) => { it ( 'should change passed sharingSession status to SHARING' , ( ) => { const testSharingSession = ( { status : 'dummystatus' , } as unknown ) as SharingSession ; sharingSessionService . changeSharingSessionStatusToSharing ( testSharingSession ) ; expect ( testSharingSession . status ) . toBe ( SharingSessionStatusEnum . SHARING ) ; } ) ; } ) ; describe ( 'when waitWhileUserIsNotCreated is called' , ( ) => { it ( 'should wait until user is created then call clearInterval' , ( ) => { const testUser = { username : 'string' , privateKey : 'string' , publicKey : 'string' , } ; sharingSessionService . waitWhileUserIsNotCreated ( ) ; expect ( setInterval ) . toHaveBeenCalledTimes ( 2 ) ; expect ( clearInterval ) . toHaveBeenCalledTimes ( 0 ) ; sharingSessionService . user = testUser ; jest . advanceTimersByTime ( 10000 ) ; expect ( setInterval ) . toHaveBeenCalledTimes ( 2 ) ; expect ( clearInterval ) . toHaveBeenCalledTimes ( 1 ) ; } ) ; } ) ; } ) ;