Skip to content

Commit

Permalink
Unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
charliesantos committed Oct 4, 2022
1 parent 2c55bdd commit cbcd116
Show file tree
Hide file tree
Showing 3 changed files with 260 additions and 100 deletions.
6 changes: 2 additions & 4 deletions tests/peerconnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,6 @@ describe('PeerConnection', () => {
const eIceServers = 'iceServers';
const eIss = 'this is iss';
const eSDP = 'sdp';
const messagesToRegisterFor = ['messagesToRegisterFor-foo', 'messagesToRegisterFor-bar'];

let context = null;
let version = null;
Expand Down Expand Up @@ -825,7 +824,6 @@ describe('PeerConnection', () => {
eCallSid,
eConstraints,
eIceServers,
messagesToRegisterFor,
callback,
);
});
Expand Down Expand Up @@ -867,7 +865,7 @@ describe('PeerConnection', () => {
assert(version.createOffer.calledWithExactly(undefined, undefined, {audio: true}, sinon.match.func, sinon.match.func));
assert.equal(callback.called, false);
assert(context.pstream.invite.calledOnce);
assert(context.pstream.invite.calledWithExactly(eSDP, eCallSid, true, eParams, messagesToRegisterFor));
assert(context.pstream.invite.calledWithExactly(eSDP, eCallSid, true, eParams));
assert(version.getSDP.calledOnce);
assert(version.getSDP.calledWithExactly());
assert(context.pstream.on.calledWithExactly('answer', sinon.match.func));
Expand All @@ -884,7 +882,7 @@ describe('PeerConnection', () => {
assert(version.createOffer.calledWithExactly(undefined, undefined, {audio: true}, sinon.match.func, sinon.match.func));
assert.equal(callback.called, false);
assert(context.pstream.invite.calledOnce);
assert(context.pstream.invite.calledWithExactly(eSDP, eCallSid, true, eParams, messagesToRegisterFor));
assert(context.pstream.invite.calledWithExactly(eSDP, eCallSid, true, eParams));
assert(version.getSDP.calledOnce);
assert(version.getSDP.calledWithExactly());
assert(context.pstream.on.calledWithExactly('answer', sinon.match.func));
Expand Down
58 changes: 47 additions & 11 deletions tests/pstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,47 @@ describe('PStream', () => {
});
});

describe('sendMessage', () => {
const callsid = 'testcallsid';
const content = { foo: 'content' };
const messagetype = 'user-defined-message';
const voiceeventsid = 'testvoiceeventsid';

it('should send a message with the provided info', () => {
pstream.sendMessage(callsid, content, undefined, messagetype, voiceeventsid);
assert.equal(pstream.transport.send.callCount, 1);
console.log(pstream.transport.send.args[0][0])
assert.deepEqual(JSON.parse(pstream.transport.send.args[0][0]), {
type: 'message',
version:EXPECTED_PSTREAM_VERSION,
payload: {
callsid,
content,
contenttype: 'application/json',
messagetype,
voiceeventsid,
}
});
});

it('should override contenttype', () => {
pstream.sendMessage(callsid, content, 'text/plain', messagetype, voiceeventsid);
assert.equal(pstream.transport.send.callCount, 1);
console.log(pstream.transport.send.args[0][0])
assert.deepEqual(JSON.parse(pstream.transport.send.args[0][0]), {
type: 'message',
version:EXPECTED_PSTREAM_VERSION,
payload: {
callsid,
content,
contenttype: 'text/plain',
messagetype,
voiceeventsid,
}
});
});
});

describe('destroy', () => {
it('should return this', () => {
assert.equal(pstream.destroy(), pstream);
Expand Down Expand Up @@ -315,24 +356,19 @@ describe('PStream', () => {
]],
['invite', [
{
args: ['bar', 'foo', true, '', []],
payload: { callsid: 'foo', sdp: 'bar', preflight: true, twilio: {}, registerFor: [] },
args: ['bar', 'foo', true, ''],
payload: { callsid: 'foo', sdp: 'bar', preflight: true, twilio: {} },
scenario: 'called with empty params'
},
{
args: ['bar', 'foo', true, 'baz=zee&foo=2', []],
payload: { callsid: 'foo', sdp: 'bar', preflight: true, twilio: { params: 'baz=zee&foo=2' }, registerFor: [] },
args: ['bar', 'foo', true, 'baz=zee&foo=2'],
payload: { callsid: 'foo', sdp: 'bar', preflight: true, twilio: { params: 'baz=zee&foo=2' } },
scenario: 'called with non-empty params'
},
{
args: ['bar', 'foo', false, '', []],
payload: { callsid: 'foo', sdp: 'bar', preflight: false, twilio: {}, registerFor: [] },
args: ['bar', 'foo', false, ''],
payload: { callsid: 'foo', sdp: 'bar', preflight: false, twilio: {} },
scenario: 'called with preflight = false'
},
{
args: ['bar', 'foo', false, '', ['registerFor-foo', 'registerFor-bar']],
payload: { callsid: 'foo', sdp: 'bar', preflight: false, twilio: {}, registerFor: ['registerFor-foo', 'registerFor-bar'] },
scenario: 'passing events to "registerFor"'
}
]],
['answer', [
Expand Down
Loading

0 comments on commit cbcd116

Please sign in to comment.