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.

224 lines
8.0 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 = canvasMockify("<div id='mynetwork'></div>");
  9. this.container = document.getElementById('mynetwork');
  10. });
  11. afterEach(function() {
  12. this.jsdom_global();
  13. this.container.remove();
  14. this.container = undefined;
  15. });
  16. describe('constructor', function () {
  17. it('sets defaults', function () {
  18. var colorPicker = new ColorPicker();
  19. assert.equal(colorPicker.pixelRatio, 1);
  20. assert.equal(colorPicker.generated, false);
  21. assert.deepEqual(colorPicker.centerCoordinates, {x:289/2, y:289/2});
  22. assert.equal(colorPicker.r, 289 * 0.49);
  23. assert.deepEqual(colorPicker.color, {r:255,g:255,b:255,a:1.0});
  24. assert.equal(colorPicker.hueCircle, undefined);
  25. assert.deepEqual(colorPicker.initialColor, {r:255,g:255,b:255,a:1.0});
  26. assert.equal(colorPicker.previousColor, undefined);
  27. assert.equal(colorPicker.applied, false);
  28. });
  29. // TODO: This gets overridden during instantiation - Is this a bug?
  30. xit('can overwrite default pixelRation', function () {
  31. var colorPicker = new ColorPicker(777);
  32. assert.equal(colorPicker.pixelRatio, 777);
  33. });
  34. });
  35. describe('insertTo', function () {
  36. it('inserts the colorPicker into a div from the DOM', function () {
  37. var colorPicker = new ColorPicker();
  38. colorPicker.insertTo(this.container);
  39. assert.equal(colorPicker.container, this.container);
  40. assert.equal(this.container.children[this.container.children.length-1], colorPicker.frame);
  41. });
  42. });
  43. describe('setUpdateCallback', function () {
  44. it('prevents non-functions from being set as callback', function () {
  45. var colorPicker = new ColorPicker();
  46. assert.throws(function () {colorPicker.setUpdateCallback(null);}, Error, null);
  47. assert.throws(function () {colorPicker.setUpdateCallback(undefined);}, Error, null);
  48. assert.throws(function () {colorPicker.setUpdateCallback([1, 2, 3]);}, Error, null);
  49. assert.throws(function () {colorPicker.setUpdateCallback({a: 42});}, Error, null);
  50. assert.throws(function () {colorPicker.setUpdateCallback(42);}, Error, null);
  51. assert.throws(function () {colorPicker.setUpdateCallback('meow');}, Error, null);
  52. });
  53. });
  54. describe('setCloseCallback', function () {
  55. it('prevents non-functions from being set as callback', function () {
  56. var colorPicker = new ColorPicker();
  57. assert.throws(function () {colorPicker.setCloseCallback(null);}, Error, null);
  58. assert.throws(function () {colorPicker.setCloseCallback(undefined);}, Error, null);
  59. assert.throws(function () {colorPicker.setCloseCallback([1, 2, 3]);}, Error, null);
  60. assert.throws(function () {colorPicker.setCloseCallback({a: 42});}, Error, null);
  61. assert.throws(function () {colorPicker.setCloseCallback(42);}, Error, null);
  62. assert.throws(function () {colorPicker.setCloseCallback('meow');}, Error, null);
  63. });
  64. });
  65. describe('_hide', function () {
  66. it('runs updateCallback when applied', function () {
  67. var callback = sinon.spy();
  68. var colorPicker = new ColorPicker();
  69. var colorBeforeHide = colorPicker.color;
  70. colorPicker.setUpdateCallback(callback);
  71. colorPicker.applied = true;
  72. colorPicker._hide();
  73. assert.equal(callback.callCount, 1);
  74. assert.deepEqual(colorBeforeHide, colorPicker.previousColor);
  75. });
  76. it('does not run updateCallback when not applied', function () {
  77. var callback = sinon.spy();
  78. var colorPicker = new ColorPicker();
  79. var colorBeforeHide = colorPicker.color;
  80. colorPicker.setUpdateCallback(callback);
  81. colorPicker.applied = false;
  82. colorPicker._hide();
  83. assert.equal(callback.callCount, 0);
  84. assert.deepEqual(colorBeforeHide, colorPicker.previousColor);
  85. });
  86. it('does not set previous color when storePrevious is false', function () {
  87. var colorPicker = new ColorPicker();
  88. colorPicker._hide(false);
  89. assert.deepEqual(colorPicker.previousColor, undefined);
  90. });
  91. });
  92. describe('_isColorString', function () {
  93. it('returns color code when color is found', function () {
  94. var colorPicker = new ColorPicker();
  95. var color = colorPicker._isColorString('black');
  96. assert.equal(color, '#000000');
  97. });
  98. it('returns undefined when color is not found', function () {
  99. var colorPicker = new ColorPicker();
  100. var color = colorPicker._isColorString('zing!');
  101. assert.equal(color, undefined);
  102. });
  103. });
  104. describe('setColor', function () {
  105. it('does not change when \'none\'', function () {
  106. var colorPicker = new ColorPicker();
  107. colorPicker.setColor('none');
  108. assert.deepEqual(colorPicker.color, { r: 255, g: 255, b: 255, a: 1 });
  109. });
  110. it('throws error when color is a bad value', function () {
  111. var colorPicker = new ColorPicker();
  112. assert.throws(function () {colorPicker.setColor(null);}, Error, null);
  113. assert.throws(function () {colorPicker.setColor(undefined);}, Error, null);
  114. assert.throws(function () {colorPicker.setColor([1, 2, 3]);}, Error, null);
  115. assert.throws(function () {colorPicker.setColor({a: 42});}, Error, null);
  116. assert.throws(function () {colorPicker.setColor(42);}, Error, null);
  117. assert.throws(function () {colorPicker.setColor('meow');}, Error, null);
  118. });
  119. it('handles html color string', function () {
  120. var colorPicker = new ColorPicker();
  121. colorPicker.setColor('black');
  122. assert.deepEqual(colorPicker.color, { r: 0, g: 0, b: 0, a: 1 });
  123. });
  124. it('handles hex string', function () {
  125. var colorPicker = new ColorPicker();
  126. colorPicker.setColor('#ffff00');
  127. assert.deepEqual(colorPicker.color, { r: 255, g: 255, b: 0, a: 1 });
  128. });
  129. it('handles rgb string', function () {
  130. var colorPicker = new ColorPicker();
  131. colorPicker.setColor('rgb(255,255,255)');
  132. assert.deepEqual(colorPicker.color, { r: 255, g: 255, b: 255, a: 1 });
  133. });
  134. it('handles rgba string', function () {
  135. var colorPicker = new ColorPicker();
  136. colorPicker.setColor('rgba(255,255,255,1)');
  137. assert.deepEqual(colorPicker.color, { r: 255, g: 255, b: 255, a: 1 });
  138. });
  139. it('handles rgb object', function () {
  140. var colorPicker = new ColorPicker();
  141. colorPicker.setColor({r:255,g:255,b:255});
  142. assert.deepEqual(colorPicker.color, { r: 255, g: 255, b: 255, a: 1 });
  143. });
  144. it('handles rgba object', function () {
  145. var colorPicker = new ColorPicker();
  146. colorPicker.setColor({r:255,g:255,b:255,a:1});
  147. assert.deepEqual(colorPicker.color, { r: 255, g: 255, b: 255, a: 1 });
  148. });
  149. });
  150. describe('show', function () {
  151. it('calls closeCallback', function () {
  152. var colorPicker = new ColorPicker();
  153. var callback = sinon.spy();
  154. colorPicker.setCloseCallback(callback);
  155. colorPicker.show();
  156. assert(callback.called);
  157. assert(callback.calledOnce);
  158. assert(colorPicker.generated)
  159. });
  160. it('resets applied state and frame display style to `block`', function () {
  161. var colorPicker = new ColorPicker();
  162. colorPicker.show();
  163. assert.equal(colorPicker.applied, false);
  164. assert.equal(colorPicker.frame.style.display, 'block');
  165. assert(colorPicker.generated)
  166. });
  167. });
  168. describe('_save', function () {
  169. it('triggers updateCallback', function () {
  170. var colorPicker = new ColorPicker();
  171. var callback = sinon.spy();
  172. colorPicker.setUpdateCallback(callback);
  173. colorPicker._save();
  174. assert(callback.called);
  175. assert(callback.calledOnce);
  176. });
  177. });
  178. describe('_apply', function () {
  179. it('triggers updateCallback', function () {
  180. var colorPicker = new ColorPicker();
  181. var callback = sinon.spy();
  182. colorPicker.setUpdateCallback(callback);
  183. colorPicker._apply();
  184. assert(callback.called);
  185. assert(callback.calledOnce);
  186. });
  187. });
  188. });