tests.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. // Spectrum Colorpicker Tests
  2. // https://github.com/bgrins/spectrum
  3. // Author: Brian Grinstead
  4. // License: MIT
  5. module("Initialization");
  6. test( "jQuery Plugin Can Be Created", function() {
  7. var el = $("<input id='spec' />").spectrum();
  8. ok(el.attr("id") === "spec", "Element returned from plugin" );
  9. el.spectrum("set", "red");
  10. equal(el.spectrum("get").toName(), "red", "Basic color setting");
  11. equal(el.spectrum("option", "showInput"), false, "Undefined option is false.");
  12. el.spectrum({showInput: true});
  13. equal(el.spectrum("option", "showInput"), true, "Double initialized spectrum is destroyed before recreating.");
  14. el.spectrum("destroy");
  15. equal(el.spectrum("container"), el, "After destroying spectrum, string function returns input.");
  16. });
  17. test( "Per-element Options Are Read From Data Attributes", function() {
  18. var el = $("<input id='spec' data-show-alpha='true' />").spectrum();
  19. equal ( el.spectrum("option", "showAlpha"), true, "Took showAlpha value from data attribute");
  20. el.spectrum("destroy");
  21. var changeDefault = $("<input id='spec' data-show-alpha='false' />").spectrum({
  22. showAlpha: true
  23. });
  24. equal ( changeDefault.spectrum("option", "showAlpha"), false, "Took showAlpha value from data attribute");
  25. changeDefault.spectrum("destroy");
  26. var noData = $("<input id='spec' />").spectrum({
  27. showAlpha: true
  28. });
  29. equal ( noData.spectrum("option", "showAlpha"), true, "Kept showAlpha without data attribute");
  30. noData.spectrum("destroy");
  31. });
  32. test( "Events Fire", function() {
  33. var el = $("<input id='spec' />").spectrum();
  34. var count = 0;
  35. expect(5);
  36. el.on("beforeShow.spectrum", function(e) {
  37. // Cancel the event the first time
  38. if (count === 0) {
  39. ok(true, "Cancel beforeShow");
  40. count++;
  41. return false;
  42. }
  43. ok (count === 1, "Allow beforeShow");
  44. count++;
  45. });
  46. el.on("show.spectrum", function(e) {
  47. ok(count === 2, "Show");
  48. count++;
  49. });
  50. el.on("hide.spectrum", function(e) {
  51. ok(count === 3, "Hide");
  52. count++;
  53. });
  54. el.on("move.spectrum", function(e) {
  55. });
  56. el.on("change", function(e, color) {
  57. ok(false, "Change should not fire from `set` call");
  58. });
  59. el.spectrum("show");
  60. el.spectrum("show");
  61. el.spectrum("hide");
  62. el.spectrum("set", "red");
  63. el.spectrum("destroy");
  64. var el2 = $("<input />").spectrum({
  65. showInput: true
  66. });
  67. el2.on("change.spectrum", function(e, color) {
  68. ok(true, "Change should fire input changing");
  69. });
  70. el2.spectrum("container").find(".sp-input").val("blue").trigger("change");
  71. el2.spectrum("destroy");
  72. });
  73. module("Defaults");
  74. test( "Default Color Is Set By Input Value", function() {
  75. var red = $("<input id='spec' value='red' />").spectrum();
  76. equal ( red.spectrum("get").toName(), "red", "Basic color setting");
  77. var noColor = $("<input id='spec' value='not a color' />").spectrum();
  78. equal ( noColor.spectrum("get").toHex(), "000000", "Defaults to black with an invalid color");
  79. var noValue = $("<input id='spec' />").spectrum();
  80. equal ( noValue.spectrum("get").toHex(), "000000", "Defaults to black with no value set");
  81. var noValueHex3 = $("<input id='spec' />").spectrum({
  82. preferredFormat: "hex3"
  83. });
  84. equal ( noValueHex3.spectrum("get").toHex(true), "000", "Defaults to 3 char hex with no value set");
  85. equal ( noValueHex3.spectrum("get").toString(), "#000", "Defaults to 3 char hex with no value set");
  86. red.spectrum("destroy");
  87. noColor.spectrum("destroy");
  88. noValue.spectrum("destroy");
  89. noValueHex3.spectrum("destroy");
  90. });
  91. module("Palettes");
  92. test( "Palette Events Fire In Correct Order ", function() {
  93. expect(2);
  94. var el = $("<input id='spec' value='red' />").spectrum({
  95. showPalette: true,
  96. palette: [
  97. ["red", "green", "blue"]
  98. ],
  99. move: function() {
  100. },
  101. });
  102. var count = 0;
  103. el.on("move.spectrum", function(e) {
  104. ok(count === 0, "move fires before change");
  105. count++;
  106. });
  107. el.on("change.spectrum", function(e) {
  108. ok(count === 1, "change fires after move");
  109. });
  110. el.spectrum("container").find(".sp-thumb-el:last-child").click();
  111. el.spectrum("destroy");
  112. });
  113. test( "Local Storage Is Limited ", function() {
  114. var el = $("<input id='spec' value='red' />").spectrum({
  115. localStorageKey: "spectrum.tests",
  116. palette: [["#ff0", "#0ff"]],
  117. maxSelectionSize: 3
  118. });
  119. el.spectrum("set", "#f00");
  120. el.spectrum("set", "#e00");
  121. el.spectrum("set", "#d00");
  122. el.spectrum("set", "#c00");
  123. el.spectrum("set", "#b00");
  124. el.spectrum("set", "#a00");
  125. equal (
  126. localStorage["spectrum.tests"],
  127. "rgb(204, 0, 0);rgb(187, 0, 0);rgb(170, 0, 0)",
  128. "Local storage array has been limited"
  129. );
  130. el.spectrum("set", "#ff0");
  131. el.spectrum("set", "#0ff");
  132. equal (
  133. localStorage["spectrum.tests"],
  134. "rgb(204, 0, 0);rgb(187, 0, 0);rgb(170, 0, 0)",
  135. "Local storage array did not get changed by selecting palette items"
  136. );
  137. el.spectrum("destroy");
  138. });
  139. module("Options");
  140. test ("replacerClassName", function() {
  141. var el = $("<input />").appendTo("body").spectrum({
  142. replacerClassName: "test"
  143. });
  144. ok (el.next(".sp-replacer").hasClass("test"), "Replacer class has been applied");
  145. el.spectrum("destroy").remove();
  146. });
  147. test ("containerClassName", function() {
  148. var el = $("<input />").appendTo("body").spectrum({
  149. containerClassName: "test"
  150. });
  151. ok (el.spectrum("container").hasClass("test"), "Container class has been applied");
  152. el.spectrum("destroy").remove();
  153. });
  154. test( "Options Can Be Set and Gotten Programmatically", function() {
  155. var spec = $("<input id='spec' />").spectrum({
  156. color: "#ECC",
  157. flat: true,
  158. showInput: true,
  159. className: "full-spectrum",
  160. showInitial: true,
  161. showPalette: true,
  162. showSelectionPalette: true,
  163. maxPaletteSize: 10,
  164. preferredFormat: "hex",
  165. localStorageKey: "spectrum.example",
  166. palette: [['red'], ['green']]
  167. });
  168. var allOptions = spec.spectrum("option");
  169. equal ( allOptions.flat, true, "Fetching all options provides accurate value");
  170. var singleOption = spec.spectrum("option", "className");
  171. equal ( singleOption, "full-spectrum", "Fetching a single option returns that value");
  172. spec.spectrum("option", "className", "changed");
  173. singleOption = spec.spectrum("option", "className");
  174. equal ( singleOption, "changed", "Changing an option then fetching it is updated");
  175. var numPaletteElements = spec.spectrum("container").find(".sp-palette-row:not(.sp-palette-row-selection) .sp-thumb-el").length;
  176. equal (numPaletteElements, 2, "Two palette elements to start");
  177. spec.spectrum("option", "palette", [['red'], ['green'], ['blue']]);
  178. var optPalette = spec.spectrum("option", "palette");
  179. deepEqual (optPalette, [['red'], ['green'], ['blue']], "Changing an option then fetching it is updated");
  180. var numPaletteElements = spec.spectrum("container").find(".sp-palette-row:not(.sp-palette-row-selection) .sp-thumb-el").length;
  181. equal (numPaletteElements, 3, "Three palette elements after updating");
  182. var appendToDefault = $("<input />").spectrum({
  183. });
  184. var container= $("<div id='c' />").appendTo("body");
  185. var appendToOther = $("<input />").spectrum({
  186. appendTo: container
  187. });
  188. var appendToParent = $("<input />").appendTo("#c").spectrum({
  189. appendTo: "parent"
  190. });
  191. var appendToOtherFlat = $("<input />").spectrum({
  192. appendTo: container,
  193. flat: true
  194. });
  195. equal ( appendToDefault.spectrum("container").parent()[0], document.body, "Appended to body by default");
  196. equal ( appendToOther.spectrum("container").parent()[0], container[0], "Can be appended to another element");
  197. equal ( appendToOtherFlat.spectrum("container").parent()[0], $(appendToOtherFlat).parent()[0], "Flat CANNOT be appended to another element, will be same as input");
  198. equal ( appendToParent.spectrum("container").parent()[0], container[0], "Passing 'parent' to appendTo works as expected");
  199. // Issue #70 - https://github.com/bgrins/spectrum/issues/70
  200. equal (spec.spectrum("option", "showPalette"), true, "showPalette is true by default");
  201. spec.spectrum("option", "showPalette", false);
  202. equal (spec.spectrum("option", "showPalette"), false, "showPalette is false after setting showPalette");
  203. spec.spectrum("option", "showPaletteOnly", true);
  204. equal (spec.spectrum("option", "showPaletteOnly"), true, "showPaletteOnly is true after setting showPaletteOnly");
  205. equal (spec.spectrum("option", "showPalette"), true, "showPalette is true after setting showPaletteOnly");
  206. spec.spectrum("destroy");
  207. appendToDefault.spectrum("destroy");
  208. appendToOther.spectrum("destroy");
  209. appendToOtherFlat.spectrum("destroy");
  210. appendToParent.spectrum("destroy").remove();
  211. delete window.localStorage["spectrum.example"];
  212. });
  213. test ("Show Input works as expected", function() {
  214. var el = $("<input />").spectrum({
  215. showInput: true,
  216. color: "red"
  217. });
  218. var input = el.spectrum("container").find(".sp-input");
  219. equal(input.val(), "red", "Input is set to color by default");
  220. input.val("").trigger("change");
  221. ok(input.hasClass("sp-validation-error"), "Input has validation error class after being emptied.");
  222. input.val("red").trigger("change");
  223. ok(!input.hasClass("sp-validation-error"), "Input does not have validation error class after being reset to original color.");
  224. equal (el.spectrum("get").toHexString(), "#ff0000", "Color is still red");
  225. el.spectrum("destroy");
  226. });
  227. test ("Tooltip is formatted based on preferred format", function() {
  228. var el = $("<input />").spectrum({
  229. showInput: true,
  230. color: "rgba(255, 255, 255, .5)",
  231. showPalette: true,
  232. palette: [["red", "rgba(255, 255, 255, .5)", "rgb(0, 0, 255)"]]
  233. });
  234. function getTitlesString() {
  235. return el.spectrum("container").find(".sp-thumb-el").map(function() {
  236. return this.getAttribute("title");
  237. }).toArray().join(" ");
  238. }
  239. equal (getTitlesString(), "rgb(255, 0, 0) rgba(255, 255, 255, 0.5) rgb(0, 0, 255)", "Titles use rgb format by default");
  240. el.spectrum("option", "preferredFormat", "hex");
  241. equal (getTitlesString(), "#ff0000 #ffffff #0000ff", "Titles are updated to hex");
  242. el.spectrum("option", "preferredFormat", "hex6");
  243. equal (getTitlesString(), "#ff0000 #ffffff #0000ff", "Titles are updated to hex6");
  244. el.spectrum("option", "preferredFormat", "hex3");
  245. equal (getTitlesString(), "#f00 #fff #00f", "Titles are updated to hex3");
  246. el.spectrum("option", "preferredFormat", "name");
  247. equal (getTitlesString(), "red white blue", "Titles are updated to name");
  248. el.spectrum("option", "preferredFormat", "hsv");
  249. equal (getTitlesString(), "hsv(0, 100%, 100%) hsva(0, 0%, 100%, 0.5) hsv(240, 100%, 100%)", "Titles are updated to hsv");
  250. el.spectrum("option", "preferredFormat", "hsl");
  251. equal (getTitlesString(), "hsl(0, 100%, 50%) hsla(0, 0%, 100%, 0.5) hsl(240, 100%, 50%)", "Titles are updated to hsl");
  252. el.spectrum("option", "preferredFormat", "rgb");
  253. equal (getTitlesString(), "rgb(255, 0, 0) rgba(255, 255, 255, 0.5) rgb(0, 0, 255)", "Titles are updated to rgb");
  254. el.spectrum("destroy");
  255. });
  256. module("Methods");
  257. test( "Methods work as described", function() {
  258. var el = $("<input id='spec' />").spectrum();
  259. // Method - show
  260. el.spectrum("show");
  261. ok (el.spectrum("container").is(":visible"), "Spectrum is visible");
  262. // Method - hide
  263. el.spectrum("hide");
  264. ok (el.spectrum("container").not(":visible"), "Spectrum is no longer visible");
  265. // Method - toggle
  266. el.spectrum("toggle");
  267. ok (el.spectrum("container").is(":visible"), "Spectrum is visible after toggle");
  268. el.spectrum("toggle");
  269. ok (el.spectrum("container").not(":visible"), "Spectrum is no longer visible after toggle");
  270. // Method - set / get
  271. el.spectrum("set", "orange");
  272. var color = el.spectrum("get", "color");
  273. ok (color.toHexString() == "#ffa500", "Color has been set and gotten as hex");
  274. ok (color.toName() == "orange", "Color has been set and gotten as name");
  275. ok (color.toHsvString() == "hsv(39, 100%, 100%)", "Color has been set and gotten as hsv");
  276. ok (color.toRgbString() == "rgb(255, 165, 0)", "Color has been set and gotten as rgb");
  277. ok (color.toHslString() == "hsl(39, 100%, 50%)", "Color has been set and gotten as hsl");
  278. // Method - container
  279. ok (el.spectrum("container").hasClass("sp-container"), "Container can be retrieved");
  280. // Method - disable
  281. el.spectrum("disable");
  282. ok (el.is(":disabled") , "Can be disabled");
  283. el.spectrum("show");
  284. ok (el.not(":visible") , "Cannot show when disabled");
  285. // Method - enable
  286. el.spectrum("enable");
  287. ok (!el.is(":disabled") , "Can be enabled");
  288. // Method - reflow
  289. el.spectrum("reflow");
  290. // Method - throw exception when not existing
  291. raises(function() {
  292. el.spectrum("no method");
  293. }, "Expecting exception to be thrown when calling with no method");
  294. // Method - destroy
  295. el.spectrum("destroy");
  296. equal (el.spectrum("container"), el , "No usage after being destroyed");
  297. equal (el.spectrum("get"), el , "No usage after being destroyed");
  298. el.spectrum("destroy");
  299. });
  300. // https://github.com/bgrins/spectrum/issues/97
  301. test( "Change events fire as described" , function() {
  302. expect(0);
  303. var input = $("<input />");
  304. input.on("change", function() {
  305. ok(false, "Change should not be fired inside of input change");
  306. });
  307. input.spectrum({
  308. color: "red",
  309. change: function() {
  310. ok (false, "Change should not be fired inside of spectrum callback");
  311. }
  312. });
  313. input.spectrum("set", "orange");
  314. });
  315. test("The selectedPalette should be updated in each spectrum instance, when storageKeys are identical.", function () {
  316. delete window.localStorage["spectrum.tests"];
  317. var colorToChoose = "rgb(0, 244, 0)";
  318. var firstEl = $("<input id='firstEl' value='red' />").spectrum({
  319. showPalette: true,
  320. localStorageKey: "spectrum.tests"
  321. });
  322. var secondEl = $("<input id='secondEl' value='blue' />").spectrum({
  323. showPalette: true,
  324. localStorageKey: "spectrum.tests"
  325. });
  326. firstEl.spectrum("set", colorToChoose);
  327. secondEl.spectrum("toggle");
  328. var selectedColor = secondEl.spectrum("container").find('span[data-color="' + colorToChoose + '"]');
  329. ok(selectedColor.length > 0, "Selected color is also shown in the others instance's palette.");
  330. delete window.localStorage["spectrum.tests"];
  331. firstEl.spectrum("destroy");
  332. secondEl.spectrum("destroy");
  333. });
  334. test("The selectedPalette should not be updated in spectrum instances that have different storageKeys.", function () {
  335. delete window.localStorage["spectrum.test_1"];
  336. delete window.localStorage["spectrum.test_2"];
  337. var colorToChoose = "rgb(0, 244, 0)";
  338. var firstEl = $("<input id='firstEl' value='red' />").spectrum({
  339. showPalette: true,
  340. localStorageKey: "spectrum.test_1"
  341. });
  342. var secondEl = $("<input id='secondEl' value='blue' />").spectrum({
  343. showPalette: true,
  344. localStorageKey: "spectrum.test_2"
  345. });
  346. firstEl.spectrum("set", colorToChoose);
  347. secondEl.spectrum("toggle");
  348. var selectedColor = secondEl.spectrum("container").find('span[data-color="' + colorToChoose + '"]');
  349. ok(selectedColor.length === 0, "Selected color should not be available in instances with other storageKey.");
  350. firstEl.spectrum("destroy");
  351. secondEl.spectrum("destroy");
  352. delete window.localStorage["spectrum.test_1"];
  353. delete window.localStorage["spectrum.test_2"];
  354. });