Skip to content

Commit

Permalink
Merge pull request #1138 from webrtcHacks/jest
Browse files Browse the repository at this point in the history
Move unit tests to jest
  • Loading branch information
fippo authored Sep 19, 2023
2 parents 873a68d + d326c5c commit 28c9f94
Show file tree
Hide file tree
Showing 13 changed files with 218 additions and 258 deletions.
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"prepare": "grunt build",
"prepublishonly": "npm test",
"test": "grunt && mocha test/unit && karma start test/karma.conf.js",
"lint-and-unit-tests": "grunt && mocha test/unit",
"lint-and-unit-tests": "grunt && jest test/unit",
"e2e-tests": "grunt && karma start test/karma.conf.js"
},
"dependencies": {
Expand All @@ -37,14 +37,15 @@
"@puppeteer/browsers": "^1.4.1",
"babel-preset-env": "^1.7.0",
"brfs": "^1.5.0",
"chai": "^3.5.0",
"eslint-plugin-jest": "^27.4.0",
"grunt": "^1.1.0",
"grunt-babel": "^8.0.0",
"grunt-browserify": "^6.0.0",
"grunt-cli": "^1.3.1",
"grunt-contrib-clean": "^1.1.0",
"grunt-contrib-copy": "^1.0.0",
"grunt-eslint": "^24.0.0",
"jest": "^29.7.0",
"karma": "^6.4.1",
"karma-browserify": "^8.1.0",
"karma-chai": "^0.1.0",
Expand All @@ -54,8 +55,6 @@
"karma-mocha-reporter": "^2.2.3",
"karma-safari-launcher": "^1.0.0",
"karma-stability-reporter": "^3.0.1",
"mocha": "^10.1.0",
"sinon": "^2.2.0",
"sinon-chai": "^2.14.0"
"mocha": "^10.2.0"
}
}
4 changes: 2 additions & 2 deletions test/unit/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"env": {
"mocha": true,
"jest": true,
"browser": true
},
"rules": {}
"plugins": ["jest"]
}
17 changes: 5 additions & 12 deletions test/unit/adapter_factory.js → test/unit/adapterfactory.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,30 @@
* that can be found in the LICENSE file in the root of the source
* tree.
*/
/* eslint-env node */
const chai = require('chai');
const expect = chai.expect;
const sinon = require('sinon');
const sinonChai = require('sinon-chai');
chai.use(sinonChai);

describe('adapter factory', () => {
const {adapterFactory} = require('../../dist/adapter_factory.js');
const utils = require('../../dist/utils.js');

let window;
beforeEach(() => {
window = {
RTCPeerConnection: sinon.stub(),
RTCPeerConnection: jest.fn(),
};
});

describe('does not shim', () => {
afterEach(() => {
utils.detectBrowser.restore();
utils.detectBrowser.mockRestore();
});
['Chrome', 'Firefox', 'Safari'].forEach(browser => {
it(browser + ' when disabled', () => {
sinon.stub(utils, 'detectBrowser').returns({
jest.spyOn(utils, 'detectBrowser').mockReturnValue({
browser: browser.toLowerCase()
});
let options = {};
options['shim' + browser] = false;
const adapter = adapterFactory(window, options);
expect(adapter).not.to.have.property('browserShim');
expect(adapter).not.toHaveProperty('browserShim');
});
});
});
Expand All @@ -48,6 +41,6 @@ describe('adapter factory', () => {
'Gecko/20100101 Firefox/44.0'
}};
const constructor = () => adapterFactory({window});
expect(constructor).not.to.throw();
expect(constructor).not.toThrow();
});
});
32 changes: 13 additions & 19 deletions test/unit/addicecandidate.js → test/unit/addicecandidate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,33 @@
* that can be found in the LICENSE file in the root of the source
* tree.
*/
/* eslint-env node */
const chai = require('chai');
const expect = chai.expect;
const sinon = require('sinon');
const sinonChai = require('sinon-chai');
chai.use(sinonChai);

