Skip to content

Commit

Permalink
chore: Separated tests to browser and headless
Browse files Browse the repository at this point in the history
  • Loading branch information
TimSusa committed May 11, 2019
1 parent dae3797 commit 6b98be4
Show file tree
Hide file tree
Showing 18 changed files with 1,927 additions and 214 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
.nyc_output
.nyc_output
.DS_Store
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
"name": "webmidi",
"version": "2.3.3",
"description": "WebMidi.js helps you tame the Web MIDI API. Send and receive MIDI messages with ease. Control instruments with user-friendly functions (playNote, sendPitchBend, etc.). React to MIDI input with simple event listeners (noteon, pitchbend, controlchange, etc.).",
"author": "Jean-Philippe Côté <jp@djip.co>",
"author": "Jean-Philippe Côté <jp@cote.cc>",
"main": "webmidi.min.js",
"types": "webmidi.d.ts",
"scripts": {
"test": "BROWSER=NONE mocha ./test/headless --exit",
"test-all": "npm run types && npm run lint && npm run test-coverage",
"test-coverage": "nyc --reporter=text npm run test",
"build": "grunt uglify",
"generate-doc": "grunt yuidoc",
"gh-pages": "grunt gh-pages",
"lint": "eslint ./src/*.js",
"release": "grunt release",
"test": "mocha ./test --exit",
"test-all": "npm run types && npm run test-coverage",
"test-coverage": "nyc --reporter=text npm run test",
"types": "tsc --strict --noEmit ./webmidi.d.ts"
},
"repository": {
Expand Down Expand Up @@ -51,7 +51,7 @@
"grunt-git": "^1.0.13",
"grunt-release": "^0.14.0",
"jzz": "^0.7.5",
"midi-test": "^1.0.3",
"midi-test": "^1.0.2",
"mocha": "^6.1.4",
"nyc": "^14.1.0",
"sinon": "^7.3.2",
Expand Down
3 changes: 3 additions & 0 deletions test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
.nyc_output
.DS_Strore
63 changes: 63 additions & 0 deletions test/browser/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">
<title>WebMidi.js Unit Tests</title>
<link rel="stylesheet" href="../../node_modules/mocha/mocha.css" />

<style>
#mocha > h1 {
font-weight: bold;
font-size: 1.5em;
}
#mocha-report > li {
padding-bottom: 1em;
border-bottom: 1px dotted gray;
}
</style>

</head>

<body>

<!-- Unit tests output container -->
<div id="mocha">

<h1>WebMidi.js Unit Tests</h1>

<!-- Jazz Plugin for browsers that do not support Web MIDI-->
<object id="Jazz1" classid="CLSID:1ACE1618-1C7D-4561-AEE1-34842AA85E90" class="hidden">
<object id="Jazz2" type="audio/x-jazz" class="hidden">
<p><a href=http://jazz-soft.net>Jazz-Plugin</a> required!</p>
</object>
</object>

</div>

<!-- WebMIDIAPIShim + some custom utils -->
<script src="../libs/WebMIDIAPIShim.js"></script>
<script src="../libs/Utils.js"></script>

<!-- Sinon, Chai & Mocha -->
<script src="../../node_modules/sinon-browser-only/sinon.js"></script>
<script src="../../node_modules/chai/chai.js"></script>
<script src="../../node_modules/mocha/mocha.js"></script>

<!-- WebMidi -->
<script src="../../src/webmidi.js"></script>

<!-- Tests -->
<script>var expect = chai.expect;</script>
<script>mocha.setup('bdd');</script>
<script src="platform.js"></script>
<script src="webmidi.js"></script>
<script src="input.js"></script>
<script src="output.js"></script>
<script>mocha.run();</script>

</body>

