diff --git a/src/polyfill/RTCPeerConnection.ts b/src/polyfill/RTCPeerConnection.ts index 61920f32..59318042 100644 --- a/src/polyfill/RTCPeerConnection.ts +++ b/src/polyfill/RTCPeerConnection.ts @@ -12,6 +12,7 @@ import RTCCertificate from './RTCCertificate'; // extend RTCConfiguration with peerIdentity interface RTCConfiguration extends globalThis.RTCConfiguration { peerIdentity?: string; + peerConnection?: PeerConnection; } export default class RTCPeerConnection extends EventTarget implements globalThis.RTCPeerConnection { @@ -121,7 +122,7 @@ export default class RTCPeerConnection extends EventTarget implements globalThis try { const peerIdentity = (config as any)?.peerIdentity ?? `peer-${getRandomString(7)}`; - this.#peerConnection = new PeerConnection(peerIdentity, + this.#peerConnection = config.peerConnection ?? new PeerConnection(peerIdentity, { ...config, iceServers: diff --git a/test/jest-tests/polyfill.test.ts b/test/jest-tests/polyfill.test.ts index 41ab1a0a..e982e40d 100644 --- a/test/jest-tests/polyfill.test.ts +++ b/test/jest-tests/polyfill.test.ts @@ -1,5 +1,6 @@ -import { expect } from '@jest/globals'; +import { expect, jest } from '@jest/globals'; import { RTCPeerConnection, RTCDataChannel } from '../../src/polyfill/index'; +import { PeerConnection } from '../../src/lib/index'; describe('polyfill', () => { // Default is 5000 ms but we need more @@ -212,4 +213,26 @@ describe('polyfill', () => { }; }); }); + + test('it should accept a preconfigured PeerConnection', () => { + const peerConnection = new PeerConnection('Peer', { + iceServers: [], + }); + + // have to override write-only method in order to spy on it + const originalFunc = peerConnection.state.bind(peerConnection); + Object.defineProperty(peerConnection, 'state', { + value: originalFunc, + writable: true, + enumerable: true, + }); + + const spy = jest.spyOn(peerConnection, 'state'); + const rtcPeerConnection = new RTCPeerConnection({ + peerConnection, + }); + const connectionState = rtcPeerConnection.connectionState; + expect(spy).toHaveBeenCalled(); + expect(connectionState).toEqual(originalFunc()); +}); });