Skip to content
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
41 changes: 41 additions & 0 deletions src/web-capabilities.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,45 @@ describe('WebCapabilities', () => {
);
});
});

describe('supportsRTCPeerConnection', () => {
afterEach(() => {
// Clean up window modifications
delete (window as Window & { RTCPeerConnection?: unknown }).RTCPeerConnection;
});

/**
* Sets up the window object with or without RTCPeerConnection.
* @param hasRTCPeerConnection - True when it should exist, false otherwise.
*/
const setupWindow = (hasRTCPeerConnection: boolean) => {
if (hasRTCPeerConnection) {
/**
* Mock RTCPeerConnection constructor for testing.
*/
// eslint-disable-next-line @typescript-eslint/no-empty-function
const MockRTCPeerConnection = function MockRTCPeerConnection() {};
Object.defineProperty(window, 'RTCPeerConnection', {
writable: true,
configurable: true,
value: MockRTCPeerConnection,
});
} else {
delete (window as Window & { RTCPeerConnection?: unknown }).RTCPeerConnection;
}
};

it('returns true when RTCPeerConnection is available', () => {
expect.assertions(1);
setupWindow(true);
expect(WebCapabilities.supportsRTCPeerConnection()).toBe(CapabilityState.CAPABLE);
});

it('returns false when RTCPeerConnection is not available', () => {
expect.assertions(1);
setupWindow(false);

expect(WebCapabilities.supportsRTCPeerConnection()).toBe(CapabilityState.NOT_CAPABLE);
});
});
});
12 changes: 12 additions & 0 deletions src/web-capabilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,16 @@ export class WebCapabilities {
? CapabilityState.CAPABLE
: CapabilityState.NOT_CAPABLE;
}

/**
* Checks whether the browser supports RTCPeerConnection. This is needed,
* because some users install browser extensions that remove RTCPeerConnection.
*
* @returns A {@link CapabilityState}.
*/
static supportsRTCPeerConnection(): CapabilityState {
return typeof RTCPeerConnection === 'function'
? CapabilityState.CAPABLE
: CapabilityState.NOT_CAPABLE;
}
}