tests.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. // Spectrum Colorpicker Tests
  2. // https://github.com/bgrins/spectrum
  3. // Author: Brian Grinstead
  4. // License: MIT
  5. // Pretend like the color inputs aren't supported for initial load.
  6. $.fn.spectrum.inputTypeColorSupport = function() { return false; };
  7. module("Initialization");
  8. test( "jQuery Plugin Can Be Created", function() {
  9. var el = $("<input id='spec' />").spectrum();
  10. ok(el.attr("id") === "spec", "Element returned from plugin" );
  11. el.spectrum("set", "red");
  12. equal(el.spectrum("get").toName(), "red", "Basic color setting");
  13. equal(el.spectrum("option", "showInput"), false, "Undefined option is false.");
  14. el.spectrum({showInput: true});
  15. equal(el.spectrum("option", "showInput"), true, "Double initialized spectrum is destroyed before recreating.");
  16. el.spectrum("destroy");
  17. equal(el.spectrum("container"), el, "After destroying spectrum, string function returns input.");
  18. });
  19. test ("Polyfill", function() {
  20. var el = $("#type-color-on-page");
  21. ok (el.spectrum("get").toHex, "The input[type=color] has been initialized on load");
  22. el.spectrum("destroy");
  23. // Pretend like the color inputs are supported.
  24. $.fn.spectrum.inputTypeColorSupport = function() { return true; };
  25. el = $("<input type='color' value='red' />").spectrum({
  26. allowEmpty: true
  27. });
  28. el.spectrum("set", null);
  29. ok(el.spectrum("get"), "input[type] color does not allow null values");
  30. el.spectrum("destroy");
  31. });
  32. test( "Per-element Options Are Read From Data Attributes", function() {
  33. var el = $("<input id='spec' data-show-alpha='true' />").spectrum();
  34. equal ( el.spectrum("option", "showAlpha"), true, "Took showAlpha value from data attribute");
  35. el.spectrum("destroy");
  36. var changeDefault = $("<input id='spec' data-show-alpha='false' />").spectrum({
  37. showAlpha: true
  38. });
  39. equal ( changeDefault.spectrum("option", "showAlpha"), false, "Took showAlpha value from data attribute");
  40. changeDefault.spectrum("destroy");
  41. var noData = $("<input id='spec' />").spectrum({
  42. showAlpha: true
  43. });
  44. equal ( noData.spectrum("option", "showAlpha"), true, "Kept showAlpha without data attribute");
  45. noData.spectrum("destroy");
  46. });
  47. test( "Events Fire", function() {
  48. var el = $("<input id='spec' />").spectrum();
  49. var count = 0;
  50. expect(5);
  51. el.on("beforeShow.spectrum", function(e) {
  52. // Cancel the event the first time
  53. if (count === 0) {
  54. ok(true, "Cancel beforeShow");
  55. count++;
  56. return false;
  57. }
  58. ok (count === 1, "Allow beforeShow");
  59. count++;
  60. });
  61. el.on("show.spectrum", function(e) {
  62. ok(count === 2, "Show");
  63. count++;
  64. });
  65. el.on("hide.spectrum", function(e) {
  66. ok(count === 3, "Hide");
  67. count++;
  68. });
  69. el.on("move.spectrum", function(e) {
  70. });
  71. el.on("change", function(e, color) {
  72. ok(false, "Change should not fire from `set` call");
  73. });
  74. el.spectrum("show");
  75. el.spectrum("show");
  76. el.spectrum("hide");
  77. el.spectrum("set", "red");
  78. el.spectrum("destroy");
  79. var el2 = $("<input />").spectrum({
  80. showInput: true
  81. });
  82. el2.on("change.spectrum", function(e, color) {
  83. ok(true, "Change should fire input changing");
  84. });
  85. el2.spectrum("container").find(".sp-input").val("blue").trigger("change");
  86. el2.spectrum("destroy");
  87. });
  88. test( "Escape hides the colorpicker", function() {
  89. expect(1);
  90. var el = $("<input id='spec' />").spectrum();
  91. el.on("hide.spectrum", function(e) {
  92. ok(true, "Hide event should fire");
  93. });
  94. // Simulate an escape before showing -- should do nothing
  95. var e = jQuery.Event("keydown");
  96. e.keyCode = 27;
  97. $(document).trigger(e);
  98. el.spectrum("show");
  99. // Simulate an escape after showing -- should call the hide handler
  100. $(document).trigger(e);
  101. el.spectrum("destroy");
  102. });
  103. test( "Dragging", function() {
  104. var el = $("<input id='spec' />").spectrum();
  105. var hueSlider = el.spectrum("container").find(".sp-hue");
  106. ok (hueSlider.length, "There is a hue slider");
  107. hueSlider.trigger("mousedown");
  108. ok ($("body").hasClass("sp-dragging"), "The body has dragging class");
  109. hueSlider.trigger("mouseup");
  110. ok (!$("body").hasClass("sp-dragging"), "The body does not have a dragging class");
  111. el.spectrum("destroy");
  112. });
  113. module("Defaults");
  114. test( "Default Color Is Set By Input Value", function() {
  115. var red = $("<input id='spec' value='red' />").spectrum();
  116. equal ( red.spectrum("get").toName(), "red", "Basic color setting");
  117. var noColor = $("<input id='spec' value='not a color' />").spectrum();
  118. equal ( noColor.spectrum("get").toHex(), "000000", "Defaults to black with an invalid color");
  119. var noValue = $("<input id='spec' />").spectrum();
  120. equal ( noValue.spectrum("get").toHex(), "000000", "Defaults to black with no value set");
  121. var noValueHex3 = $("<input id='spec' />").spectrum({
  122. preferredFormat: "hex3"
  123. });
  124. equal ( noValueHex3.spectrum("get").toHex(true), "000", "Defaults to 3 char hex with no value set");
  125. equal ( noValueHex3.spectrum("get").toString(), "#000", "Defaults to 3 char hex with no value set");
  126. red.spectrum("destroy");
  127. noColor.spectrum("destroy");
  128. noValue.spectrum("destroy");
  129. noValueHex3.spectrum("destroy");
  130. });
  131. module("Palettes");
  132. test( "Palette Events Fire In Correct Order ", function() {
  133. expect(2);
  134. var el = $("<input id='spec' value='red' />").spectrum({
  135. showPalette: true,
  136. palette: [
  137. ["red", "green", "blue"]
  138. ],
  139. move: function() {
  140. }
  141. });
  142. var count = 0;
  143. el.on("move.spectrum", function(e) {
  144. ok(count === 0, "move fires before change");
  145. count++;
  146. });
  147. el.on("change.spectrum", function(e) {
  148. ok(count === 1, "change fires after move");
  149. });
  150. el.spectrum("container").find(".sp-thumb-el:last-child").click();
  151. el.spectrum("destroy");
  152. });
  153. test( "Palette click events work ", function() {
  154. var el = $("<input id='spec' value='red' />").spectrum({
  155. showPalette: true,
  156. palette: [
  157. ["red", "green", "blue"]
  158. ],
  159. move: function() {
  160. }
  161. });
  162. el.spectrum("container").find(".sp-thumb-el:nth-child(3)").click();
  163. equal (el.spectrum("get").toName(), "blue", "First click worked");
  164. el.spectrum("container").find(".sp-thumb-el:nth-child(2) .sp-thumb-inner").click();
  165. equal (el.spectrum("get").toName(), "green", "Second click worked (on child element)");
  166. el.spectrum("container").find(".sp-thumb-el:nth-child(1) .sp-thumb-inner").click();
  167. equal (el.spectrum("get").toName(), "red", "Third click worked (on child element)");
  168. el.spectrum("destroy");
  169. });
  170. test( "hideAfterPaletteSelect: Palette stays open after color select", function() {
  171. var el = $("<input id='spec' value='red' />").spectrum({
  172. showPalette: true,
  173. hideAfterPaletteSelect: false,
  174. palette: [
  175. ["red", "green", "blue"]
  176. ]
  177. });
  178. el.spectrum("show");
  179. el.spectrum("container").find(".sp-thumb-el:nth-child(1)").click();
  180. ok(!el.spectrum("container").hasClass('sp-hidden'), "palette is still visible after color selection");
  181. el.spectrum("destroy");
  182. });
  183. test( "hideAfterPaletteSelect: Palette closes after color select", function() {
  184. var el = $("<input id='spec' value='red' />").spectrum({
  185. showPalette: true,
  186. hideAfterPaletteSelect: true,
  187. palette: [
  188. ["red", "green", "blue"]
  189. ]
  190. });
  191. el.spectrum("show");
  192. el.spectrum("container").find(".sp-thumb-el:nth-child(1)").click();
  193. ok(el.spectrum("container").hasClass('sp-hidden'), "palette is still hidden after color selection");
  194. el.spectrum("destroy");
  195. });
  196. test( "Local Storage Is Limited ", function() {
  197. var el = $("<input id='spec' value='red' />").spectrum({
  198. localStorageKey: "spectrum.tests",
  199. palette: [["#ff0", "#0ff"]],
  200. maxSelectionSize: 3
  201. });
  202. el.spectrum("set", "#f00");
  203. el.spectrum("set", "#e00");
  204. el.spectrum("set", "#d00");
  205. el.spectrum("set", "#c00");
  206. el.spectrum("set", "#b00");
  207. el.spectrum("set", "#a00");
  208. equal (
  209. localStorage["spectrum.tests"],
  210. "rgb(204, 0, 0);rgb(187, 0, 0);rgb(170, 0, 0)",
  211. "Local storage array has been limited"
  212. );
  213. el.spectrum("set", "#ff0");
  214. el.spectrum("set", "#0ff");
  215. equal (
  216. localStorage["spectrum.tests"],
  217. "rgb(204, 0, 0);rgb(187, 0, 0);rgb(170, 0, 0)",
  218. "Local storage array did not get changed by selecting palette items"
  219. );
  220. el.spectrum("destroy");
  221. });
  222. module("Options");
  223. test ("allowEmpty", function() {
  224. var el = $("<input value='red' />").spectrum({
  225. allowEmpty: true
  226. });
  227. el.spectrum("set", null);
  228. ok(!el.spectrum("get"), "input[type] color does not allow null values");
  229. el.spectrum("destroy");
  230. el = $("<input value='red' />").spectrum();
  231. el.spectrum("set", null);
  232. ok(el.spectrum("get"), "input[type] color does not allow null values");
  233. el.spectrum("destroy");
  234. });
  235. test ("clickoutFiresChange", function() {
  236. var el = $("<input value='red' />").spectrum({
  237. clickoutFiresChange: false
  238. });
  239. el.spectrum("show");
  240. equal ( el.spectrum("get").toName(), "red", "Color is initialized");
  241. el.spectrum("set", "orange");
  242. equal ( el.spectrum("get").toName(), "orange", "Color is set");
  243. $(document).click();
  244. equal ( el.spectrum("get").toName(), "red", "Color is reverted after clicking 'cancel'");
  245. el.spectrum("destroy");
  246. // Try again with default behavior (clickoutFiresChange = true)
  247. el = $("<input value='red' />").spectrum();
  248. el.spectrum("show");
  249. equal ( el.spectrum("get").toName(), "red", "Color is initialized");
  250. el.spectrum("set", "orange");
  251. equal ( el.spectrum("get").toName(), "orange", "Color is set");
  252. $(document).click();
  253. equal ( el.spectrum("get").toName(), "orange", "Color is changed after clicking out");
  254. el.spectrum("destroy");
  255. });
  256. test ("replacerClassName", function() {
  257. var el = $("<input />").appendTo("body").spectrum({
  258. replacerClassName: "test"
  259. });
  260. ok (el.next(".sp-replacer").hasClass("test"), "Replacer class has been applied");
  261. el.spectrum("destroy").remove();
  262. });
  263. test ("containerClassName", function() {
  264. var el = $("<input />").appendTo("body").spectrum({
  265. containerClassName: "test"
  266. });
  267. ok (el.spectrum("container").hasClass("test"), "Container class has been applied");
  268. el.spectrum("destroy").remove();
  269. });
  270. test( "Options Can Be Set and Gotten Programmatically", function() {
  271. var spec = $("<input id='spec' />").spectrum({
  272. color: "#ECC",
  273. flat: true,
  274. showInput: true,
  275. className: "full-spectrum",
  276. showInitial: true,
  277. showPalette: true,
  278. showSelectionPalette: true,
  279. maxPaletteSize: 10,
  280. preferredFormat: "hex",
  281. localStorageKey: "spectrum.example",
  282. palette: [['red'], ['green']]
  283. });
  284. var allOptions = spec.spectrum("option");
  285. equal ( allOptions.flat, true, "Fetching all options provides accurate value");
  286. var singleOption = spec.spectrum("option", "className");
  287. equal ( singleOption, "full-spectrum", "Fetching a single option returns that value");
  288. spec.spectrum("option", "className", "changed");
  289. singleOption = spec.spectrum("option", "className");
  290. equal ( singleOption, "changed", "Changing an option then fetching it is updated");
  291. var numPaletteElements = spec.spectrum("container").find(".sp-palette-row:not(.sp-palette-row-selection) .sp-thumb-el").length;
  292. equal (numPaletteElements, 2, "Two palette elements to start");
  293. spec.spectrum("option", "palette", [['red'], ['green'], ['blue']]);
  294. var optPalette = spec.spectrum("option", "palette");
  295. deepEqual (optPalette, [['red'], ['green'], ['blue']], "Changing an option then fetching it is updated");
  296. numPaletteElements = spec.spectrum("container").find(".sp-palette-row:not(.sp-palette-row-selection) .sp-thumb-el").length;
  297. equal (numPaletteElements, 3, "Three palette elements after updating");
  298. var appendToDefault = $("<input />").spectrum({
  299. });
  300. var container = $("<div id='c' />").appendTo("body");
  301. var appendToOther = $("<input />").spectrum({
  302. appendTo: container
  303. });
  304. var appendToParent = $("<input />").appendTo("#c").spectrum({
  305. appendTo: "parent"
  306. });
  307. var appendToOtherFlat = $("<input />").spectrum({
  308. appendTo: container,
  309. flat: true
  310. });
  311. equal ( appendToDefault.spectrum("container").parent()[0], document.body, "Appended to body by default");
  312. equal ( appendToOther.spectrum("container").parent()[0], container[0], "Can be appended to another element");
  313. equal ( appendToOtherFlat.spectrum("container").parent()[0], $(appendToOtherFlat).parent()[0], "Flat CANNOT be appended to another element, will be same as input");
  314. equal ( appendToParent.spectrum("container").parent()[0], container[0], "Passing 'parent' to appendTo works as expected");
  315. // Issue #70 - https://github.com/bgrins/spectrum/issues/70
  316. equal (spec.spectrum("option", "showPalette"), true, "showPalette is true by default");
  317. spec.spectrum("option", "showPalette", false);
  318. equal (spec.spectrum("option", "showPalette"), false, "showPalette is false after setting showPalette");
  319. spec.spectrum("option", "showPaletteOnly", true);
  320. equal (spec.spectrum("option", "showPaletteOnly"), true, "showPaletteOnly is true after setting showPaletteOnly");
  321. equal (spec.spectrum("option", "showPalette"), true, "showPalette is true after setting showPaletteOnly");
  322. spec.spectrum("destroy");
  323. appendToDefault.spectrum("destroy");
  324. appendToOther.spectrum("destroy");
  325. appendToOtherFlat.spectrum("destroy");
  326. appendToParent.spectrum("destroy").remove();
  327. delete window.localStorage["spectrum.example"];
  328. container.remove();
  329. });
  330. test ("Show Input works as expected", function() {
  331. var el = $("<input />").spectrum({
  332. showInput: true,
  333. color: "red"
  334. });
  335. var input = el.spectrum("container").find(".sp-input");
  336. equal(input.val(), "red", "Input is set to color by default");
  337. input.val("").trigger("change");
  338. ok(input.hasClass("sp-validation-error"), "Input has validation error class after being emptied.");
  339. input.val("red").trigger("change");
  340. ok(!input.hasClass("sp-validation-error"), "Input does not have validation error class after being reset to original color.");
  341. equal (el.spectrum("get").toHexString(), "#ff0000", "Color is still red");
  342. el.spectrum("destroy");
  343. });
  344. test ("Toggle Picker Area button works as expected", function() {
  345. var div = $("<div style='position:absolute; right:0; height:20px; width:150px'>").appendTo('body').show(),
  346. el = $("<input />").appendTo(div);
  347. el.spectrum({
  348. showInput: true,
  349. showPaletteOnly: true,
  350. togglePaletteOnly: true,
  351. color: "red"
  352. });
  353. var spectrum = el.spectrum("container").show(),
  354. toggle = spectrum.find(".sp-palette-toggle"),
  355. picker = spectrum.find(".sp-picker-container"),
  356. palette = spectrum.find(".sp-palette-container");
  357. // Open the Colorpicker
  358. el.spectrum("show");
  359. equal(picker.is(":hidden"), true, "The picker area is hidden by default.");
  360. ok(spectrum.hasClass("sp-palette-only"), "The 'palette-only' class is enabled.");
  361. // Click the Picker area Toggle button to show the Picker
  362. toggle.click();
  363. equal(picker.is(":hidden"), false, "After toggling, the picker area is visible.");
  364. ok(!spectrum.hasClass("sp-palette-only"), "The 'palette-only' class is disabled.");
  365. equal(Math.round(picker.offset().top), Math.round(palette.offset().top), "The picker area is next to the palette.");
  366. // Click the toggle again to hide the picker
  367. toggle.trigger("click");
  368. equal(picker.is(":hidden"), true, "After toggling again, the picker area is hidden.");
  369. ok(spectrum.hasClass("sp-palette-only"), "And the 'palette-only' class is enabled.");
  370. // Cleanup
  371. el.spectrum("hide");
  372. el.spectrum("destroy");
  373. el.remove();
  374. div.remove();
  375. });
  376. test ("Tooltip is formatted based on preferred format", function() {
  377. var el = $("<input />").spectrum({
  378. showInput: true,
  379. color: "red",
  380. showPalette: true,
  381. palette: [["red", "rgba(255, 255, 255, .5)", "rgb(0, 0, 255)"]]
  382. });
  383. el.spectrum("show");
  384. function getTitlesString() {
  385. return el.spectrum("container").find(".sp-thumb-el").map(function() {
  386. return this.getAttribute("title");
  387. }).toArray().join(" ");
  388. }
  389. equal (getTitlesString(), "rgb(255, 0, 0) rgba(255, 255, 255, 0.5) rgb(0, 0, 255)", "Titles use rgb format by default");
  390. el.spectrum("option", "preferredFormat", "hex");
  391. equal (getTitlesString(), "#ff0000 #ffffff #0000ff", "Titles are updated to hex");
  392. equal (el.spectrum("get").toString(), "#ff0000", "Value's format is updated");
  393. el.spectrum("option", "preferredFormat", "hex6");
  394. equal (getTitlesString(), "#ff0000 #ffffff #0000ff", "Titles are updated to hex6");
  395. equal (el.spectrum("get").toString(), "#ff0000", "Value's format is updated");
  396. el.spectrum("option", "preferredFormat", "hex3");
  397. equal (getTitlesString(), "#f00 #fff #00f", "Titles are updated to hex3");
  398. equal (el.spectrum("get").toString(), "#f00", "Value's format is updated");
  399. el.spectrum("option", "preferredFormat", "name");
  400. equal (getTitlesString(), "red #ffffff blue", "Titles are updated to name");
  401. equal (el.spectrum("get").toString(), "red", "Value's format is updated");
  402. el.spectrum("option", "preferredFormat", "hsv");
  403. equal (getTitlesString(), "hsv(0, 100%, 100%) hsva(0, 0%, 100%, 0.5) hsv(240, 100%, 100%)", "Titles are updated to hsv");
  404. equal (el.spectrum("get").toString(), "hsv(0, 100%, 100%)", "Value's format is updated");
  405. el.spectrum("option", "preferredFormat", "hsl");
  406. equal (getTitlesString(), "hsl(0, 100%, 50%) hsla(0, 0%, 100%, 0.5) hsl(240, 100%, 50%)", "Titles are updated to hsl");
  407. equal (el.spectrum("get").toString(), "hsl(0, 100%, 50%)", "Value's format is updated");
  408. el.spectrum("option", "preferredFormat", "rgb");
  409. equal (getTitlesString(), "rgb(255, 0, 0) rgba(255, 255, 255, 0.5) rgb(0, 0, 255)", "Titles are updated to rgb");
  410. equal (el.spectrum("get").toString(), "rgb(255, 0, 0)", "Value's format is updated");
  411. el.spectrum("destroy");
  412. });
  413. module("Methods");
  414. test( "Methods work as described", function() {
  415. var el = $("<input id='spec' />").spectrum();
  416. // Method - show
  417. el.spectrum("show");
  418. ok (el.spectrum("container").is(":visible"), "Spectrum is visible");
  419. // Method - hide
  420. el.spectrum("hide");
  421. ok (el.spectrum("container").not(":visible"), "Spectrum is no longer visible");
  422. // Method - toggle
  423. el.spectrum("toggle");
  424. ok (el.spectrum("container").is(":visible"), "Spectrum is visible after toggle");
  425. el.spectrum("toggle");
  426. ok (el.spectrum("container").not(":visible"), "Spectrum is no longer visible after toggle");
  427. // Method - set / get
  428. el.spectrum("set", "orange");
  429. var color = el.spectrum("get", "color");
  430. ok (color.toHexString() == "#ffa500", "Color has been set and gotten as hex");
  431. ok (color.toName() == "orange", "Color has been set and gotten as name");
  432. ok (color.toHsvString() == "hsv(39, 100%, 100%)", "Color has been set and gotten as hsv");
  433. ok (color.toRgbString() == "rgb(255, 165, 0)", "Color has been set and gotten as rgb");
  434. ok (color.toHslString() == "hsl(39, 100%, 50%)", "Color has been set and gotten as hsl");
  435. // Method - container
  436. ok (el.spectrum("container").hasClass("sp-container"), "Container can be retrieved");
  437. // Method - disable
  438. el.spectrum("disable");
  439. ok (el.is(":disabled") , "Can be disabled");
  440. el.spectrum("show");
  441. ok (el.not(":visible") , "Cannot show when disabled");
  442. // Method - enable
  443. el.spectrum("enable");
  444. ok (!el.is(":disabled") , "Can be enabled");
  445. // Method - reflow
  446. el.spectrum("reflow");
  447. // Method - throw exception when not existing
  448. raises(function() {
  449. el.spectrum("no method");
  450. }, "Expecting exception to be thrown when calling with no method");
  451. // Method - destroy
  452. el.spectrum("destroy");
  453. equal (el.spectrum("container"), el , "No usage after being destroyed");
  454. equal (el.spectrum("get"), el , "No usage after being destroyed");
  455. el.spectrum("destroy");
  456. });
  457. // https://github.com/bgrins/spectrum/issues/97
  458. test( "Change events fire as described" , function() {
  459. expect(0);
  460. var input = $("<input />");
  461. input.on("change", function() {
  462. ok(false, "Change should not be fired inside of input change");
  463. });
  464. input.spectrum({
  465. color: "red",
  466. change: function() {
  467. ok (false, "Change should not be fired inside of spectrum callback");
  468. }
  469. });
  470. input.spectrum("set", "orange");
  471. });
  472. test("The selectedPalette should be updated in each spectrum instance, when storageKeys are identical.", function () {
  473. delete window.localStorage["spectrum.tests"];
  474. var colorToChoose = "rgb(0, 244, 0)";
  475. var firstEl = $("<input id='firstEl' value='red' />").spectrum({
  476. showPalette: true,
  477. localStorageKey: "spectrum.tests"
  478. });
  479. var secondEl = $("<input id='secondEl' value='blue' />").spectrum({
  480. showPalette: true,
  481. localStorageKey: "spectrum.tests"
  482. });
  483. firstEl.spectrum("set", colorToChoose);
  484. secondEl.spectrum("toggle");
  485. var selectedColor = secondEl.spectrum("container").find('span[data-color="' + colorToChoose + '"]');
  486. ok(selectedColor.length > 0, "Selected color is also shown in the others instance's palette.");
  487. delete window.localStorage["spectrum.tests"];
  488. firstEl.spectrum("destroy");
  489. secondEl.spectrum("destroy");
  490. });
  491. test("The selectedPalette should not be updated in spectrum instances that have different storageKeys.", function () {
  492. delete window.localStorage["spectrum.test_1"];
  493. delete window.localStorage["spectrum.test_2"];
  494. var colorToChoose = "rgb(0, 244, 0)";
  495. var firstEl = $("<input id='firstEl' value='red' />").spectrum({
  496. showPalette: true,
  497. localStorageKey: "spectrum.test_1"
  498. });
  499. var secondEl = $("<input id='secondEl' value='blue' />").spectrum({
  500. showPalette: true,
  501. localStorageKey: "spectrum.test_2"
  502. });
  503. firstEl.spectrum("set", colorToChoose);
  504. secondEl.spectrum("toggle");
  505. var selectedColor = secondEl.spectrum("container").find('span[data-color="' + colorToChoose + '"]');
  506. ok(selectedColor.length === 0, "Selected color should not be available in instances with other storageKey.");
  507. firstEl.spectrum("destroy");
  508. secondEl.spectrum("destroy");
  509. delete window.localStorage["spectrum.test_1"];
  510. delete window.localStorage["spectrum.test_2"];
  511. });
  512. test( "Cancelling reverts color", function() {
  513. var el = $("<input value='red' />").spectrum();
  514. el.spectrum("show");
  515. equal ( el.spectrum("get").toName(), "red", "Color is initialized");
  516. el.spectrum("set", "orange");
  517. equal ( el.spectrum("get").toName(), "orange", "Color is set");
  518. el.spectrum("container").find(".sp-cancel").click();
  519. equal ( el.spectrum("get").toName(), "red", "Color is reverted after clicking 'cancel'");
  520. el.spectrum("destroy");
  521. });
  522. test( "Choosing updates the color", function() {
  523. var el = $("<input value='red' />").spectrum();
  524. el.spectrum("show");
  525. equal ( el.spectrum("get").toName(), "red", "Color is initialized");
  526. el.spectrum("set", "orange");
  527. equal ( el.spectrum("get").toName(), "orange", "Color is set");
  528. el.spectrum("container").find(".sp-choose").click();
  529. equal ( el.spectrum("get").toName(), "orange", "Color is kept after clicking 'choose'");
  530. el.spectrum("destroy");
  531. });
  532. test( "Custom offset", function() {
  533. var el = $("<input value='red' />").spectrum();
  534. el.spectrum("show");
  535. deepEqual (el.spectrum("container").offset(), {top: 0, left: 0});
  536. el.spectrum("hide");
  537. el.spectrum("offset", {top: 10, left: 10});
  538. el.spectrum("show");
  539. deepEqual (el.spectrum("container").offset(), {top: 10, left: 10});
  540. el.spectrum("hide");
  541. el.spectrum("offset", null);
  542. el.spectrum("show");
  543. deepEqual (el.spectrum("container").offset(), {top: 0, left: 0});
  544. el.spectrum("hide");
  545. el.spectrum("destroy");
  546. var el2 = $("<input value='red' />").spectrum({
  547. offset: { top: 100, left: 100 }
  548. });
  549. el2.spectrum("show");
  550. deepEqual (el2.spectrum("container").offset(), {top: 100, left: 100});
  551. el2.spectrum("hide");
  552. });