summaryrefslogtreecommitdiffstats
path: root/test/helpers
diff options
context:
space:
mode:
Diffstat (limited to 'test/helpers')
-rw-r--r--test/helpers/bindSpec.js59
-rw-r--r--test/helpers/createSpec.js102
-rw-r--r--test/helpers/dollarSpec.js62
-rw-r--r--test/helpers/doubleDollarSpec.js49
-rw-r--r--test/helpers/fireSpec.js60
-rw-r--r--test/helpers/regExpEscapeSpec.js38
6 files changed, 370 insertions, 0 deletions
diff --git a/test/helpers/bindSpec.js b/test/helpers/bindSpec.js
new file mode 100644
index 0000000..8eea7ff
--- /dev/null
+++ b/test/helpers/bindSpec.js
@@ -0,0 +1,59 @@
+describe("Awesomplete.$.bind", function () {
+
+ $.fixture("plain");
+
+ subject(function () {
+ return function () { Awesomplete.$.bind(this.element, this.events) };
+ });
+
+ describe("whith invalid element", function () {
+ it("does nothing if element is undefined", function () {
+ this.element = undefined;
+ expect(this.subject).not.toThrow();
+ });
+
+ it("does nothing if element is null", function () {
+ this.element = null;
+ expect(this.subject).not.toThrow();
+ });
+
+ it("does nothing if element is false", function () {
+ this.element = false;
+ expect(this.subject).not.toThrow();
+ });
+
+ it("does nothing if element is 0", function () {
+ this.element = 0;
+ expect(this.subject).not.toThrow();
+ });
+
+ it("does nothing if element is empty string", function () {
+ this.element = "";
+ expect(this.subject).not.toThrow();
+ });
+ });
+
+ describe("with valid element", function () {
+ def("element", function () { return $("#plain") });
+
+ beforeEach(function () {
+ spyOn(this.element, "addEventListener");
+ });
+
+ it("adds event listeners for all events", function () {
+ this.events = { click: $.noop, input: $.noop };
+ this.subject();
+
+ expect(this.element.addEventListener).toHaveBeenCalledWith("click", this.events.click);
+ expect(this.element.addEventListener).toHaveBeenCalledWith("input", this.events.input);
+ });
+
+ it("adds single event listener for multiple events", function () {
+ this.events = { "click input": $.noop };
+ this.subject();
+
+ expect(this.element.addEventListener).toHaveBeenCalledWith("click", this.events["click input"]);
+ expect(this.element.addEventListener).toHaveBeenCalledWith("input", this.events["click input"]);
+ });
+ });
+});
diff --git a/test/helpers/createSpec.js b/test/helpers/createSpec.js
new file mode 100644
index 0000000..a9ef31e
--- /dev/null
+++ b/test/helpers/createSpec.js
@@ -0,0 +1,102 @@
+describe("Awesomplete.$.create", function () {
+
+ $.fixture("options");
+
+ subject(function () { return Awesomplete.$.create(this.tag, this.options || {}) });
+
+ def("tag", "div");
+
+ it("creates DOM element", function () {
+ expect(this.subject instanceof HTMLElement).toBe(true);
+ });
+
+ describe("with various tag names", function () {
+ it("creates <ul> element", function () {
+ this.tag = "ul";
+ expect(this.subject.tagName).toEqual("UL");
+ });
+
+ it("creates <li> element", function () {
+ this.tag = "li";
+ expect(this.subject.tagName).toEqual("LI");
+ });
+ });
+
+ describe("without options", function () {
+ it("creates element without any attributes", function () {
+ expect(this.subject.attributes.length).toEqual(0);
+ });
+ });
+
+ describe("with simple options", function () {
+ it("assigns properties", function () {
+ this.options = { id: "id1", className: "class-name" };
+
+ expect(this.subject.id).toEqual("id1");
+ expect(this.subject.className).toEqual("class-name");
+ });
+
+ it("assigns attributes", function () {
+ this.options = { attr1: "val1", attr2: "val2" };
+
+ expect(this.subject.getAttribute("attr1")).toEqual("val1");
+ expect(this.subject.getAttribute("attr2")).toEqual("val2");
+ });
+ });
+
+ describe("with option for boolean attribute/property", function () {
+ it("assigns from true value", function () {
+ this.options = { hidden: true };
+ expect(this.subject.hasAttribute("hidden")).toBe(true);
+ });
+
+ it("assigns from truthy value", function () {
+ this.options = { hidden: "hidden" };
+ expect(this.subject.hasAttribute("hidden")).toBe(true);
+ });
+
+ it("assigns from false value", function () {
+ this.options = { hidden: false };
+ expect(this.subject.hasAttribute("hidden")).toBe(false);
+ });
+
+ it("assigns from falsy value", function () {
+ this.options = { hidden: "" };
+ expect(this.subject.hasAttribute("hidden")).toBe(false);
+ });
+ });
+
+ describe("with inside: option", function () {
+ it("appends to container by element", function () {
+ this.options = { inside: $("#data-list") };
+
+ expect(this.subject).toEqual(this.options.inside.lastChild);
+ });
+
+ it("appends to container by selector", function () {
+ this.options = { inside: "#data-list" };
+
+ expect(this.subject).toEqual($(this.options.inside).lastChild);
+ });
+ });
+
+ describe("with around: option", function () {
+ it("wraps specified element", function () {
+ this.options = { around: $("#no-options") };
+
+ var originalParent = this.options.around.parentNode;
+ expect(this.subject.parentNode).toEqual(originalParent);
+
+ expect(this.subject.firstChild).toEqual(this.options.around);
+ });
+
+ it("wraps element specified by selector", function () {
+ this.options = { around: "#no-options" };
+
+ var originalParent = $(this.options.around).parentNode;
+ expect(this.subject.parentNode).toEqual(originalParent);
+
+ expect(this.subject.firstChild).toEqual($(this.options.around));
+ });
+ });
+});
diff --git a/test/helpers/dollarSpec.js b/test/helpers/dollarSpec.js
new file mode 100644
index 0000000..3ba864e
--- /dev/null
+++ b/test/helpers/dollarSpec.js
@@ -0,0 +1,62 @@
+describe("Awesomplete.$", function () {
+
+ $.fixture("options");
+
+ subject(function () { return Awesomplete.$(this.expression, this.context) });
+
+ describe("with default context", itFindsElement);
+
+ describe("with custom context", function () {
+ def("context", function () { return fixture.el });
+
+ itFindsElement();
+ });
+
+ describe("with truthy non string expression", function () {
+ it("returns the expression back", function () {
+ this.expression = $("#no-options");
+ expect(this.subject).toBe(this.expression);
+ });
+ });
+
+ describe("with falsy non string expression", function () {
+ it("returns null if expression is undefined", function () {
+ this.expression = undefined;
+ expect(this.subject).toBeNull();
+ });
+
+ it("returns null if expression is null", function () {
+ this.expression = null;
+ expect(this.subject).toBeNull();
+ });
+
+ it("returns null if expression is false", function () {
+ this.expression = false;
+ expect(this.subject).toBeNull();
+ });
+ });
+
+ // Shared behaviors
+
+ function itFindsElement() {
+ it("returns DOM element", function () {
+ this.expression = "#no-options";
+ expect(this.subject instanceof HTMLElement).toBe(true);
+ });
+
+ it("finds by id", function () {
+ this.expression = "#no-options";
+ expect(this.subject.id).toEqual("no-options");
+ });
+
+ it("finds by class name", function () {
+ this.expression = ".simple-input";
+ expect(this.subject.id).toEqual("no-options");
+ });
+
+ it("finds by tag name", function () {
+ this.expression = "datalist";
+ expect(this.subject.id).toEqual("list");
+ });
+ }
+});
diff --git a/test/helpers/doubleDollarSpec.js b/test/helpers/doubleDollarSpec.js
new file mode 100644
index 0000000..c76a10d
--- /dev/null
+++ b/test/helpers/doubleDollarSpec.js
@@ -0,0 +1,49 @@
+describe("Awesomplete.$$", function () {
+
+ $.fixture("options");
+
+ subject(function () { return Awesomplete.$$(this.expression, this.context) });
+
+ describe("with default context", itFindsAllElements);
+
+ describe("with custom context", function () {
+ def("context", function () { return fixture.el });
+
+ itFindsAllElements();
+ });
+
+ // Shared behaviors
+
+ function itFindsAllElements() {
+ it("returns an array of DOM elements", function () {
+ this.expression = "#no-options";
+ expect(this.subject).toEqual(jasmine.any(Array));
+ expect(this.subject[0] instanceof HTMLElement).toBe(true);
+ });
+
+ it("finds all elements", function () {
+ this.expression = "input";
+ expect(this.subject.length).toEqual($$("input").length);
+ });
+
+ it("finds DOM element", function () {
+ this.expression = "#no-options";
+ expect(this.subject[0] instanceof HTMLElement).toBe(true);
+ });
+
+ it("finds by id", function () {
+ this.expression = "#no-options";
+ expect(this.subject[0].id).toEqual("no-options");
+ });
+
+ it("finds by class name", function () {
+ this.expression = ".simple-input";
+ expect(this.subject[0].id).toEqual("no-options");
+ });
+
+ it("finds by tag name", function () {
+ this.expression = "datalist";
+ expect(this.subject[0].id).toEqual("list");
+ });
+ }
+});
diff --git a/test/helpers/fireSpec.js b/test/helpers/fireSpec.js
new file mode 100644
index 0000000..f876cce
--- /dev/null
+++ b/test/helpers/fireSpec.js
@@ -0,0 +1,60 @@
+describe("Awesomplete.$.fire", function () {
+
+ $.fixture("plain");
+
+ subject(function () {
+ return Awesomplete.$.fire.bind(Awesomplete.$, this.element);
+ });
+
+ def("element", function () { return $("#plain") });
+
+ beforeEach(function () {
+ spyOn(this.element, "dispatchEvent");
+ });
+
+ it("fires event once", function () {
+ this.subject("click");
+ expect(this.element.dispatchEvent.calls.count()).toEqual(1);
+ });
+
+ describe("fires different event types", function () {
+ it("fires click event", function () {
+ this.subject("click");
+ expect(this.element.dispatchEvent).toHaveBeenCalledWith(jasmine.objectContaining({ type: "click" }));
+ });
+
+ it("fires input event", function () {
+ this.subject("input");
+ expect(this.element.dispatchEvent).toHaveBeenCalledWith(jasmine.objectContaining({ type: "input" }));
+ });
+ });
+
+ describe("sets event properties", function () {
+ it("makes cancelable event", function () {
+ this.subject("click");
+ expect(this.element.dispatchEvent).toHaveBeenCalledWith(jasmine.objectContaining({ cancelable: true }));
+ });
+
+ it("can't make non cancelable event", function () {
+ this.subject("click", { cancelable: false });
+ expect(this.element.dispatchEvent).toHaveBeenCalledWith(jasmine.objectContaining({ cancelable: true }));
+ });
+
+ it("makes event that bubbles", function () {
+ this.subject("click");
+ expect(this.element.dispatchEvent).toHaveBeenCalledWith(jasmine.objectContaining({ bubbles: true }));
+ });
+
+ it("can't make event that does not bubble", function () {
+ this.subject("click", { bubles: false });
+ expect(this.element.dispatchEvent).toHaveBeenCalledWith(jasmine.objectContaining({ bubbles: true }));
+ });
+
+ it("sets properties on the event", function () {
+ var properties = { text: "hello", preventDefault: $.noop };
+
+ this.subject("click", properties);
+ expect(this.element.dispatchEvent).toHaveBeenCalledWith(jasmine.objectContaining(properties));
+ });
+ });
+});
diff --git a/test/helpers/regExpEscapeSpec.js b/test/helpers/regExpEscapeSpec.js
new file mode 100644
index 0000000..c50167b
--- /dev/null
+++ b/test/helpers/regExpEscapeSpec.js
@@ -0,0 +1,38 @@
+describe("Awesomplete.$.regExpEscape", function () {
+
+ subject(function () { return Awesomplete.$.regExpEscape(this.str) });
+
+ describe("with regular expression special characters", function () {
+ it("escapes backslashes", function () {
+ this.str = "\\";
+ expect(this.subject).toBe("\\\\");
+ });
+
+ it("escapes brackets, braces and parentheses", function () {
+ this.str = "[]{}()";
+ expect(this.subject).toBe("\\[\\]\\{\\}\\(\\)");
+ });
+
+ it("escapes other special characters", function () {
+ this.str = "-^$*+?.|";
+ expect(this.subject).toBe("\\-\\^\\$\\*\\+\\?\\.\\|");
+ });
+
+ it("escapes the whole string", function () {
+ this.str = "**";
+ expect(this.subject).toBe("\\*\\*");
+ });
+ });
+
+ describe("with plain characters", function () {
+ it("does not escape letters", function () {
+ this.str = "abcdefjhijklmnopqrstuvwxyzABCDEFJHIJKLMNOPQRSTUVWXYZ";
+ expect(this.subject).toBe(this.str);
+ });
+
+ it("does not escape numbers", function () {
+ this.str = "0123456789";
+ expect(this.subject).toBe(this.str);
+ });
+ });
+});