Browse Source

Tests and sinon dependency introduced

mbroad/unittest/lib/shared
MacLeod Broad 7 years ago
parent
commit
319fced2b5
No known key found for this signature in database GPG Key ID: F1B295D13C3CC9CF
5 changed files with 362 additions and 4 deletions
  1. +1
    -0
      package.json
  2. +92
    -0
      test/Activator.test.js
  3. +123
    -0
      test/ColorPicker.test.js
  4. +141
    -0
      test/Configurator.test.js
  5. +5
    -4
      test/Popup.test.js

+ 1
- 0
package.json View File

@ -66,6 +66,7 @@
"mocha-jsdom": "^1.1.0", "mocha-jsdom": "^1.1.0",
"nyc": "^11.2.1", "nyc": "^11.2.1",
"rimraf": "^2.6.1", "rimraf": "^2.6.1",
"sinon": "^4.0.1",
"test-console": "^1.0.0", "test-console": "^1.0.0",
"uglify-js": "^2.8.29", "uglify-js": "^2.8.29",
"uuid": "^3.1.0", "uuid": "^3.1.0",

+ 92
- 0
test/Activator.test.js View File

@ -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');
});
it('emits a `change` event', function () {
var eventSpy = sinon.spy();
var activator = new Activator(this.container);
activator.on('change', eventSpy);
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 () {
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);
});
});
});

+ 123
- 0
test/ColorPicker.test.js View File

@ -0,0 +1,123 @@
var assert = require('assert');
var sinon = require('sinon');
var jsdom_global = require('jsdom-global');
var canvasMockify = require('./canvas-mock');
var ColorPicker = require('../lib/shared/ColorPicker').default;
describe('ColorPicker', 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 colorPicker = new ColorPicker();
assert.equal(colorPicker.pixelRatio, 1);
assert.equal(colorPicker.generated, false);
assert.deepEqual(colorPicker.centerCoordinates, {x:289/2, y:289/2});
assert.equal(colorPicker.r, 289 * 0.49);
assert.deepEqual(colorPicker.color, {r:255,g:255,b:255,a:1.0});
assert.equal(colorPicker.hueCircle, undefined);
assert.deepEqual(colorPicker.initialColor, {r:255,g:255,b:255,a:1.0});
assert.equal(colorPicker.previousColor, undefined);
assert.equal(colorPicker.applied, false);
});
// TODO: This gets overridden during instantiation - Is this a bug?
xit('can overwrite default pixelRation', function () {
var colorPicker = new ColorPicker(777);
assert.equal(colorPicker.pixelRatio, 777);
});
});
describe('insertTo', function () {
it('inserts the colorPicker into a div from the DOM', function () {
var colorPicker = new ColorPicker();
colorPicker.insertTo(this.container);
assert.equal(colorPicker.container, this.container);
assert.equal(this.container.children[this.container.children.length-1], colorPicker.frame);
});
});
describe('setUpdateCallback', function () {
it('prevents non-functions from being set as callback', function () {
var colorPicker = new ColorPicker();
assert.throws(function () {colorPicker.setUpdateCallback(null);}, Error, null);
});
});
describe('setCloseCallback', function () {
it('prevents non-functions from being set as callback', function () {
var colorPicker = new ColorPicker();
assert.throws(function () {colorPicker.setCloseCallback(null);}, Error, null);
});
});
describe('_hide', function () {
it('runs updateCallback when applied', function () {
var callback = sinon.spy();
var colorPicker = new ColorPicker();
colorPicker.setUpdateCallback(callback);
colorPicker.applied = true;
colorPicker._hide();
assert.equal(callback.callCount, 1);
});
it('does not run updateCallback when not applied', function () {
var callback = sinon.spy();
var colorPicker = new ColorPicker();
colorPicker.setUpdateCallback(callback);
colorPicker.applied = false;
colorPicker._hide();
assert.equal(callback.callCount, 0);
});
});
describe('_isColorString', function () {
it('returns color code when color is found', function () {
var colorPicker = new ColorPicker();
var color = colorPicker._isColorString('black');
assert.equal(color, '#000000');
});
it('returns undefined when color is not found', function () {
var colorPicker = new ColorPicker();
var color = colorPicker._isColorString('zing!');
assert.equal(color, undefined);
});
});
describe('setColor', function () {
it('does not change when \'none\'', function () {
var colorPicker = new ColorPicker();
colorPicker.setColor('none');
assert.deepEqual(colorPicker.color, { r: 255, g: 255, b: 255, a: 1 });
});
it('handles null', function () {
var colorPicker = new ColorPicker();
colorPicker.setColor('none');
assert.deepEqual(colorPicker.color, { r: 255, g: 255, b: 255, a: 1 });
});
});
});

+ 141
- 0
test/Configurator.test.js View File

