From f81eef53f3703b62f3fa38b4003b41b060211b01 Mon Sep 17 00:00:00 2001 From: MacLeod Broad <> Date: Tue, 24 Oct 2017 21:46:15 -0400 Subject: [PATCH] Address spacing, and unnecessary tests --- lib/shared/Configurator.js | 3 +-- test/ColorPicker.test.js | 30 +++++++++++++++++++++++- test/Configurator.test.js | 4 ++-- test/DataSet.test.js | 40 +++++++------------------------ test/util.test.js | 48 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 88 insertions(+), 37 deletions(-) diff --git a/lib/shared/Configurator.js b/lib/shared/Configurator.js index 4e40457b..0f973430 100644 --- a/lib/shared/Configurator.js +++ b/lib/shared/Configurator.js @@ -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) { diff --git a/test/ColorPicker.test.js b/test/ColorPicker.test.js index 6852179c..e96dd08c 100644 --- a/test/ColorPicker.test.js +++ b/test/ColorPicker.test.js @@ -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); + 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); }); }); @@ -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); }); }); @@ -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); }); }); @@ -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 () { @@ -165,6 +190,7 @@ 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 () { @@ -172,8 +198,10 @@ describe('ColorPicker', function () { colorPicker.show(); assert.equal(colorPicker.applied, false); assert.equal(colorPicker.frame.style.display, 'block'); + assert(colorPicker.generated) }); }); + describe('_save', function () { it('triggers updateCallback', function () { diff --git a/test/Configurator.test.js b/test/Configurator.test.js index c37ed574..a6fb587e 100644 --- a/test/Configurator.test.js +++ b/test/Configurator.test.js @@ -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); }); }); @@ -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 () { diff --git a/test/DataSet.test.js b/test/DataSet.test.js index 295f00a4..f3e1d6bd 100644 --- a/test/DataSet.test.js +++ b/test/DataSet.test.js @@ -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}); @@ -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 () { it('finds the maximum value', function () { @@ -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 () { @@ -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 undefined 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]); }); }); diff --git a/test/util.test.js b/test/util.test.js index 84c163db..43b7175a 100644 --- a/test/util.test.js +++ b/test/util.test.js @@ -635,49 +635,62 @@ describe('mergeOptions', function () { assert.throws(function () {util.convert({}, 'UnknownType');}, Error, null); }); }); + describe('getType', function () { + it('of object null is null', function () { assert.equal(util.getType(null), 'null'); }); + it('of object Boolean is Boolean', function () { function Tester () {} Tester.prototype = Object.create(Boolean.prototype); assert.equal(util.getType(new Tester('true')), 'Boolean'); }); + it('of object Number is Number', function () { function Tester () {} Tester.prototype = Object.create(Number.prototype); assert.equal(util.getType(new Tester(1)), 'Number'); }); + it('of object String is String', function () { function Tester () {} Tester.prototype = Object.create(String.prototype); assert.equal(util.getType(new Tester('stringy!')), 'String'); }); + it('of object Array is Array', function () { assert.equal(util.getType(new Array([])), 'Array'); }); + it('of object Date is Date', function () { assert.equal(util.getType(new Date()), 'Date'); }); + it('of object any other type is Object', function () { assert.equal(util.getType({}), 'Object'); }); + it('of number is Number', function () { assert.equal(util.getType(1), 'Number'); }); + it('of boolean is Boolean', function () { assert.equal(util.getType(true), 'Boolean'); }); + it('of string is String', function () { assert.equal(util.getType('string'), 'String'); }); + it('of undefined is undefined', function () { assert.equal(util.getType(), 'undefined'); }); }); describe('easingFunctions', function () { + it('take a number and output a number', function () { for (var key in util.easingFunctions) { if (util.easingFunctions.hasOwnProperty(key)) { @@ -689,30 +702,38 @@ describe('mergeOptions', function () { }); describe('getScrollBarWidth', function () { + beforeEach(function() { this.jsdom_global = jsdom_global(); }); + afterEach(function() { this.jsdom_global(); }); + it('returns 0 when there is no content', function () { assert.equal(util.getScrollBarWidth(), 0); }); }); describe('equalArray', function () { + it('arrays of different lengths are not equal', function () { assert.equal(util.equalArray([1, 2, 3], [1, 2]), false) }); + it('arrays with different content are not equal', function () { assert.equal(util.equalArray([1, 2, 3], [3, 2, 1]), false) }); + it('same content arrays are equal', function () { assert(util.equalArray([1, 2, 3], [1, 2, 3])) }); + it('empty arrays are equal', function () { assert(util.equalArray([], [])) }); + it('the same array is equal', function () { var arr = [1, 2, 3]; assert(util.equalArray(arr, arr)) @@ -720,96 +741,121 @@ describe('mergeOptions', function () { }); describe('asBoolean', function () { + it('resolves value from a function', function () { assert(util.option.asBoolean(function () {return true}, false)); }); + it('returns default value for null', function () { assert(util.option.asBoolean(null, true)); }); + it('returns true for other types', function () { assert(util.option.asBoolean('should be true', false)); }); + it('returns null for undefined', function () { assert.equal(util.option.asBoolean(), null); }); }); describe('asNumber', function () { + it('resolves value from a function', function () { assert.equal(util.option.asNumber(function () {return 777}, 13), 777); }); + it('returns default value for null', function () { assert.equal(util.option.asNumber(null, 13), 13); }); + it('returns number for other types', function () { assert.equal(util.option.asNumber('777', 13), 777); }); + it('returns default for NaN', function () { assert.equal(util.option.asNumber(NaN, 13), 13); }); + it('returns null for undefined', function () { assert.equal(util.option.asNumber(), null); }); }); describe('asString', function () { + it('resolves value from a function', function () { assert.equal(util.option.asString(function () {return 'entered'}, 'default'), 'entered'); }); + it('returns default value for null', function () { assert.equal(util.option.asString(null, 'default'), 'default'); }); + it('returns string for other types', function () { assert.equal(util.option.asString(777, 'default'), '777'); }); + it('returns default for undefined', function () { assert.equal(util.option.asString(undefined, 'default'), 'default'); }); + it('returns null for undefined', function () { assert.equal(util.option.asString(), null); }); }); describe('asSize', function () { + it('resolves value from a function', function () { assert.equal(util.option.asSize(function () {return '100px'}, '50px'), '100px'); }); + it('returns default value for null', function () { assert.equal(util.option.asSize(null, '50px'), '50px'); }); + it('returns string with px for other number', function () { assert.equal(util.option.asSize(100, '50px'), '100px'); }); + it('returns default for undefined', function () { assert.equal(util.option.asSize(undefined, '50px'), '50px'); }); + it('returns null for undefined', function () { assert.equal(util.option.asSize(), null); }); }); describe('asElement', function () { + before(function() { this.jsdom_global = jsdom_global(); this.value = document.createElement("div"); this.defaultValue = document.createElement("div"); }); + it('resolves value from a function', function () { var me = this; assert.equal(util.option.asElement(function () {return me.value}, this.defaultValue), this.value); }); + it('returns Element', function () { assert.equal(util.option.asElement(this.value, this.defaultValue), this.value); }); + it('returns default value for null', function () { assert.equal(util.option.asElement(null, this.defaultValue), this.defaultValue); }); + it('returns null for undefined', function () { assert.equal(util.option.asElement(), null); }); }); describe('binarySearchValue', function () { + it('Finds center target on odd sized array', function () { assert.equal( util.binarySearchValue( @@ -820,6 +866,7 @@ describe('mergeOptions', function () { 1 ); }); + it('Finds target on odd sized array', function () { assert.equal( util.binarySearchValue( @@ -830,6 +877,7 @@ describe('mergeOptions', function () { 2 ); }); + it('Cannot find target', function () { assert.equal( util.binarySearchValue(