</html>
270 changes: 270 additions & 0 deletions test/browser/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
describe('Input', function() {

beforeEach("Enable WebMidi.js", function (done) {

WebMidi.disable();

WebMidi.enable(function() {

if (WebMidi.inputs.length > 0) {
done();
} else {

// Calling this.skip() throws an error. We catch it so it does not up show in the console
try {
this.skip();
} catch (err) {
// console.warn(err.message)
}

}

}.bind(this));

});

describe('addListener()', function() {

it("should throw error if listener is not a function", function() {

expect(function () {
WebMidi.inputs[0].addListener('noteon');
}).to.throw(TypeError);

});

it("should throw error if type is invalid", function() {

['prout', '', undefined, null, function() {}].forEach(function (param) {
expect(function () {
WebMidi.inputs[0].addListener(param, 1, function() {});
}).to.throw(TypeError);
});

});

it("should throw error if channel is invalid", function() {

expect(function () {
WebMidi.inputs[0].addListener('noteon', "abc", function() {});
}).to.throw(RangeError);

expect(function () {
WebMidi.inputs[0].addListener('noteon', 123, function() {});
}).to.throw(RangeError);

});

it("should actually add the listener(s)", function() {

function a() {}
function b() {}

WebMidi.inputs[0].addListener('noteoff', 10, a);
expect(WebMidi.inputs[0].hasListener('noteoff', 10, a)).to.equal(true);

WebMidi.inputs[0].addListener('noteon', 10, a);
expect(WebMidi.inputs[0].hasListener('noteon', 10, b)).to.equal(false);

WebMidi.inputs[0].addListener('keyaftertouch', "all", a);
expect(WebMidi.inputs[0].hasListener('keyaftertouch', 3, a)).to.equal(true);

WebMidi.inputs[0].addListener('controlchange', [1, 2, 3], a);
expect(WebMidi.inputs[0].hasListener('controlchange', 3, a)).to.equal(true);
expect(WebMidi.inputs[0].hasListener('controlchange', [1, 3], a)).to.equal(true);
expect(WebMidi.inputs[0].hasListener('controlchange', [1, 2, 3], a)).to.equal(true);
expect(WebMidi.inputs[0].hasListener('controlchange', "all", a)).to.equal(false);

WebMidi.inputs[0].addListener('channelmode', "all", a);
expect(WebMidi.inputs[0].hasListener('channelmode', 3, a)).to.equal(true);
expect(WebMidi.inputs[0].hasListener('channelmode', "all", a)).to.equal(true);
expect(WebMidi.inputs[0].hasListener('channelmode', "all", b)).to.equal(false);
expect(WebMidi.inputs[0].hasListener('channelmode', 5, a)).to.equal(true);

});

});

describe('hasListener()', function() {

it("should throw error if listener is not a function", function(done) {

['prout', undefined, null].forEach(function (param) {
expect(function () {
WebMidi.inputs[0].hasListener('noteon', param);
}).to.throw(TypeError);
});

done();

});

it("should correctly check for listeners", function(done) {

function a() {}
function b() {}

WebMidi.inputs[0].addListener('noteoff', 10, a);
expect(WebMidi.inputs[0].hasListener('noteoff', 10, a)).to.equal(true);

WebMidi.inputs[0].addListener('noteon', 10, a);
expect(WebMidi.inputs[0].hasListener('noteon', 10, b)).to.equal(false);

WebMidi.inputs[0].addListener('keyaftertouch', "all", a);
expect(WebMidi.inputs[0].hasListener('keyaftertouch', 3, a)).to.equal(true);

WebMidi.inputs[0].addListener('controlchange', [1, 2, 3], a);
expect(WebMidi.inputs[0].hasListener('controlchange', 3, a)).to.equal(true);
expect(WebMidi.inputs[0].hasListener('controlchange', [1, 3], a)).to.equal(true);
expect(WebMidi.inputs[0].hasListener('controlchange', [1, 2, 3], a)).to.equal(true);
expect(WebMidi.inputs[0].hasListener('controlchange', "all", a)).to.equal(false);

WebMidi.inputs[0].addListener('channelmode', "all", a);
expect(WebMidi.inputs[0].hasListener('channelmode', 3, a)).to.equal(true);
expect(WebMidi.inputs[0].hasListener('channelmode', "all", a)).to.equal(true);
expect(WebMidi.inputs[0].hasListener('channelmode', "all", b)).to.equal(false);
expect(WebMidi.inputs[0].hasListener('channelmode', "all", a)).to.equal(true);

done();

});

});

describe('removeListener()', function() {

it("should throw error if type is not supported", function() {

['xxx', null].forEach(function (param) {
expect(function () {
WebMidi.inputs[0].removeListener(param);
}).to.throw(TypeError);
});

});

it("should throw error if listener is defined but not a function", function() {

['prout', null, {}].forEach(function (param) {
expect(function () {
WebMidi.inputs[0].removeListener("start", "all", param);
}).to.throw(TypeError);
});

});

it("should throw error if filters param is defined but not an object", function() {

['prout', undefined, null, 123].forEach(function (param) {
expect(function () {
WebMidi.inputs[0].removeListener("start", function() {}, 123);
}).to.throw(TypeError);
});

});

it("should correctly remove listeners", function() {

function a() {}
function b() {}

WebMidi.inputs[0].addListener('programchange', 10, a);
WebMidi.inputs[0].removeListener('programchange', 10, a);
expect(WebMidi.inputs[0].hasListener('programchange', 10, a)).to.equal(false);

WebMidi.inputs[0].addListener('sysex', undefined, a);
WebMidi.inputs[0].removeListener('sysex', undefined, a);
expect(WebMidi.inputs[0].hasListener('sysex', undefined, a)).to.equal(false);

WebMidi.inputs[0].addListener('channelaftertouch', 10, a);
WebMidi.inputs[0].removeListener('channelaftertouch', "all", a);
expect(WebMidi.inputs[0].hasListener('channelaftertouch', 10, a)).to.equal(false);

WebMidi.inputs[0].addListener('pitchbend', "all", a);
WebMidi.inputs[0].removeListener('pitchbend', 10, a);
expect(WebMidi.inputs[0].hasListener('pitchbend', 10, a)).to.equal(false);
expect(WebMidi.inputs[0].hasListener('pitchbend', 11, a)).to.equal(true);

WebMidi.inputs[0].addListener('noteoff', 'all', a);
WebMidi.inputs[0].addListener('noteoff', "all", b);
WebMidi.inputs[0].removeListener('noteoff');
expect(WebMidi.inputs[0].hasListener('noteoff', 10, a)).to.equal(false);
expect(WebMidi.inputs[0].hasListener('noteoff', 11, b)).to.equal(false);

WebMidi.inputs[0].addListener('keyaftertouch', "all", a);
WebMidi.inputs[0].addListener('keyaftertouch', "all", b);
WebMidi.inputs[0].removeListener();
expect(WebMidi.inputs[0].hasListener('keyaftertouch', 10, a)).to.equal(false);
expect(WebMidi.inputs[0].hasListener('keyaftertouch', "all", b)).to.equal(false);

WebMidi.inputs[0].addListener('stop', "all", a);
WebMidi.inputs[0].addListener('stop', "all", b);
WebMidi.inputs[0].removeListener('stop');
expect(WebMidi.inputs[0].hasListener('stop', "all", a)).to.equal(false);
expect(WebMidi.inputs[0].hasListener('stop', "all", b)).to.equal(false);

});

});

describe('getCcNameByNumber()', function() {

it("should throw an error when an invalid CC number is provided", function () {

[-1, 120, function() {}].forEach(function (param) {
expect(function () {
WebMidi.inputs[0].getCcNameByNumber(param);
}).to.throw(RangeError);
});

});

it("should return undefined when there is no defined name for a valid number", function () {
expect(WebMidi.inputs[0].getCcNameByNumber(3)).to.equal(undefined);
});

it("should return correct name", function (done) {

for (var key in WebMidi.MIDI_CONTROL_CHANGE_MESSAGES) {
if (WebMidi.MIDI_CONTROL_CHANGE_MESSAGES.hasOwnProperty(key)) {
expect(
WebMidi.inputs[0].getCcNameByNumber(WebMidi.MIDI_CONTROL_CHANGE_MESSAGES[key])
).to.equal(key);
}
}

done();

});

});

describe('getChannelModeByNumber()', function() {

it("should throw an error when an invalid channel mode number is provided", function () {

expect(function () {

[-1, 0, 119, 128, undefined, null, function() {}].forEach(function (param) {
WebMidi.inputs[0].getChannelModeByNumber(param);
});

}).to.throw(RangeError);

});

it("should return correct channel mode name", function () {

for (var key in WebMidi.MIDI_CHANNEL_MODE_MESSAGES) {
if (WebMidi.MIDI_CHANNEL_MODE_MESSAGES.hasOwnProperty(key)) {
expect(
WebMidi.inputs[0].getChannelModeByNumber(WebMidi.MIDI_CHANNEL_MODE_MESSAGES[key])
).to.equal(key);
}
}

});

});

});
Loading

0 comments on commit 6b98be4

Please sign in to comment.