@ -0,0 +1,141 @@
var assert = require('assert');
var jsdom_global = require('jsdom-global');
var canvasMockify = require('./canvas-mock');
var Configurator = require('../lib/shared/Configurator').default;
var Network = require('../lib/network/Network');
var {allOptions, configureOptions} = require('../lib/network/options.js');
describe('Configurator', function () {
beforeEach(function() {
this.jsdom_global = jsdom_global(
"<div id='mynetwork'></div><div id='other'></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 extends options with default options', function () {
var config = new Configurator();
assert.equal(JSON.stringify(config.options), JSON.stringify(config.defaultOptions));
});
});
describe('setOptions', 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));
});
it('with undefined will set enabled to true', function () {
var config = new Configurator(Network, this.container);
config.options.enabled = false;
config.setOptions();
assert.equal(JSON.stringify(config.options), JSON.stringify(config.defaultOptions));
});
it('with string sets filter and set enabled to true', function () {
var config = new Configurator(Network, this.container);
config.setOptions('stringFilter!');
assert.equal(config.options.filter, 'stringFilter!');
assert.equal(config.options.enabled, true);
});
it('with array sets filter and set enabled to true', function () {
var config = new Configurator(Network, this.container);
config.setOptions(['array', 'Filter', '!']);
assert.equal(config.options.filter, 'array,Filter,!');
assert.equal(config.options.enabled, true);
});
it('with object sets filter', function () {
var config = new Configurator(Network, this.container);
config.setOptions(
{container: 'newContainer',
filter: 'newFilter',
showButton: 'newShowButton',
enabled: false
});
assert.equal(config.options.container, 'newContainer');
assert.equal(config.options.filter, 'newFilter');
assert.equal(config.options.showButton, 'newShowButton');
assert.equal(config.options.enabled, false);
});
it('with object and filter is false enabled will be false', function () {
var config = new Configurator(Network, this.container);
config.setOptions({filter: false});
assert.equal(config.options.enabled, false);
});
it('with boolean true sets filter', function () {
var config = new Configurator(Network, this.container);
config.setOptions(true);
assert.equal(config.options.enabled, true);
});
it('with boolean false sets filter', function () {
var config = new Configurator(Network, this.container);
config.setOptions(false);
assert.equal(config.options.enabled, false);
});
it('with function sets filter', function () {
var config = new Configurator(Network, this.container);
config.setOptions(function () {});
assert.equal(config.options.enabled, true);
});
it('with null raises exception', function () {
var config = new Configurator(Network, this.container);
assert.throws(function () {config.setOptions(null)}, TypeError, null);
});
});
describe('setModuleOptions', function () {
it('creates no new dom elements if enabled is false', function () {
var config = new Configurator(Network, this.container);
config.setModuleOptions();
assert.equal(this.container.children.length, 0);
});
it('adds div with vis-configuration-wrapper class when enabled', function () {
var config = new Configurator(Network, this.container);
config.options.enabled = true;
config.setModuleOptions();
assert.equal(this.container.children.length, 1);
assert.equal(this.container.children[0].className, 'vis-configuration-wrapper');
});
it('overwrites config.container with config.options.container', function () {
var config = new Configurator(Network, this.container);
config.options.enabled = true;
config.options.container = document.getElementById('other');
config.setModuleOptions();
assert.equal(config.container, config.options.container);
assert.equal(config.container.children[0].className, 'vis-configuration-wrapper');
});
});
// TODO: This test needs work
describe('getOptions', function () {
xit('creates no new dom elements if enabled is false', function () {
var config = new Configurator(Network, this.container, configureOptions);
var options = config.getOptions();
});
});
});

+ 5
- 4
test/Popup.test.js View File

@ -6,7 +6,7 @@ var Popup = require('../lib/shared/Popup').default;
describe('Popup', function () { describe('Popup', function () {
before(function() {
beforeEach(function() {
this.jsdom_global = jsdom_global( this.jsdom_global = jsdom_global(
"<div id='mynetwork'></div>", "<div id='mynetwork'></div>",
{ skipWindowCheck: true} { skipWindowCheck: true}
@ -15,8 +15,10 @@ describe('Popup', function () {
this.container = document.getElementById('mynetwork'); this.container = document.getElementById('mynetwork');
}); });
after(function() {
afterEach(function() {
this.jsdom_global(); this.jsdom_global();
this.container.remove();
this.container = undefined;
}); });
describe('constructor', function () { describe('constructor', function () {
@ -134,10 +136,9 @@ describe('Popup', function () {
describe('destroy', function () { describe('destroy', function () {
it('removes frame from container', function () { it('removes frame from container', function () {
var numChildrenBeforePopup = this.container.children.length;
var popup = new Popup(this.container); var popup = new Popup(this.container);
popup.destroy(); popup.destroy();
assert.equal(this.container.children.length, numChildrenBeforePopup);
assert.equal(this.container.children.length, 0);
}); });
}); });
}); });

Loading…
Cancel
Save