File tree 2 files changed +26
-2
lines changed
2 files changed +26
-2
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ import RTCCertificate from './RTCCertificate';
12
12
// extend RTCConfiguration with peerIdentity
13
13
interface RTCConfiguration extends globalThis . RTCConfiguration {
14
14
peerIdentity ?: string ;
15
+ peerConnection ?: PeerConnection ;
15
16
}
16
17
17
18
export default class RTCPeerConnection extends EventTarget implements globalThis . RTCPeerConnection {
@@ -121,7 +122,7 @@ export default class RTCPeerConnection extends EventTarget implements globalThis
121
122
122
123
try {
123
124
const peerIdentity = ( config as any ) ?. peerIdentity ?? `peer-${ getRandomString ( 7 ) } ` ;
124
- this . #peerConnection = new PeerConnection ( peerIdentity ,
125
+ this . #peerConnection = config . peerConnection ?? new PeerConnection ( peerIdentity ,
125
126
{
126
127
...config ,
127
128
iceServers :
Original file line number Diff line number Diff line change 1
- import { expect } from '@jest/globals' ;
1
+ import { expect , jest } from '@jest/globals' ;
2
2
import { RTCPeerConnection , RTCDataChannel } from '../../src/polyfill/index' ;
3
+ import { PeerConnection } from '../../src/lib/index' ;
3
4
4
5
describe ( 'polyfill' , ( ) => {
5
6
// Default is 5000 ms but we need more
@@ -212,4 +213,26 @@ describe('polyfill', () => {
212
213
} ;
213
214
} ) ;
214
215
} ) ;
216
+
217
+ test ( 'it should accept a preconfigured PeerConnection' , ( ) => {
218
+ const peerConnection = new PeerConnection ( 'Peer' , {
219
+ iceServers : [ ] ,
220
+ } ) ;
221
+
222
+ // have to override write-only method in order to spy on it
223
+ const originalFunc = peerConnection . state . bind ( peerConnection ) ;
224
+ Object . defineProperty ( peerConnection , 'state' , {
225
+ value : originalFunc ,
226
+ writable : true ,
227
+ enumerable : true ,
228
+ } ) ;
229
+
230
+ const spy = jest . spyOn ( peerConnection , 'state' ) ;
231
+ const rtcPeerConnection = new RTCPeerConnection ( {
232
+ peerConnection,
233
+ } ) ;
234
+ const connectionState = rtcPeerConnection . connectionState ;
235
+ expect ( spy ) . toHaveBeenCalled ( ) ;
236
+ expect ( connectionState ) . toEqual ( originalFunc ( ) ) ;
237
+ } ) ;
215
238
} ) ;
You can’t perform that action at this time.
0 commit comments