Browse Source

Corrects broken tests and adds more tests to ColorPicker

mbroad/unittest/lib/shared
MacLeod Broad 7 years ago
parent
commit
e25d2e2286
No known key found for this signature in database GPG Key ID: F1B295D13C3CC9CF
5 changed files with 88 additions and 9 deletions
  1. +1
    -1
      lib/shared/ColorPicker.js
  2. +79
    -2
      test/ColorPicker.test.js
  3. +2
    -2
      test/Configurator.test.js
  4. +1
    -1
      test/Popup.test.js
  5. +5
    -3
      test/canvas-mock.js

+ 1
- 1
lib/shared/ColorPicker.js View File

@ -92,7 +92,7 @@ class ColorPicker {
* Supported formats: * Supported formats:
* 'red' --> HTML color string * 'red' --> HTML color string
* '#ffffff' --> hex string * '#ffffff' --> hex string
* 'rbg(255,255,255)' --> rgb string
* 'rgb(255,255,255)' --> rgb string
* 'rgba(255,255,255,1.0)' --> rgba string * 'rgba(255,255,255,1.0)' --> rgba string
* {r:255,g:255,b:255} --> rgb object * {r:255,g:255,b:255} --> rgb object
* {r:255,g:255,b:255,a:1.0} --> rgba object * {r:255,g:255,b:255,a:1.0} --> rgba object

+ 79
- 2
test/ColorPicker.test.js View File

@ -114,10 +114,87 @@ describe('ColorPicker', function () {
assert.deepEqual(colorPicker.color, { r: 255, g: 255, b: 255, a: 1 }); assert.deepEqual(colorPicker.color, { r: 255, g: 255, b: 255, a: 1 });
}); });
it('handles null', function () {
it('throws error when color is null', function () {
var colorPicker = new ColorPicker(); var colorPicker = new ColorPicker();
colorPicker.setColor('none');
assert.throws(function () {colorPicker.setColor(null);}, Error, null);
});
it('handles html color string', function () {
var colorPicker = new ColorPicker();
colorPicker.setColor('black');
assert.deepEqual(colorPicker.color, { r: 0, g: 0, b: 0, a: 1 });
});
it('handles hex string', function () {
var colorPicker = new ColorPicker();
colorPicker.setColor('#ffff00');
assert.deepEqual(colorPicker.color, { r: 255, g: 255, b: 0, a: 1 });
});
it('handles rgb string', function () {
var colorPicker = new ColorPicker();
colorPicker.setColor('rgb(255,255,255)');
assert.deepEqual(colorPicker.color, { r: 255, g: 255, b: 255, a: 1 }); assert.deepEqual(colorPicker.color, { r: 255, g: 255, b: 255, a: 1 });
}); });
it('handles rgba string', function () {
var colorPicker = new ColorPicker();
colorPicker.setColor('rgba(255,255,255,1)');
assert.deepEqual(colorPicker.color, { r: 255, g: 255, b: 255, a: 1 });
});
it('handles rgb object', function () {
var colorPicker = new ColorPicker();
colorPicker.setColor({r:255,g:255,b:255});
assert.deepEqual(colorPicker.color, { r: 255, g: 255, b: 255, a: 1 });
});
it('handles rgba object', function () {
var colorPicker = new ColorPicker();
colorPicker.setColor({r:255,g:255,b:255,a:1});
assert.deepEqual(colorPicker.color, { r: 255, g: 255, b: 255, a: 1 });
});
});
describe('show', function () {
it('calls closeCallback', function () {
var colorPicker = new ColorPicker();
var callback = sinon.spy();
colorPicker.setCloseCallback(callback);
colorPicker.show();
assert(callback.called);
assert(callback.calledOnce);
});
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');
});
});
describe('_save', function () {
it('triggers updateCallback', function () {
var colorPicker = new ColorPicker();
var callback = sinon.spy();
colorPicker.setUpdateCallback(callback);
colorPicker._save();
assert(callback.called);
assert(callback.calledOnce);
});
});
describe('_apply', function () {
it('triggers updateCallback', function () {
var colorPicker = new ColorPicker();
var callback = sinon.spy();
colorPicker.setUpdateCallback(callback);
colorPicker._apply();
assert(callback.called);
assert(callback.calledOnce);
});
}); });
}); });

+ 2
- 2
test/Configurator.test.js View File

@ -38,11 +38,11 @@ describe('Configurator', function () {
assert.equal(JSON.stringify(config.options), JSON.stringify(config.defaultOptions)); assert.equal(JSON.stringify(config.options), JSON.stringify(config.defaultOptions));
}); });
it('with undefined will set enabled to true', function () {
it('with undefined will set enabled to false', function () {
var config = new Configurator(Network, this.container); var config = new Configurator(Network, this.container);
config.options.enabled = false; config.options.enabled = false;
config.setOptions(); config.setOptions();
assert.equal(JSON.stringify(config.options), JSON.stringify(config.defaultOptions));
assert.equal(config.options.enabled, false);
}); });
it('with string sets filter and set enabled to true', function () { it('with string sets filter and set enabled to true', function () {

+ 1
- 1
test/Popup.test.js View File

@ -57,7 +57,7 @@ describe('Popup', function () {
assert(isNaN(popup.y)); assert(isNaN(popup.y));
}); });
it('handles null with NaN', function () {
it('handles undefined with NaN', function () {
var popup = new Popup(this.container); var popup = new Popup(this.container);
popup.setPosition(undefined, undefined); popup.setPosition(undefined, undefined);
assert(isNaN(popup.x)); assert(isNaN(popup.x));

+ 5
- 3
test/canvas-mock.js View File

@ -22,6 +22,7 @@ function replaceCanvasContext (el) {
setTransform: function(){}, setTransform: function(){},
drawImage: function(){}, drawImage: function(){},
save: function(){}, save: function(){},
text: function(){},
fillText: function(){}, fillText: function(){},
restore: function(){}, restore: function(){},
beginPath: function(){}, beginPath: function(){},
@ -32,6 +33,7 @@ function replaceCanvasContext (el) {
translate: function(){}, translate: function(){},
scale: function(){}, scale: function(){},
rotate: function(){}, rotate: function(){},
circle: function(){},
arc: function(){}, arc: function(){},
fill: function(){}, fill: function(){},
@ -44,10 +46,10 @@ function replaceCanvasContext (el) {
width: 12*text.length, width: 12*text.length,
height: 14 height: 14
}; };
},
}
}; };
} }
};
}
/** /**
@ -59,7 +61,7 @@ function replaceCanvasContext (el) {
* The override is only done if there is no 2D context already present. * The override is only done if there is no 2D context already present.
* This allows for normal running in a browser, and for node.js the usage of 'canvas'. * This allows for normal running in a browser, and for node.js the usage of 'canvas'.
* *
* @param {object} the current global window object. This can possible come from module 'jsdom',
* @param {object} window - current global window object. This can possible come from module 'jsdom',
* when running under node.js. * when running under node.js.
*/ */
function mockify(window) { function mockify(window) {

Loading…
Cancel
Save