import { ipcRenderer } from 'electron' ; import PeerConnection from './features/PeerConnection' ; import { handleIpcRenderer } from './peerConnectionHelperRendererWindowIndex' ; jest . useFakeTimers ( ) ; jest . mock ( 'electron' , ( ) => { return { ipcRenderer : { on : jest . fn ( ) , send : jest . fn ( ) , } , remote : { getGlobal : jest . fn ( ) , } , } ; } ) ; jest . mock ( 'simple-peer' ) ; jest . mock ( './features/PeerConnection' ) ; describe ( 'peerConnectionHelperRendererWindowIndex tests' , ( ) => { beforeEach ( ( ) => { jest . clearAllMocks ( ) ; jest . restoreAllMocks ( ) ; PeerConnection . mockClear ( ) ; } ) ; function mockAndGetPeerConnectionInstance ( ) { handleIpcRenderer ( ) ; let callback = ipcRenderer . on . mock . calls [ 0 ] [ 1 ] ; ipcRenderer . on . mockClear ( ) ; callback ( ) ; callback = ipcRenderer . on . mock . calls [ 0 ] [ 1 ] ; callback ( undefined , { roomId : '12' , sharingSessionID : '39392' , user : 'asd' , appTheme : true , appLanguage : 'bz' , } ) ; return PeerConnection . mock . instances [ 0 ] ; } describe ( 'when handleIpcRenderer was called' , ( ) => { it ( 'should set ipcRenderer.on("start-peer-connection" listener' , ( ) => { handleIpcRenderer ( ) ; expect ( ipcRenderer . on ) . toHaveBeenCalledWith ( 'start-peer-connection' , expect . anything ( ) ) ; } ) ; describe ( 'when ipcRenderer.on("start-peer-connection" callback occurred' , ( ) => { it ( 'should set ipcRenderer.on("create-peer-connection-with-data"' , ( ) => { handleIpcRenderer ( ) ; const callback = ipcRenderer . on . mock . calls [ 0 ] [ 1 ] ; ipcRenderer . on . mockClear ( ) ; callback ( ) ; expect ( ipcRenderer . on ) . toHaveBeenCalledWith ( 'create-peer-connection-with-data' , expect . anything ( ) ) ; } ) ; it ( 'should set ipcRenderer listeners' , ( ) => { handleIpcRenderer ( ) ; const callback = ipcRenderer . on . mock . calls [ 0 ] [ 1 ] ; ipcRenderer . on . mockClear ( ) ; callback ( ) ; expect ( ipcRenderer . on ) . toHaveBeenCalledWith ( 'create-peer-connection-with-data' , expect . anything ( ) ) ; expect ( ipcRenderer . on ) . toHaveBeenCalledWith ( 'create-peer-connection-with-data' , expect . anything ( ) ) ; expect ( ipcRenderer . on ) . toHaveBeenCalledWith ( 'set-desktop-capturer-source-id' , expect . anything ( ) ) ; expect ( ipcRenderer . on ) . toHaveBeenCalledWith ( 'call-peer' , expect . anything ( ) ) ; expect ( ipcRenderer . on ) . toHaveBeenCalledWith ( 'disconnect-by-host-machine-user' , expect . anything ( ) ) ; expect ( ipcRenderer . on ) . toHaveBeenCalledWith ( 'deny-connection-for-partner' , expect . anything ( ) ) ; expect ( ipcRenderer . on ) . toHaveBeenCalledWith ( 'send-user-allowed-to-connect' , expect . anything ( ) ) ; expect ( ipcRenderer . on ) . toHaveBeenCalledWith ( 'app-color-theme-changed' , expect . anything ( ) ) ; expect ( ipcRenderer . on ) . toHaveBeenCalledWith ( 'app-language-changed' , expect . anything ( ) ) ; } ) ; describe ( 'when ipcRenderer.on("create-peer-connection-with-data" callback occurred' , ( ) => { it ( 'should initialize PeerConnection' , ( ) => { handleIpcRenderer ( ) ; let callback = ipcRenderer . on . mock . calls [ 0 ] [ 1 ] ; ipcRenderer . on . mockClear ( ) ; callback ( ) ; callback = ipcRenderer . on . mock . calls [ 0 ] [ 1 ] ; callback ( undefined , { roomId : '12' , sharingSessionID : '39392' , user : 'asd' , appTheme : true , appLanguage : 'bz' , } ) ; expect ( PeerConnection ) . toHaveBeenCalled ( ) ; const peerConnectionInstance = PeerConnection . mock . instances [ 0 ] ; expect ( peerConnectionInstance . setOnDeviceConnectedCallback ) . toBeCalled ( ) ; } ) ; describe ( 'when on device connected callback occurred' , ( ) => { it ( 'should call ipcRenderer.send("peer-connected" with device data' , ( ) => { const peerConnectionInstance = mockAndGetPeerConnectionInstance ( ) ; const callback = peerConnectionInstance . setOnDeviceConnectedCallback . mock . calls [ 0 ] [ 0 ] ; const testDeviceData = 'asd23faga' ; callback ( testDeviceData ) ; expect ( ipcRenderer . send ) . toHaveBeenCalledWith ( 'peer-connected' , testDeviceData ) ; } ) ; } ) ; } ) ; describe ( 'when ipcRenderer.on("set-desktop-capturer-source-id" callback occurred' , ( ) => { it ( 'should call peerConnection.setDesktopCapturerSourceID(id) with proper source id' , ( ) => { const peerConnectionInstance = mockAndGetPeerConnectionInstance ( ) ; const setDesktopCapturerSourceIdCallback = ipcRenderer . on . mock . calls [ 1 ] [ 1 ] ; const testSourceID = '12411' ; setDesktopCapturerSourceIdCallback ( undefined , testSourceID ) ; expect ( peerConnectionInstance . setDesktopCapturerSourceID ) . toHaveBeenCalledWith ( testSourceID ) ; } ) ; } ) ; describe ( 'when ipcRenderer.on("call-peer" callback occurred' , ( ) => { it ( 'should call peerConnection.callPeer()' , ( ) => { const peerConnectionInstance = mockAndGetPeerConnectionInstance ( ) ; const callPeerCallback = ipcRenderer . on . mock . calls [ 2 ] [ 1 ] ; callPeerCallback ( ) ; expect ( peerConnectionInstance . callPeer ) . toHaveBeenCalled ( ) ; } ) ; } ) ; describe ( 'when ipcRenderer.on("disconnect-by-host-machine-user" callback occurred' , ( ) => { it ( 'should call peerConnection.disconnectByHostMachineUser()' , ( ) => { const peerConnectionInstance = mockAndGetPeerConnectionInstance ( ) ; const disconnectCallback = ipcRenderer . on . mock . calls [ 3 ] [ 1 ] ; disconnectCallback ( ) ; expect ( peerConnectionInstance . disconnectByHostMachineUser ) . toHaveBeenCalled ( ) ; } ) ; } ) ; describe ( 'when ipcRenderer.on("deny-connection-for-partner" callback occurred' , ( ) => { it ( 'should call peerConnection.denyConnectionForPartner()' , ( ) => { const peerConnectionInstance = mockAndGetPeerConnectionInstance ( ) ; const denyConnectionCallback = ipcRenderer . on . mock . calls [ 4 ] [ 1 ] ; denyConnectionCallback ( ) ; expect ( peerConnectionInstance . denyConnectionForPartner ) . toHaveBeenCalled ( ) ; } ) ; } ) ; describe ( 'when ipcRenderer.on("send-user-allowed-to-connect" callback occurred' , ( ) => { it ( 'should call peerConnection.sendUserAllowedToConnect()' , ( ) => { const peerConnectionInstance = mockAndGetPeerConnectionInstance ( ) ; const sendUserAllowedToConnectCallback = ipcRenderer . on . mock . calls [ 5 ] [ 1 ] ; sendUserAllowedToConnectCallback ( ) ; expect ( peerConnectionInstance . sendUserAllowedToConnect ) . toHaveBeenCalled ( ) ; } ) ; } ) ; } ) ; } ) ; } ) ;