// Spectrum Colorpicker Tests // https://github.com/bgrins/spectrum // Author: Brian Grinstead // License: MIT // Pretend like the color inputs aren't supported for initial load. $.fn.spectrum.inputTypeColorSupport = function() { return false; }; module("Initialization"); test( "jQuery Plugin Can Be Created", function() { var el = $("").spectrum(); ok(el.attr("id") === "spec", "Element returned from plugin" ); el.spectrum("set", "red"); equal(el.spectrum("get").toName(), "red", "Basic color setting"); equal(el.spectrum("option", "showInput"), false, "Undefined option is false."); el.spectrum({showInput: true}); equal(el.spectrum("option", "showInput"), true, "Double initialized spectrum is destroyed before recreating."); el.spectrum("destroy"); equal(el.spectrum("container"), el, "After destroying spectrum, string function returns input."); }); test ("Polyfill", function() { var el = $("#type-color-on-page"); ok (el.spectrum("get").toHex, "The input[type=color] has been initialized on load"); el.spectrum("destroy"); // Pretend like the color inputs are supported. $.fn.spectrum.inputTypeColorSupport = function() { return true; }; el = $("").spectrum({ allowEmpty: true }); el.spectrum("set", null); ok(el.spectrum("get"), "input[type] color does not allow null values"); el.spectrum("destroy"); }); test( "Per-element Options Are Read From Data Attributes", function() { var el = $("").spectrum(); equal ( el.spectrum("option", "showAlpha"), true, "Took showAlpha value from data attribute"); el.spectrum("destroy"); var changeDefault = $("").spectrum({ showAlpha: true }); equal ( changeDefault.spectrum("option", "showAlpha"), true, "Took showAlpha value from options arg"); changeDefault.spectrum("destroy"); var noData = $("").spectrum({ showAlpha: true }); equal ( noData.spectrum("option", "showAlpha"), true, "Kept showAlpha without data attribute"); noData.spectrum("destroy"); }); test( "Events Fire", function() { expect(4); var count = 0; var el = $("").spectrum(); el.on("beforeShow.spectrum", function(e) { // Cancel the event the first time if (count === 0) { ok(true, "Cancel beforeShow"); count++; return false; } equal(count, 1, "Allow beforeShow"); count++; }); el.on("show.spectrum", function(e) { equal(count, 2, "Show"); count++; }); el.on("hide.spectrum", function(e) { equal(count, 3, "Hide"); count++; }); el.on("move.spectrum", function(e) { ok(false, "Change should not fire from `move` call"); }); el.on("change", function(e, color) { ok(false, "Change should not fire from `set` call"); }); el.spectrum("show"); el.spectrum("show"); el.spectrum("hide"); el.spectrum("set", "red"); el.spectrum("destroy"); }); test( "Events Fire (text input change)", function() { expect(3); var count = 0; var el = $("").spectrum({ showInput: true }); el.on("move.spectrum", function(e, color) { equal(count, 0, "Move fires when input changes"); count++; }); el.on("change.spectrum", function(e, color) { equal(count, 2, "Change should not fire when input changes, only when chosen"); count++; }); el.spectrum("container").find(".sp-input").val("blue").trigger("change"); count++; el.spectrum("container").find(".sp-choose").click(); el.spectrum("destroy"); equal(count, 3, "All events fired"); }); test( "Escape hides the colorpicker", function() { expect(1); var el = $("").spectrum(); el.on("hide.spectrum", function(e) { ok(true, "Hide event should fire"); }); // Simulate an escape before showing -- should do nothing var e = jQuery.Event("keydown"); e.keyCode = 27; $(document).trigger(e); el.spectrum("show"); // Simulate an escape after showing -- should call the hide handler $(document).trigger(e); el.spectrum("destroy"); }); test( "Dragging", function() { var el = $("").spectrum(); var hueSlider = el.spectrum("container").find(".sp-hue"); ok (hueSlider.length, "There is a hue slider"); hueSlider.trigger("mousedown"); ok ($("body").hasClass("sp-dragging"), "The body has dragging class"); hueSlider.trigger("mouseup"); ok (!$("body").hasClass("sp-dragging"), "The body does not have a dragging class"); el.spectrum("destroy"); }); module("Defaults"); test( "Default Color Is Set By Input Value", function() { var red = $("").spectrum(); equal ( red.spectrum("get").toName(), "red", "Basic color setting"); var noColor = $("").spectrum(); equal ( noColor.spectrum("get").toHex(), "000000", "Defaults to black with an invalid color"); var noValue = $("").spectrum(); equal ( noValue.spectrum("get").toHex(), "000000", "Defaults to black with no value set"); var noValueHex3 = $("").spectrum({ preferredFormat: "hex3" }); equal ( noValueHex3.spectrum("get").toHex(true), "000", "Defaults to 3 char hex with no value set"); equal ( noValueHex3.spectrum("get").toString(), "#000", "Defaults to 3 char hex with no value set"); red.spectrum("destroy"); noColor.spectrum("destroy"); noValue.spectrum("destroy"); noValueHex3.spectrum("destroy"); }); module("Palettes"); test( "Palette Events Fire In Correct Order ", function() { expect(4); var el = $("").spectrum({ showPalette: true, palette: [ ["red", "green", "blue"] ], move: function() { } }); var count = 0; el.on("move.spectrum", function(e) { equal(count, 0, "move fires before change"); count++; }); el.on("change.spectrum", function(e) { equal(count, 1, "change fires after move"); count++; }); el.spectrum("container").find(".sp-thumb-el:last-child").click(); equal(count, 1, "Change event hasn't fired after palette click"); el.spectrum("container").find(".sp-choose").click(); equal(count, 2, "Change event has fired after choose button click"); el.spectrum("destroy"); }); test( "Palette click events work", function() { expect(7); var moveCount = 0; var moves = ["blue", "green", "red"]; var changeCount = 0; var el = $("").spectrum({ showPalette: true, preferredFormat: "name", palette: [ ["red", "green", "blue"] ], show: function(c) { equal(c.toName(), "orange", "correct shown color"); }, move: function(c) { equal(c.toName(), moves[moveCount], "Move # " + moveCount + " is correct"); moveCount++; }, change: function(c) { equal(changeCount, 0, "Only one change happens"); equal(c.toName(), "red"); changeCount++; } }).spectrum("show"); el.spectrum("container").find(".sp-thumb-el:nth-child(3)").click(); el.spectrum("container").find(".sp-thumb-el:nth-child(2) .sp-thumb-inner").click(); el.spectrum("container").find(".sp-thumb-el:nth-child(1) .sp-thumb-inner").click(); el.spectrum("container").find(".sp-choose").click(); equal(el.val(), "red", "Element's value is set"); el.spectrum("destroy"); }); test( "Palette doesn't changes don't stick if cancelled", function() { expect(4); var moveCount = 0; var moves = ["blue", "green", "red", "orange"]; var changeCount = 0; var el = $("").spectrum({ showPalette: true, preferredFormat: "name", palette: [ ["red", "green", "blue"] ], move: function(c) { equal(c.toName(), moves[moveCount], "Move # " + moveCount + " is correct"); moveCount++; }, change: function(c) { ok(false, "No change fires"); } }).spectrum("show"); el.spectrum("container").find(".sp-thumb-el:nth-child(3)").click(); el.spectrum("container").find(".sp-thumb-el:nth-child(2)").click(); el.spectrum("container").find(".sp-thumb-el:nth-child(1)").click(); el.spectrum("container").find(".sp-cancel").click(); equal(el.val(), "orange", "Element's value is the same"); el.spectrum("destroy"); }); test( "hideAfterPaletteSelect: Palette stays open after color select when false", function() { var el = $("").spectrum({ showPalette: true, hideAfterPaletteSelect: false, palette: [ ["red", "green", "blue"] ] }); el.spectrum("show"); el.spectrum("container").find(".sp-thumb-el:nth-child(1)").click(); ok(!el.spectrum("container").hasClass('sp-hidden'), "palette is still visible after color selection"); el.spectrum("destroy"); }); test( "hideAfterPaletteSelect: Palette closes after color select when true", function() { expect(2); var el = $("").spectrum({ showPalette: true, hideAfterPaletteSelect: true, change: function(c) { equal(c.toName(), "red", "change fires"); }, palette: [ ["red", "green", "blue"] ] }); el.spectrum("show"); el.spectrum("container").find(".sp-thumb-el:nth-child(1)").click(); ok(el.spectrum("container").hasClass('sp-hidden'), "palette is still hidden after color selection"); el.spectrum("destroy"); }); test( "Local Storage Is Limited ", function() { var el = $("").spectrum({ localStorageKey: "spectrum.tests", palette: [["#ff0", "#0ff"]], maxSelectionSize: 3 }); el.spectrum("set", "#f00"); el.spectrum("set", "#e00"); el.spectrum("set", "#d00"); el.spectrum("set", "#c00"); el.spectrum("set", "#b00"); el.spectrum("set", "#a00"); equal ( localStorage["spectrum.tests"], "rgb(204, 0, 0);rgb(187, 0, 0);rgb(170, 0, 0)", "Local storage array has been limited" ); el.spectrum("set", "#ff0"); el.spectrum("set", "#0ff"); equal ( localStorage["spectrum.tests"], "rgb(204, 0, 0);rgb(187, 0, 0);rgb(170, 0, 0)", "Local storage array did not get changed by selecting palette items" ); el.spectrum("destroy"); }); module("Options"); test ("allowEmpty", function() { var el = $("").spectrum({ allowEmpty: true }); el.spectrum("set", null); ok(!el.spectrum("get"), "input[type] color does not allow null values"); el.spectrum("destroy"); el = $("").spectrum(); el.spectrum("set", null); ok(el.spectrum("get"), "input[type] color does not allow null values"); el.spectrum("destroy"); }); test ("clickoutFiresChange", function() { var el = $("").spectrum({ clickoutFiresChange: false }); el.spectrum("show"); equal ( el.spectrum("get").toName(), "red", "Color is initialized"); el.spectrum("set", "orange"); equal ( el.spectrum("get").toName(), "orange", "Color is set"); $(document).click(); equal ( el.spectrum("get").toName(), "red", "Color is reverted after clicking 'cancel'"); el.spectrum("destroy"); // Try again with default behavior (clickoutFiresChange = true) el = $("").spectrum(); el.spectrum("show"); equal ( el.spectrum("get").toName(), "red", "Color is initialized"); el.spectrum("set", "orange"); equal ( el.spectrum("get").toName(), "orange", "Color is set"); $(document).click(); equal ( el.spectrum("get").toName(), "orange", "Color is changed after clicking out"); el.spectrum("destroy"); }); test ("replacerClassName", function() { var el = $("").appendTo("body").spectrum({ replacerClassName: "test" }); ok (el.next(".sp-replacer").hasClass("test"), "Replacer class has been applied"); el.spectrum("destroy").remove(); }); test ("containerClassName", function() { var el = $("").appendTo("body").spectrum({ containerClassName: "test" }); ok (el.spectrum("container").hasClass("test"), "Container class has been applied"); el.spectrum("destroy").remove(); }); test( "Options Can Be Set and Gotten Programmatically", function() { var spec = $("").spectrum({ color: "#ECC", flat: true, showInput: true, className: "full-spectrum", showInitial: true, showPalette: true, showSelectionPalette: true, maxPaletteSize: 10, preferredFormat: "hex", localStorageKey: "spectrum.example", palette: [['red'], ['green']] }); var allOptions = spec.spectrum("option"); equal ( allOptions.flat, true, "Fetching all options provides accurate value"); var singleOption = spec.spectrum("option", "className"); equal ( singleOption, "full-spectrum", "Fetching a single option returns that value"); spec.spectrum("option", "className", "changed"); singleOption = spec.spectrum("option", "className"); equal ( singleOption, "changed", "Changing an option then fetching it is updated"); var numPaletteElements = spec.spectrum("container").find(".sp-palette-row:not(.sp-palette-row-selection) .sp-thumb-el").length; equal (numPaletteElements, 2, "Two palette elements to start"); spec.spectrum("option", "palette", [['red'], ['green'], ['blue']]); var optPalette = spec.spectrum("option", "palette"); deepEqual (optPalette, [['red'], ['green'], ['blue']], "Changing an option then fetching it is updated"); numPaletteElements = spec.spectrum("container").find(".sp-palette-row:not(.sp-palette-row-selection) .sp-thumb-el").length; equal (numPaletteElements, 3, "Three palette elements after updating"); var appendToDefault = $("").spectrum({ }); var container = $("
").appendTo("body"); var appendToOther = $("").spectrum({ appendTo: container }); var appendToParent = $("").appendTo("#c").spectrum({ appendTo: "parent" }); var appendToOtherFlat = $("").spectrum({ appendTo: container, flat: true }); equal ( appendToDefault.spectrum("container").parent()[0], document.body, "Appended to body by default"); equal ( appendToOther.spectrum("container").parent()[0], container[0], "Can be appended to another element"); equal ( appendToOtherFlat.spectrum("container").parent()[0], $(appendToOtherFlat).parent()[0], "Flat CANNOT be appended to another element, will be same as input"); equal ( appendToParent.spectrum("container").parent()[0], container[0], "Passing 'parent' to appendTo works as expected"); // Issue #70 - https://github.com/bgrins/spectrum/issues/70 equal (spec.spectrum("option", "showPalette"), true, "showPalette is true by default"); spec.spectrum("option", "showPalette", false); equal (spec.spectrum("option", "showPalette"), false, "showPalette is false after setting showPalette"); spec.spectrum("option", "showPaletteOnly", true); equal (spec.spectrum("option", "showPaletteOnly"), true, "showPaletteOnly is true after setting showPaletteOnly"); equal (spec.spectrum("option", "showPalette"), true, "showPalette is true after setting showPaletteOnly"); spec.spectrum("destroy"); appendToDefault.spectrum("destroy"); appendToOther.spectrum("destroy"); appendToOtherFlat.spectrum("destroy"); appendToParent.spectrum("destroy").remove(); delete window.localStorage["spectrum.example"]; container.remove(); }); test ("Show Input works as expected", function() { var el = $("").spectrum({ showInput: true, color: "red" }); var input = el.spectrum("container").find(".sp-input"); equal(input.val(), "red", "Input is set to color by default"); input.val("").trigger("change"); ok(input.hasClass("sp-validation-error"), "Input has validation error class after being emptied."); input.val("red").trigger("change"); ok(!input.hasClass("sp-validation-error"), "Input does not have validation error class after being reset to original color."); equal (el.spectrum("get").toHexString(), "#ff0000", "Color is still red"); el.spectrum("destroy"); }); test ("Toggle Picker Area button works as expected", function() { var div = $("