Skip to content

Commit 3e32efa

Browse files
authored
feat: added supportsRTCPeerConnection() (#17)
1 parent 54bd90d commit 3e32efa

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

src/web-capabilities.spec.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,45 @@ describe('WebCapabilities', () => {
206206
);
207207
});
208208
});
209+
210+
describe('supportsRTCPeerConnection', () => {
211+
afterEach(() => {
212+
// Clean up window modifications
213+
delete (window as Window & { RTCPeerConnection?: unknown }).RTCPeerConnection;
214+
});
215+
216+
/**
217+
* Sets up the window object with or without RTCPeerConnection.
218+
* @param hasRTCPeerConnection - True when it should exist, false otherwise.
219+
*/
220+
const setupWindow = (hasRTCPeerConnection: boolean) => {
221+
if (hasRTCPeerConnection) {
222+
/**
223+
* Mock RTCPeerConnection constructor for testing.
224+
*/
225+
// eslint-disable-next-line @typescript-eslint/no-empty-function
226+
const MockRTCPeerConnection = function MockRTCPeerConnection() {};
227+
Object.defineProperty(window, 'RTCPeerConnection', {
228+
writable: true,
229+
configurable: true,
230+
value: MockRTCPeerConnection,
231+
});
232+
} else {
233+
delete (window as Window & { RTCPeerConnection?: unknown }).RTCPeerConnection;
234+
}
235+
};
236+
237+
it('returns true when RTCPeerConnection is available', () => {
238+
expect.assertions(1);
239+
setupWindow(true);
240+
expect(WebCapabilities.supportsRTCPeerConnection()).toBe(CapabilityState.CAPABLE);
241+
});
242+
243+
it('returns false when RTCPeerConnection is not available', () => {
244+
expect.assertions(1);
245+
setupWindow(false);
246+
247+
expect(WebCapabilities.supportsRTCPeerConnection()).toBe(CapabilityState.NOT_CAPABLE);
248+
});
249+
});
209250
});

src/web-capabilities.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,16 @@ export class WebCapabilities {
110110
? CapabilityState.CAPABLE
111111
: CapabilityState.NOT_CAPABLE;
112112
}
113+
114+
/**
115+
* Checks whether the browser supports RTCPeerConnection. This is needed,
116+
* because some users install browser extensions that remove RTCPeerConnection.
117+
*
118+
* @returns A {@link CapabilityState}.
119+
*/
120+
static supportsRTCPeerConnection(): CapabilityState {
121+
return typeof RTCPeerConnection === 'function'
122+
? CapabilityState.CAPABLE
123+
: CapabilityState.NOT_CAPABLE;
124+
}
113125
}

0 commit comments

Comments
 (0)