describe('addIceCandidate with null or empty candidate', () => {
const shim = require('../../dist/common_shim');
let window;
let origAddIceCandidate;
beforeEach(() => {
window = {
RTCPeerConnection: sinon.stub(),
RTCPeerConnection: jest.fn(),
};
origAddIceCandidate = sinon.stub();
origAddIceCandidate = jest.fn();
window.RTCPeerConnection.prototype.addIceCandidate = origAddIceCandidate;
});

describe('does nothing if', () => {
it('RTCPeerConnection is not defined', () => {
expect(() => shim.shimAddIceCandidateNullOrEmpty({}, {})).not.to.throw();
expect(() => shim.shimAddIceCandidateNullOrEmpty({}, {})).not.toThrow();
});
it('RTCPeerConnection.prototype.addIceCandidate is undefined', () => {
window.RTCPeerConnection.prototype.addIceCandidate = null;
expect(() => shim.shimAddIceCandidateNullOrEmpty({}, {})).not.to.throw();
expect(() => shim.shimAddIceCandidateNullOrEmpty({}, {})).not.toThrow();
});
it('the candidate argument is optional', () => {
expect(window.RTCPeerConnection.prototype.addIceCandidate.length)
.to.equal(0);
.toBe(0);
shim.shimAddIceCandidateNullOrEmpty({}, {});
expect(window.RTCPeerConnection.prototype.addIceCandidate)
.to.equal(origAddIceCandidate);
.toBe(origAddIceCandidate);
});
});

Expand All @@ -46,7 +40,7 @@ describe('addIceCandidate with null or empty candidate', () => {
(candidate) => origAddIceCandidate(candidate);
shim.shimAddIceCandidateNullOrEmpty(window, {});
expect(window.RTCPeerConnection.prototype.addIceCandidate.length)
.to.equal(0);
.toBe(0);
});

it('ignores addIceCandidate(null)', () => {
Expand All @@ -55,7 +49,7 @@ describe('addIceCandidate with null or empty candidate', () => {
shim.shimAddIceCandidateNullOrEmpty(window, {});
const pc = new window.RTCPeerConnection();
pc.addIceCandidate({candidate: '', sdpMLineIndex: 0});
expect(origAddIceCandidate.callCount).to.equal(1);
expect(origAddIceCandidate.mock.calls.length).toBe(1);
});

describe('Chrome behaviour', () => {
Expand All @@ -73,7 +67,7 @@ describe('addIceCandidate with null or empty candidate', () => {

const pc = new window.RTCPeerConnection();
pc.addIceCandidate({candidate: '', sdpMLineIndex: 0});
expect(origAddIceCandidate.callCount).to.equal(0);
expect(origAddIceCandidate.mock.calls.length).toBe(0);
});

it('passes {candidate: ""} after Chrome 78', () => {
Expand All @@ -82,7 +76,7 @@ describe('addIceCandidate with null or empty candidate', () => {

const pc = new window.RTCPeerConnection();
pc.addIceCandidate({candidate: '', sdpMLineIndex: 0});
expect(origAddIceCandidate.callCount).to.equal(1);
expect(origAddIceCandidate.mock.calls.length).toBe(1);
});
});

Expand All @@ -101,7 +95,7 @@ describe('addIceCandidate with null or empty candidate', () => {

const pc = new window.RTCPeerConnection();
pc.addIceCandidate({candidate: '', sdpMLineIndex: 0});
expect(origAddIceCandidate.callCount).to.equal(0);
expect(origAddIceCandidate.mock.calls.length).toBe(0);
});

it('passes {candidate: ""} after Firefox 68', () => {
Expand All @@ -110,7 +104,7 @@ describe('addIceCandidate with null or empty candidate', () => {

const pc = new window.RTCPeerConnection();
pc.addIceCandidate({candidate: '', sdpMLineIndex: 0});
expect(origAddIceCandidate.callCount).to.equal(1);
expect(origAddIceCandidate.mock.calls.length).toBe(1);
});
});

Expand All @@ -128,7 +122,7 @@ describe('addIceCandidate with null or empty candidate', () => {

const pc = new window.RTCPeerConnection();
pc.addIceCandidate({candidate: '', sdpMLineIndex: 0});
expect(origAddIceCandidate.callCount).to.equal(0);
expect(origAddIceCandidate.mock.calls.length).toBe(0);
});
});
});
99 changes: 55 additions & 44 deletions test/unit/chrome.js → test/unit/chrome.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@
* that can be found in the LICENSE file in the root of the source
* tree.
*/
/* eslint-env node */
const chai = require('chai');
const expect = chai.expect;
const sinon = require('sinon');
const sinonChai = require('sinon-chai');
chai.use(sinonChai);

/* a mock of the Chrome RTCLegacyStatReport */
function RTCLegacyStatsReport() {
Expand Down Expand Up @@ -41,7 +35,7 @@ describe('Chrome shim', () => {
window = {
navigator: {
mediaDevices: {
getUserMedia: sinon.stub().returns(Promise.resolve('stream')),
getUserMedia: jest.fn().mockReturnValue(Promise.resolve('stream')),
},
},
RTCPeerConnection: function() {}
Expand Down Expand Up @@ -74,20 +68,20 @@ describe('Chrome shim', () => {

it('returns chrome legacy getStats when called with a callback', (done) => {
pc.getStats((result) => {
expect(result).to.have.property('result');
expect(result).toHaveProperty('result');
const report = result.result()[0];
expect(report).to.have.property('id');
expect(report).to.have.property('type');
expect(report).to.have.property('timestamp');
expect(report).to.have.property('stat');
expect(report).toHaveProperty('id');
expect(report).toHaveProperty('type');
expect(report).toHaveProperty('timestamp');
expect(report).toHaveProperty('stat');
done();
});
});

it('is translated into a Map', () => {
return pc.getStats()
.then(result => {
expect(result).to.be.a('Map');
expect(result).toBeInstanceOf(Map);
});
});
});
Expand Down Expand Up @@ -119,80 +113,97 @@ describe('Chrome shim', () => {
});

describe('getDisplayMedia shim', () => {
const getSourceId = sinon.stub().returns(Promise.resolve('abc'));
const getSourceId = jest.fn().mockReturnValue(Promise.resolve('abc'));

it('does not overwrite an existing ' +
'navigator.mediaDevices.getDisplayMedia', () => {
window.navigator.mediaDevices.getDisplayMedia = 'foo';
shim.shimGetDisplayMedia(window, getSourceId);
expect(window.navigator.mediaDevices.getDisplayMedia).to.equal('foo');
expect(window.navigator.mediaDevices.getDisplayMedia).toBe('foo');
});

it('does not if navigator.mediaDevices does not exist', () => {
delete window.navigator.mediaDevices;
shim.shimGetDisplayMedia(window);
expect(window.navigator.mediaDevices).to.equal(undefined);
expect(window.navigator.mediaDevices).toBe(undefined);
});

it('shims navigator.mediaDevices.getDisplayMedia', () => {
shim.shimGetDisplayMedia(window, getSourceId);
expect(window.navigator.mediaDevices.getDisplayMedia).to.be.a('function');
expect(typeof window.navigator.mediaDevices.getDisplayMedia)
.toBe('function');
});

it('calls getUserMedia with the sourceId', () => {
it('calls getUserMedia with the sourceId', async() => {
shim.shimGetDisplayMedia(window, getSourceId);
return window.navigator.mediaDevices.getDisplayMedia({video: true})
.then(() => {
expect(window.navigator.mediaDevices.getUserMedia)
.to.have.been.calledWith({video: {mandatory: {
await window.navigator.mediaDevices.getDisplayMedia({video: true});
expect(window.navigator.mediaDevices.getUserMedia.mock.calls.length)
.toBe(1);
expect(window.navigator.mediaDevices.getUserMedia.mock.calls[0][0])
.toEqual({
video: {
mandatory: {
chromeMediaSource: 'desktop',
chromeMediaSourceId: 'abc',
maxFrameRate: 3,
}}});
}
}
});
});

it('translates frameRate to legacy maxFrameRate', () => {
it('translates frameRate to legacy maxFrameRate', async() => {
shim.shimGetDisplayMedia(window, getSourceId);
return window.navigator.mediaDevices
.getDisplayMedia({video: {frameRate: 25}})
.then(() => {
expect(window.navigator.mediaDevices.getUserMedia)
.to.have.been.calledWith({video: {mandatory: {
await window.navigator.mediaDevices
.getDisplayMedia({video: {frameRate: 25}});
expect(window.navigator.mediaDevices.getUserMedia.mock.calls.length)
.toBe(1);
expect(window.navigator.mediaDevices.getUserMedia.mock.calls[0][0])
.toEqual({
video: {
mandatory: {
chromeMediaSource: 'desktop',
chromeMediaSourceId: 'abc',
maxFrameRate: 25,
}}});
}
}
});
});

it('translates width to legacy maxWidth', () => {
it('translates width to legacy maxWidth', async() => {
shim.shimGetDisplayMedia(window, getSourceId);
return window.navigator.mediaDevices
.getDisplayMedia({video: {width: 640}})
.then(() => {
expect(window.navigator.mediaDevices.getUserMedia)
.to.have.been.calledWith({video: {mandatory: {
await window.navigator.mediaDevices
.getDisplayMedia({video: {width: 640}});
expect(window.navigator.mediaDevices.getUserMedia.mock.calls.length)
.toBe(1);
expect(window.navigator.mediaDevices.getUserMedia.mock.calls[0][0])
.toEqual({
video: {
mandatory: {
chromeMediaSource: 'desktop',
chromeMediaSourceId: 'abc',
maxFrameRate: 3,
maxWidth: 640,
}}});
}
}
});
});

it('translates height to legacy maxHeight', () => {
it('translates height to legacy maxHeight', async() => {
shim.shimGetDisplayMedia(window, getSourceId);
return window.navigator.mediaDevices
.getDisplayMedia({video: {height: 480}})
.then(() => {
expect(window.navigator.mediaDevices.getUserMedia)
.to.have.been.calledWith({video: {mandatory: {
await window.navigator.mediaDevices
.getDisplayMedia({video: {height: 480}});
expect(window.navigator.mediaDevices.getUserMedia.mock.calls.length)
.toBe(1);
expect(window.navigator.mediaDevices.getUserMedia.mock.calls[0][0])
.toEqual({
video: {
mandatory: {
chromeMediaSource: 'desktop',
chromeMediaSourceId: 'abc',
maxFrameRate: 3,
maxHeight: 480,
}}});
}
}
});
});
});
Expand Down
Loading

0 comments on commit 28c9f94

Please sign in to comment.