-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Adding unit tests for lib/shared #3600
Changes from 17 commits
b450758
b50ddc5
bbcaa78
6c8cf80
294525c
319fced
989bfd2
3aae9fc
a56cd13
4b766a5
e25d2e2
4c68bae
139bc86
429b63c
6288a41
645fc91
c19e524
f81eef5
031d640
0f8455f
8303487
388edb5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,25 +30,6 @@ exports.recursiveDOMDelete = function (DOMobject) { | |
} | ||
}; | ||
|
||
/** | ||
* this function gives you a range between 0 and 1 based on the min and max values in the set, the total sum of all values and the current value. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. RIP, you useless code! |
||
* | ||
* @param {number} min | ||
* @param {number} max | ||
* @param {number} total | ||
* @param {number} value | ||
* @returns {number} | ||
*/ | ||
exports.giveRange = function (min, max, total, value) { | ||
if (max == min) { | ||
return 0.5; | ||
} | ||
else { | ||
var scale = 1 / (max - min); | ||
return Math.max(0, (value - min) * scale); | ||
} | ||
}; | ||
|
||
/** | ||
* Test whether given object is a string | ||
* @param {*} object | ||
|
@@ -82,29 +63,13 @@ exports.isDate = function (object) { | |
}; | ||
|
||
/** | ||
* Create a semi UUID | ||
* source: http://stackoverflow.com/a/105074/1262753 | ||
* Create a UUID | ||
* @return {string} uuid | ||
*/ | ||
exports.randomUUID = function () { | ||
return uuid.v4(); | ||
}; | ||
|
||
/** | ||
* assign all keys of an object that are not nested objects to a certain value (used for color objects). | ||
* @param {object} obj | ||
* @param {number} value | ||
*/ | ||
exports.assignAllKeys = function (obj, value) { | ||
for (var prop in obj) { | ||
if (obj.hasOwnProperty(prop)) { | ||
if (typeof obj[prop] !== 'object') { | ||
obj[prop] = value; | ||
} | ||
} | ||
} | ||
}; | ||
|
||
|
||
/** | ||
* Copy property from b to a if property present in a. | ||
|
@@ -1272,7 +1237,7 @@ exports.insertSort = function (a,compare) { | |
a[j] = k; | ||
} | ||
return a; | ||
} | ||
}; | ||
|
||
|
||
/** | ||
|
@@ -1592,8 +1557,6 @@ exports.topMost = function (pile, accessors) { | |
for (let i = 1; i < accessors.length; i++){ | ||
if (candidate) { | ||
candidate = candidate[accessors[i]] | ||
} else { | ||
continue; | ||
} | ||
} | ||
if (typeof candidate != 'undefined') { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
var assert = require('assert'); | ||
var sinon = require('sinon'); | ||
var jsdom_global = require('jsdom-global'); | ||
|
||
var canvasMockify = require('./canvas-mock'); | ||
var Activator = require('../lib/shared/Activator'); | ||
|
||
|
||
describe('Activator', function () { | ||
beforeEach(function() { | ||
this.jsdom_global = jsdom_global( | ||
"<div id='mynetwork'></div>", | ||
{ skipWindowCheck: true} | ||
); | ||
canvasMockify(window); | ||
this.container = document.getElementById('mynetwork'); | ||
}); | ||
|
||
afterEach(function() { | ||
this.jsdom_global(); | ||
this.container.remove(); | ||
this.container = undefined; | ||
}); | ||
|
||
describe('constructor', function () { | ||
|
||
it('sets defaults', function () { | ||
var activator = new Activator(this.container); | ||
assert.equal(activator.active, false); | ||
}); | ||
|
||
it('creates overlay', function () { | ||
var activator = new Activator(this.container); | ||
assert.equal(activator.dom.container.children[0].className, 'vis-overlay'); | ||
}); | ||
}); | ||
|
||
describe('activate', function () { | ||
it('emits an `activate` event', function () { | ||
var eventSpy = sinon.spy(); | ||
var activator = new Activator(this.container); | ||
activator.on('activate', eventSpy); | ||
activator.activate(); | ||
assert.equal(activator.active, true); | ||
assert(eventSpy.called, 'Event did not fire.'); | ||
assert(eventSpy.calledOnce, 'Event fired more than once'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I sort of half understand what There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case |
||
}); | ||
|
||
it('emits a `change` event', function () { | ||
var eventSpy = sinon.spy(); | ||
var activator = new Activator(this.container); | ||
activator.on('change', eventSpy); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't help noticing in the code that What about consecutive same calls, e.g. If this is not documented, I would consider this faultly. Please give it some thought. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure what to do about this, but would also consider it outside the scope of this particular issue. I agree with you, but don't know the impact of making corrective changes. Would you like me to open an issue to investigate? |
||
activator.activate(); | ||
assert.equal(activator.active, true); | ||
assert(eventSpy.called, 'Event did not fire.'); | ||
assert(eventSpy.calledOnce, 'Event fired more than once'); | ||
}); | ||
}); | ||
|
||
describe('deactivate', function () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Describe-block is practically identical to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think drying it would muddy the test state, or make the tests more complex. |
||
it('emits a `deactivate` event', function () { | ||
var eventSpy = sinon.spy(); | ||
var activator = new Activator(this.container); | ||
activator.on('deactivate', eventSpy); | ||
activator.deactivate(); | ||
assert.equal(activator.active, false); | ||
assert(eventSpy.called, 'Event did not fire.'); | ||
assert(eventSpy.calledOnce, 'Event fired more than once'); | ||
}); | ||
|
||
it('emits a `change` event', function () { | ||
var eventSpy = sinon.spy(); | ||
var activator = new Activator(this.container); | ||
activator.on('change', eventSpy); | ||
activator.deactivate(); | ||
assert.equal(activator.active, false); | ||
assert(eventSpy.called, 'Event did not fire.'); | ||
assert(eventSpy.calledOnce, 'Event fired more than once'); | ||
}); | ||
}); | ||
|
||
describe('destroy', function () { | ||
|
||
it('sets inactive, removes keycharm, and removes hammer', function () { | ||
var activator = new Activator(this.container); | ||
activator.destroy(); | ||
assert.equal(activator.active, false); | ||
assert.equal(activator.keycharm, null); | ||
assert.equal(activator.hammer, null); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd really prefer it if the brace positioning is the same as the surrounding code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch BTW. It's bitten me before that
typeof null === 'object'
. This should actually be checked in the entire code.