import { NgZone } from '@angular/core' ; import { JConnectionStub , JHubProxyStub } from './jConnection.stub' ; import { TestBed } from '@angular/core/testing' ; import { SignalRConnection , SignalRConfiguration , BroadcastEventListener } from 'projects/ng2-signalr/src/public-api' ; describe ( 'Connection' , ( ) => { let zone ; let jConnectionStub ; let hubProxy ; beforeEach ( ( ) => { zone = new NgZone ( { enableLongStackTrace : true } ) ; jConnectionStub = new JConnectionStub ( ) ; hubProxy = new JHubProxyStub ( ) ; } ) ; afterAll ( ( ) => { TestBed . resetTestingModule ( ) ; } ) ; it ( 'id should get jConnection-id' , ( ) => { const connection = new SignalRConnection ( jConnectionStub , hubProxy , zone , new SignalRConfiguration ( ) ) ; expect ( connection . id ) . toBe ( jConnectionStub . id ) ; } ) ; it ( 'listen should proxy on listener event' , ( ) => { spyOn ( hubProxy , 'on' ) ; const connection = new SignalRConnection ( jConnectionStub , hubProxy , zone , new SignalRConfiguration ( ) ) ; const listener = new BroadcastEventListener < any > ( 'OnMessageSent' ) ; connection . listen ( listener ) ; expect ( hubProxy . on ) . toHaveBeenCalledWith ( listener . event , jasmine . any ( Function ) ) ; } ) ; it ( 'listenFor should proxy on event' , ( ) => { spyOn ( hubProxy , 'on' ) ; const connection = new SignalRConnection ( jConnectionStub , hubProxy , zone , new SignalRConfiguration ( ) ) ; const listener = connection . listenFor < any > ( 'OnMessageSent' ) ; expect ( hubProxy . on ) . toHaveBeenCalledWith ( 'OnMessageSent' , jasmine . any ( Function ) ) ; expect ( listener . event ) . toBe ( 'OnMessageSent' ) ; } ) ; it ( 'listenFor should throw when event is empty' , ( ) => { const connection = new SignalRConnection ( jConnectionStub , hubProxy , zone , new SignalRConfiguration ( ) ) ; const action1 = ( ) => connection . listenFor < any > ( '' ) ; const action2 = ( ) => connection . listenFor < any > ( null ) ; expect ( action1 ) . toThrow ( ) ; expect ( action2 ) . toThrow ( ) ; } ) ; } ) ;