Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: accept preconfigured PeerConnection as polyfill arg #260

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/polyfill/RTCPeerConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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:
Expand Down
25 changes: 24 additions & 1 deletion test/jest-tests/polyfill.test.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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());
});
});