Skip to content
This repository has been archived by the owner on Jul 29, 2019. It is now read-only.

Adding unit tests for lib/shared #3600

Merged
merged 22 commits into from
Nov 4, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/shared/Configurator.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ class Configurator {
this.options.filter = options.join();
}
else if (typeof options === 'object') {
if (options == null)
{
if (options == null) {
throw new TypeError('options cannot be null');
}
if (options.container !== undefined) {
Expand Down
30 changes: 29 additions & 1 deletion test/ColorPicker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ describe('ColorPicker', function () {
it('prevents non-functions from being set as callback', function () {
var colorPicker = new ColorPicker();
assert.throws(function () {colorPicker.setUpdateCallback(null);}, Error, null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm inclined to add the usual suspects here: 'null, undefined, [1,2,3], {a: 42}, 42, "meow"`.

Actually, I would consider being able to undo a callback (with null and/or undefined) a feature here.

Same applies to setCloseCallback().

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed

assert.throws(function () {colorPicker.setUpdateCallback(undefined);}, Error, null);
assert.throws(function () {colorPicker.setUpdateCallback([1, 2, 3]);}, Error, null);
assert.throws(function () {colorPicker.setUpdateCallback({a: 42});}, Error, null);
assert.throws(function () {colorPicker.setUpdateCallback(42);}, Error, null);
assert.throws(function () {colorPicker.setUpdateCallback('meow');}, Error, null);
});
});

Expand All @@ -67,6 +72,11 @@ describe('ColorPicker', function () {
it('prevents non-functions from being set as callback', function () {
var colorPicker = new ColorPicker();
assert.throws(function () {colorPicker.setCloseCallback(null);}, Error, null);
assert.throws(function () {colorPicker.setCloseCallback(undefined);}, Error, null);
assert.throws(function () {colorPicker.setCloseCallback([1, 2, 3]);}, Error, null);
assert.throws(function () {colorPicker.setCloseCallback({a: 42});}, Error, null);
assert.throws(function () {colorPicker.setCloseCallback(42);}, Error, null);
assert.throws(function () {colorPicker.setCloseCallback('meow');}, Error, null);
});
});

Expand All @@ -75,19 +85,29 @@ describe('ColorPicker', function () {
it('runs updateCallback when applied', function () {
var callback = sinon.spy();
var colorPicker = new ColorPicker();
var colorBeforeHide = colorPicker.color;
colorPicker.setUpdateCallback(callback);
colorPicker.applied = true;
colorPicker._hide();
assert.equal(callback.callCount, 1);
assert.deepEqual(colorBeforeHide, colorPicker.previousColor);
});

it('does not run updateCallback when not applied', function () {
var callback = sinon.spy();
var colorPicker = new ColorPicker();
var colorBeforeHide = colorPicker.color;
colorPicker.setUpdateCallback(callback);
colorPicker.applied = false;
colorPicker._hide();
assert.equal(callback.callCount, 0);
assert.deepEqual(colorBeforeHide, colorPicker.previousColor);
});

it('does not set previous color when storePrevious is false', function () {
var colorPicker = new ColorPicker();
colorPicker._hide(false);
assert.deepEqual(colorPicker.previousColor, undefined);
});
});

Expand All @@ -114,9 +134,14 @@ describe('ColorPicker', function () {
assert.deepEqual(colorPicker.color, { r: 255, g: 255, b: 255, a: 1 });
});

it('throws error when color is null', function () {
it('throws error when color is a bad value', function () {
var colorPicker = new ColorPicker();
assert.throws(function () {colorPicker.setColor(null);}, Error, null);
assert.throws(function () {colorPicker.setColor(undefined);}, Error, null);
assert.throws(function () {colorPicker.setColor([1, 2, 3]);}, Error, null);
assert.throws(function () {colorPicker.setColor({a: 42});}, Error, null);
assert.throws(function () {colorPicker.setColor(42);}, Error, null);
assert.throws(function () {colorPicker.setColor('meow');}, Error, null);
});

it('handles html color string', function () {
Expand Down Expand Up @@ -165,15 +190,18 @@ describe('ColorPicker', function () {
colorPicker.show();
assert(callback.called);
assert(callback.calledOnce);
assert(colorPicker.generated)
});

it('resets applied state and frame display style to `block`', function () {
var colorPicker = new ColorPicker();
colorPicker.show();
assert.equal(colorPicker.applied, false);
assert.equal(colorPicker.frame.style.display, 'block');
assert(colorPicker.generated)
});
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On account of thoroughness (which you are, very), I would expect test it('generates the hue circle') here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're not going to do this?


describe('_save', function () {

it('triggers updateCallback', function () {
Expand Down
4 changes: 2 additions & 2 deletions test/Configurator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('Configurator', function () {

it('sets extends options with default options', function () {
var config = new Configurator();
assert.equal(JSON.stringify(config.options), JSON.stringify(config.defaultOptions));
assert.deepEqual(config.options, config.defaultOptions);
});
});

Expand All @@ -35,7 +35,7 @@ describe('Configurator', function () {
it('with undefined will not modify defaults', function () {
var config = new Configurator(Network, this.container);
config.setOptions();
assert.equal(JSON.stringify(config.options), JSON.stringify(config.defaultOptions));
assert.deepEqual(config.options, config.defaultOptions);
});

it('with undefined will set enabled to false', function () {
Expand Down
40 changes: 8 additions & 32 deletions test/DataSet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ describe('DataSet', function () {
assert.equal(minValue, null);
});

it('ignores undefined values', function () {
it('handles undefined values', function () {
var dataset = new DataSet([{id: undefined}, {id: 1}, {id: 2}, {id: 3}]);
var minValue = dataset.min('id');
assert.deepEqual(minValue, {id: 1});
Expand All @@ -429,14 +429,9 @@ describe('DataSet', function () {
var minValue = dataset.min('id');
assert.deepEqual(minValue, {id: 10000000000000001});
});

xit('handles big values - but not really, because of javascript number handling', function () {
var dataset = new DataSet([{id: -10000000000000001}, {id: -10000000000000002}, {id: -10000000000000003}]);
var minValue = dataset.min('id');
assert.deepEqual(minValue, {id: -10000000000000003});
assert.equal('' + minValue.id, '-10000000000000003')
});
});


describe('max', function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newline (I usually do 2 here)


it('finds the maximum value', function () {
Expand All @@ -451,24 +446,11 @@ describe('DataSet', function () {
assert.equal(maxValue, null);
});

it('ignores undefined values', function () {
it('handles undefined values', function () {
var dataset = new DataSet([{id: undefined}, {id: 1}, {id: 2}, {id: 3}]);
var maxValue = dataset.max('id');
assert.deepEqual(maxValue, {id: 3});
});

xit('handles big values - but not really, because of javascript number handling', function () {
var dataset = new DataSet([{id: 10000000000000001}, {id: 10000000000000002}, {id: 10000000000000003}]);
var maxValue = dataset.max('id');
assert.deepEqual(maxValue, {id: 10000000000000003});
assert.equal('' + maxValue.id, '10000000000000003')
});

xit('handles negative values - This does not work because of javascript', function () {
var dataset = new DataSet([{id: -10000000000000001}, {id: -10000000000000002}, {id: -10000000000000003}]);
var maxValue = dataset.max('id');
assert.deepEqual(maxValue, {id: -10000000000000001});
});
});

describe('distinct', function () {
Expand All @@ -485,22 +467,16 @@ describe('DataSet', function () {
assert.deepEqual(distinctValues, []);
});

it('ignores undefined values', function () {
it('handles undefined values', function () {
var dataset = new DataSet([{val: undefined}, {val: 1}, {val: 2}, {val: 3}]);
var distinctValues = dataset.distinct('val');
assert.deepEqual(distinctValues, [1, 2, 3]);
});

xit('handles big values - but not really, because of javascript number handling', function () {
var dataset = new DataSet([{val: 10000000000000001}, {val: 10000000000000002}, {val: 10000000000000003}, {val: 10000000000000004}]);
it('handles duplicate values', function () {
var dataset = new DataSet([{val: 1}, {val: 1}, {val: 2}, {val: 3}]);
var distinctValues = dataset.distinct('val');
assert.deepEqual(distinctValues, [ 10000000000000000, 10000000000000002, 10000000000000003, 10000000000000004]);
});

xit('handles negative values - This does not work because of javascript', function () {
var dataset = new DataSet([{val: -10000000000000001}, {val: -10000000000000002}, {val: -10000000000000003}, {val: -10000000000000004}]);
var distinctValues = dataset.distinct('val');
assert.deepEqual(distinctValues, [ -10000000000000000, -10000000000000002, -10000000000000003, -10000000000000004]);
assert.deepEqual(distinctValues, [1, 2, 3]);
});
});

Expand Down
Loading