vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.9 KiB

  1. var assert = require('assert');
  2. var sinon = require('sinon');
  3. var jsdom_global = require('jsdom-global');
  4. var canvasMockify = require('./canvas-mock');
  5. var ColorPicker = require('../lib/shared/ColorPicker').default;
  6. describe('ColorPicker', function () {
  7. beforeEach(function() {
  8. this.jsdom_global = jsdom_global(
  9. "<div id='mynetwork'></div>",
  10. { skipWindowCheck: true}
  11. );
  12. canvasMockify(window);
  13. this.container = document.getElementById('mynetwork');
  14. });
  15. afterEach(function() {
  16. this.jsdom_global();
  17. this.container.remove();
  18. this.container = undefined;
  19. });
  20. describe('constructor', function () {
  21. it('sets defaults', function () {
  22. var colorPicker = new ColorPicker();
  23. assert.equal(colorPicker.pixelRatio, 1);
  24. assert.equal(colorPicker.generated, false);
  25. assert.deepEqual(colorPicker.centerCoordinates, {x:289/2, y:289/2});
  26. assert.equal(colorPicker.r, 289 * 0.49);
  27. assert.deepEqual(colorPicker.color, {r:255,g:255,b:255,a:1.0});
  28. assert.equal(colorPicker.hueCircle, undefined);
  29. assert.deepEqual(colorPicker.initialColor, {r:255,g:255,b:255,a:1.0});
  30. assert.equal(colorPicker.previousColor, undefined);
  31. assert.equal(colorPicker.applied, false);
  32. });
  33. // TODO: This gets overridden during instantiation - Is this a bug?
  34. xit('can overwrite default pixelRation', function () {
  35. var colorPicker = new ColorPicker(777);
  36. assert.equal(colorPicker.pixelRatio, 777);
  37. });
  38. });
  39. describe('insertTo', function () {
  40. it('inserts the colorPicker into a div from the DOM', function () {
  41. var colorPicker = new ColorPicker();
  42. colorPicker.insertTo(this.container);
  43. assert.equal(colorPicker.container, this.container);
  44. assert.equal(this.container.children[this.container.children.length-1], colorPicker.frame);
  45. });
  46. });
  47. describe('setUpdateCallback', function () {
  48. it('prevents non-functions from being set as callback', function () {
  49. var colorPicker = new ColorPicker();
  50. assert.throws(function () {colorPicker.setUpdateCallback(null);}, Error, null);
  51. });
  52. });
  53. describe('setCloseCallback', function () {
  54. it('prevents non-functions from being set as callback', function () {
  55. var colorPicker = new ColorPicker();
  56. assert.throws(function () {colorPicker.setCloseCallback(null);}, Error, null);
  57. });
  58. });
  59. describe('_hide', function () {
  60. it('runs updateCallback when applied', function () {
  61. var callback = sinon.spy();
  62. var colorPicker = new ColorPicker();
  63. colorPicker.setUpdateCallback(callback);
  64. colorPicker.applied = true;
  65. colorPicker._hide();
  66. assert.equal(callback.callCount, 1);
  67. });
  68. it('does not run updateCallback when not applied', function () {
  69. var callback = sinon.spy();
  70. var colorPicker = new ColorPicker();
  71. colorPicker.setUpdateCallback(callback);
  72. colorPicker.applied = false;
  73. colorPicker._hide();
  74. assert.equal(callback.callCount, 0);
  75. });
  76. });
  77. describe('_isColorString', function () {
  78. it('returns color code when color is found', function () {
  79. var colorPicker = new ColorPicker();
  80. var color = colorPicker._isColorString('black');
  81. assert.equal(color, '#000000');
  82. });
  83. it('returns undefined when color is not found', function () {
  84. var colorPicker = new ColorPicker();
  85. var color = colorPicker._isColorString('zing!');
  86. assert.equal(color, undefined);
  87. });
  88. });
  89. describe('setColor', function () {
  90. it('does not change when \'none\'', function () {
  91. var colorPicker = new ColorPicker();
  92. colorPicker.setColor('none');
  93. assert.deepEqual(colorPicker.color, { r: 255, g: 255, b: 255, a: 1 });
  94. });
  95. it('handles null', function () {
  96. var colorPicker = new ColorPicker();
  97. colorPicker.setColor('none');
  98. assert.deepEqual(colorPicker.color, { r: 255, g: 255, b: 255, a: 1 });
  99. });
  100. });
  101. });