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.

200 lines
6.3 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('throws error when color is null', function () {
  96. var colorPicker = new ColorPicker();
  97. assert.throws(function () {colorPicker.setColor(null);}, Error, null);
  98. });
  99. it('handles html color string', function () {
  100. var colorPicker = new ColorPicker();
  101. colorPicker.setColor('black');
  102. assert.deepEqual(colorPicker.color, { r: 0, g: 0, b: 0, a: 1 });
  103. });
  104. it('handles hex string', function () {
  105. var colorPicker = new ColorPicker();
  106. colorPicker.setColor('#ffff00');
  107. assert.deepEqual(colorPicker.color, { r: 255, g: 255, b: 0, a: 1 });
  108. });
  109. it('handles rgb string', function () {
  110. var colorPicker = new ColorPicker();
  111. colorPicker.setColor('rgb(255,255,255)');
  112. assert.deepEqual(colorPicker.color, { r: 255, g: 255, b: 255, a: 1 });
  113. });
  114. it('handles rgba string', function () {
  115. var colorPicker = new ColorPicker();
  116. colorPicker.setColor('rgba(255,255,255,1)');
  117. assert.deepEqual(colorPicker.color, { r: 255, g: 255, b: 255, a: 1 });
  118. });
  119. it('handles rgb object', function () {
  120. var colorPicker = new ColorPicker();
  121. colorPicker.setColor({r:255,g:255,b:255});
  122. assert.deepEqual(colorPicker.color, { r: 255, g: 255, b: 255, a: 1 });
  123. });
  124. it('handles rgba object', function () {
  125. var colorPicker = new ColorPicker();
  126. colorPicker.setColor({r:255,g:255,b:255,a:1});
  127. assert.deepEqual(colorPicker.color, { r: 255, g: 255, b: 255, a: 1 });
  128. });
  129. });
  130. describe('show', function () {
  131. it('calls closeCallback', function () {
  132. var colorPicker = new ColorPicker();
  133. var callback = sinon.spy();
  134. colorPicker.setCloseCallback(callback);
  135. colorPicker.show();
  136. assert(callback.called);
  137. assert(callback.calledOnce);
  138. });
  139. it('resets applied state and frame display style to `block`', function () {
  140. var colorPicker = new ColorPicker();
  141. colorPicker.show();
  142. assert.equal(colorPicker.applied, false);
  143. assert.equal(colorPicker.frame.style.display, 'block');
  144. });
  145. });
  146. describe('_save', function () {
  147. it('triggers updateCallback', function () {
  148. var colorPicker = new ColorPicker();
  149. var callback = sinon.spy();
  150. colorPicker.setUpdateCallback(callback);
  151. colorPicker._save();
  152. assert(callback.called);
  153. assert(callback.calledOnce);
  154. });
  155. });
  156. describe('_apply', function () {
  157. it('triggers updateCallback', function () {
  158. var colorPicker = new ColorPicker();
  159. var callback = sinon.spy();
  160. colorPicker.setUpdateCallback(callback);
  161. colorPicker._apply();
  162. assert(callback.called);
  163. assert(callback.calledOnce);
  164. });
  165. });
  166. });