Skip to content

Instantly share code, notes, and snippets.

@contra contra/mock-rtc.js
Created Dec 11, 2015

Embed
What would you like to do?
WebRTC Mock
'use strict';
var mock = require('../temp-mock');
var temasys = require('./index');
var nu = require('new-operator');
var RTCPeerConnectionMock = mock({
methods: [
'addIceCandidate',
'addStream',
'close',
'createAnswer',
'createDTMFSender',
'createDataChannel',
'createOffer',
'setLocalDescription',
'setRemoteDescription',
'removeStream',
'getLocalStreams',
'getRemoteStreams',
'getStats',
'getStreamById',
'updateIce'
],
properties: [
'iceConnectionState',
'iceGatheringState',
'localDescription',
'remoteDescription',
'onaddstream',
'ondatachannel',
'onicecandidate',
'oniceconnectionstatechange',
'onnegotiationneeded',
'onremovestream',
'onsignalingstatechange',
'signalingState'
]
});
var RTCSessionDescriptionMock = mock({
properties: [
'type',
'sdp'
]
});
var RTCIceCandidateMock = mock({
properties: [
'candidate',
'sdpMid',
'sdpMLineIndex'
]
});
module.exports = {
RTCPeerConnection: function(){
var inst = nu.apply(RTCPeerConnectionMock, arguments);
temasys(function(rtc){
mock.resolve(inst, rtc.RTCPeerConnection);
});
return inst;
},
RTCSessionDescription: function(){
var inst = nu.apply(RTCSessionDescriptionMock, arguments);
temasys(function(rtc){
mock.resolve(inst, rtc.RTCSessionDescription);
});
return inst;
},
RTCIceCandidate: function(){
var inst = nu.apply(RTCIceCandidateMock, arguments);
temasys(function(rtc){
mock.resolve(inst, rtc.RTCIceCandidate);
});
return inst;
}
};
'use strict';
// temporarily mock a class
// when the thing is loaded, replay all actions
// that happened on the mock against the real thing
function mockit(opt) {
if (!opt) opt = {};
if (!opt.properties) opt.properties = [];
if (!opt.methods) opt.methods = [];
function Mock(){
this._ctorArgs = arguments;
this._actions = [];
this._state = {};
this._opt = opt;
}
// TODO: detect key conflicts between these options
opt.properties.forEach(hookProperty);
opt.methods.forEach(hookMethod);
return Mock;
function hookMethod(k){
Mock.prototype[k] = function(){
this._actions.push({
type: 'call',
key: k,
args: arguments
});
return this;
};
}
function hookProperty(k){
Object.defineProperty(Mock.prototype, k, {
set: function(v){
this._state[k] = v;
if (this._actions) {
this._actions.push({
type: 'set',
key: k,
value: v
});
}
},
get: function(){
return this._state[k];
}
});
}
}
mockit.resolve = function(inst, fn) {
// create the real thing
var clazz = fn.apply(fn, inst._ctorArgs);
if (typeof clazz === 'undefined') {
clazz = {};
}
// replay all of our stuff onto the real thing
inst._actions.forEach(function(action){
if (action.type === 'set') {
clazz[action.key] = action.value;
} else if (action.type === 'call' &&
typeof clazz[action.key] === 'function') {
clazz[action.key].apply(inst, action.args);
}
});
// make our fake thing into the real thing
inst._opt.methods.forEach(function(k){
inst[k] = clazz[k];
});
inst._opt.properties.forEach(function(k){
Object.defineProperty(inst, k, {
set: function(v) {
return clazz[k] = v;
},
get: function(k) {
return clazz[k];
}
});
});
delete inst._opt;
delete inst._state;
delete inst._actions;
delete inst._ctorArgs;
return clazz;
};
module.exports = mockit;
@rohanj1607

This comment has been minimized.

Copy link

commented Jul 18, 2018

You haven't included index.js file in the gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.