summaryrefslogtreecommitdiffstats
path: root/test/api
diff options
context:
space:
mode:
Diffstat (limited to 'test/api')
-rw-r--r--test/api/closeSpec.js30
-rw-r--r--test/api/evaluateSpec.js57
-rw-r--r--test/api/gotoSpec.js64
-rw-r--r--test/api/nextSpec.js57
-rw-r--r--test/api/openSpec.js50
-rw-r--r--test/api/openedSpec.js26
-rw-r--r--test/api/previousSpec.js57
-rw-r--r--test/api/selectSpec.js122
-rw-r--r--test/api/selectedSpec.js41
9 files changed, 504 insertions, 0 deletions
diff --git a/test/api/closeSpec.js b/test/api/closeSpec.js
new file mode 100644
index 0000000..b1de6bc
--- /dev/null
+++ b/test/api/closeSpec.js
@@ -0,0 +1,30 @@
+describe("awesomplete.close", function () {
+
+ $.fixture("plain");
+
+ subject(function () { return new Awesomplete("#plain") });
+
+ beforeEach(function () {
+ this.subject.open();
+ this.subject.next();
+ });
+
+ it("closes completer", function () {
+ this.subject.close();
+ expect(this.subject.ul.hasAttribute("hidden")).toBe(true);
+ });
+
+ it("makes no item selected", function () {
+ this.subject.close();
+
+ expect(this.subject.selected).toBe(false);
+ expect(this.subject.index).toBe(-1);
+ });
+
+ it("fires awesomplete-close event", function () {
+ var handler = $.spyOnEvent(this.subject.input, "awesomplete-close");
+ this.subject.close();
+
+ expect(handler).toHaveBeenCalled();
+ });
+});
diff --git a/test/api/evaluateSpec.js b/test/api/evaluateSpec.js
new file mode 100644
index 0000000..cc064b6
--- /dev/null
+++ b/test/api/evaluateSpec.js
@@ -0,0 +1,57 @@
+describe("awesomplete.evaluate", function () {
+
+ $.fixture("plain");
+
+ subject(function () {
+ return new Awesomplete("#plain", { list: ["item1", "item2", "item3"] });
+ });
+
+ describe("with too short input value", function () {
+ beforeEach(function () {
+ $.type(this.subject.input, "i");
+ });
+
+ it("closes completer", function () {
+ spyOn(this.subject, "close");
+ this.subject.evaluate();
+
+ expect(this.subject.close).toHaveBeenCalled();
+ });
+ });
+
+ describe("with no items found", function () {
+ beforeEach(function () {
+ $.type(this.subject.input, "nosuchitem");
+ });
+
+ it("closes completer", function () {
+ spyOn(this.subject, "close");
+ this.subject.evaluate();
+
+ expect(this.subject.close).toHaveBeenCalled();
+ });
+ });
+
+ describe("with some items found", function () {
+ beforeEach(function () {
+ $.type(this.subject.input, "ite");
+ });
+
+ it("opens completer", function () {
+ spyOn(this.subject, "open");
+ this.subject.evaluate();
+
+ expect(this.subject.open).toHaveBeenCalled();
+ });
+
+ it("fills completer with found items", function () {
+ this.subject.evaluate();
+ expect(this.subject.ul.children.length).toBe(3);
+ });
+
+ it("makes no item selected", function () {
+ this.subject.evaluate();
+ expect(this.subject.selected).toBe(false);
+ });
+ });
+});
diff --git a/test/api/gotoSpec.js b/test/api/gotoSpec.js
new file mode 100644
index 0000000..da96cf0
--- /dev/null
+++ b/test/api/gotoSpec.js
@@ -0,0 +1,64 @@
+describe("awesomplete.goto", function () {
+
+ $.fixture("plain");
+
+ subject(function () {
+ return new Awesomplete("#plain", { list: ["item1", "item2", "item3"] });
+ });
+
+ def("firstIndex", function () { return 0 });
+ def("lastIndex", function () { return this.subject.ul.children.length - 1 });
+
+ beforeEach(function () {
+ $.type(this.subject.input, "ite");
+ });
+
+ it("clears previous aria-selected", function () {
+ this.subject.goto(this.firstIndex);
+ this.subject.goto(this.lastIndex);
+
+ expect(this.subject.ul.children[this.firstIndex].getAttribute("aria-selected")).toBe("false");
+ });
+
+ it("goes to first item", function () {
+ this.subject.goto(this.firstIndex);
+ expect(this.subject.index).toBe(this.firstIndex);
+ });
+
+ it("goes to last item", function () {
+ this.subject.goto(this.lastIndex);
+ expect(this.subject.index).toBe(this.lastIndex);
+ });
+
+ it("fires awesomplete-highlight event", function () {
+ var handler = $.spyOnEvent(this.subject.input, "awesomplete-highlight");
+ this.subject.goto(1);
+
+ expect(handler).toHaveBeenCalled();
+ });
+
+ describe("with item index > -1", function () {
+ beforeEach(function () {
+ this.subject.goto(this.firstIndex);
+ });
+
+ it("sets aria-selected", function () {
+ expect(this.subject.ul.children[this.firstIndex].getAttribute("aria-selected")).toBe("true");
+ });
+
+ it("updates status", function () {
+ expect(this.subject.status.textContent).toBe("item1");
+ });
+ });
+
+ describe("with item index = -1", function () {
+ beforeEach(function () {
+ this.subject.goto(this.firstIndex);
+ this.subject.goto(-1);
+ });
+
+ it("does not update status", function () {
+ expect(this.subject.status.textContent).toBe("item1");
+ });
+ });
+});
diff --git a/test/api/nextSpec.js b/test/api/nextSpec.js
new file mode 100644
index 0000000..bef3134
--- /dev/null
+++ b/test/api/nextSpec.js
@@ -0,0 +1,57 @@
+describe("awesomplete.next", function () {
+
+ $.fixture("plain");
+
+ subject(function () {
+ return new Awesomplete("#plain", { list: ["item1", "item2", "item3"] });
+ });
+
+ def("firstIndex", function () { return 0 });
+ def("lastIndex", function () { return this.subject.ul.children.length - 1 });
+
+ describe("without any items found", function () {
+ beforeEach(function () {
+ $.type(this.subject.input, "nosuchitem");
+ this.subject.open();
+ });
+
+ it("does not select any item", function () {
+ this.subject.next();
+ expect(this.subject.selected).toBe(false);
+ });
+ });
+
+ describe("with some items found", function () {
+ beforeEach(function () {
+ $.type(this.subject.input, "ite");
+ this.subject.open();
+ });
+
+ describe("and no item was already selected", function () {
+ it("selects the first item ", function () {
+ this.subject.next();
+ expect(this.subject.index).toBe(this.firstIndex);
+ });
+ });
+
+ describe("and some item was already selected", function () {
+ it("selects the second item", function () {
+ this.subject.goto(this.firstIndex);
+ this.subject.next();
+ expect(this.subject.index).toBe(this.firstIndex + 1);
+ });
+
+ it("selects the last item", function () {
+ this.subject.goto(this.lastIndex - 1);
+ this.subject.next();
+ expect(this.subject.index).toBe(this.lastIndex);
+ });
+
+ it("selects no item after reaching the end", function () {
+ this.subject.goto(this.lastIndex);
+ this.subject.next();
+ expect(this.subject.selected).toBe(false);
+ });
+ });
+ });
+});
diff --git a/test/api/openSpec.js b/test/api/openSpec.js
new file mode 100644
index 0000000..848b960
--- /dev/null
+++ b/test/api/openSpec.js
@@ -0,0 +1,50 @@
+describe("awesomplete.open", function () {
+
+ $.fixture("plain");
+
+ subject(function () { return new Awesomplete("#plain", this.options) });
+
+ it("opens completer", function () {
+ this.subject.open();
+ expect(this.subject.ul.hasAttribute("hidden")).toBe(false);
+ });
+
+ // Exposes this bug https://github.com/LeaVerou/awesomplete/pull/16740
+ // FIXME better fix is probably required as discussed in PR above
+ xit("fills in the list on creation", function () {
+ $("#plain").value = "ite";
+ this.options = { list: "item1, item2" };
+ this.subject.open();
+
+ expect(this.subject.ul.children.length).toBe(2);
+ });
+
+ it("fires awesomplete-open event", function () {
+ var handler = $.spyOnEvent(this.subject.input, "awesomplete-open");
+ this.subject.open();
+
+ expect(handler).toHaveBeenCalled();
+ });
+
+ describe("with autoFirst: true", function () {
+ def("options", function () { return { autoFirst: true } });
+
+ it("automatically selects first item", function () {
+ spyOn(this.subject, "goto");
+ this.subject.open();
+
+ expect(this.subject.goto).toHaveBeenCalledWith(0);
+ });
+ });
+
+ describe("with autoFirst: false", function () {
+ def("options", function () { return { autoFirst: false } });
+
+ it("does not select any item", function () {
+ this.subject.open();
+
+ expect(this.subject.selected).toBe(false);
+ expect(this.subject.index).toBe(-1);
+ });
+ });
+});
diff --git a/test/api/openedSpec.js b/test/api/openedSpec.js
new file mode 100644
index 0000000..370ffdc
--- /dev/null
+++ b/test/api/openedSpec.js
@@ -0,0 +1,26 @@
+describe("awesomplete.opened", function () {
+
+ $.fixture("plain");
+
+ subject(function () { return new Awesomplete("#plain") });
+
+ describe("with newly created completer", function () {
+ it("is false", function () {
+ expect(this.subject.opened).toBe(false);
+ });
+ });
+
+ describe("with opened completer", function () {
+ it("is true", function () {
+ this.subject.open();
+ expect(this.subject.opened).toBe(true);
+ });
+ });
+
+ describe("with closed completer", function () {
+ it("is false", function () {
+ this.subject.close();
+ expect(this.subject.opened).toBe(false);
+ });
+ });
+});
diff --git a/test/api/previousSpec.js b/test/api/previousSpec.js
new file mode 100644
index 0000000..c1d7daf
--- /dev/null
+++ b/test/api/previousSpec.js
@@ -0,0 +1,57 @@
+describe("awesomplete.previous", function () {
+
+ $.fixture("plain");
+
+ subject(function () {
+ return new Awesomplete("#plain", { list: ["item1", "item2", "item3"] });
+ });
+
+ def("firstIndex", function () { return 0 });
+ def("lastIndex", function () { return this.subject.ul.children.length - 1 });
+
+ describe("without any items found", function () {
+ beforeEach(function () {
+ $.type(this.subject.input, "nosuchitem");
+ this.subject.open();
+ });
+
+ it("does not select any item", function () {
+ this.subject.previous();
+ expect(this.subject.selected).toBe(false);
+ });
+ });
+
+ describe("with some items found", function () {
+ beforeEach(function () {
+ $.type(this.subject.input, "ite");
+ this.subject.open();
+ });
+
+ describe("and no item was already selected", function () {
+ it("selects the last item ", function () {
+ this.subject.previous();
+ expect(this.subject.index).toBe(this.lastIndex);
+ });
+ });
+
+ describe("and some item was already selected", function () {
+ it("selects the second item from the end", function () {
+ this.subject.goto(this.lastIndex);
+ this.subject.previous();
+ expect(this.subject.index).toBe(this.lastIndex - 1);
+ });
+
+ it("selects the first item", function () {
+ this.subject.goto(this.firstIndex + 1);
+ this.subject.previous();
+ expect(this.subject.index).toBe(this.firstIndex);
+ });
+
+ it("selects no item after reaching the start", function () {
+ this.subject.goto(this.firstIndex);
+ this.subject.previous();
+ expect(this.subject.selected).toBe(false);
+ });
+ });
+ });
+});
diff --git a/test/api/selectSpec.js b/test/api/selectSpec.js
new file mode 100644
index 0000000..82b4069
--- /dev/null
+++ b/test/api/selectSpec.js
@@ -0,0 +1,122 @@
+describe("awesomplete.select", function () {
+
+ $.fixture("plain");
+
+ subject(function () {
+ return new Awesomplete("#plain", { list: ["item1", "item2", "item3"] });
+ });
+
+ def("firstIndex", function () { return 0 });
+ def("lastIndex", function () { return this.subject.ul.children.length - 1 });
+ def("lastLi", function () { return this.subject.ul.children[this.lastIndex] });
+
+ beforeEach(function () {
+ $.type(this.subject.input, "ite");
+ });
+
+ describe("with closed completer", itDoesNotSelectAnyItem);
+
+ describe("with opened completer", function () {
+ beforeEach(function () {
+ this.subject.open();
+ });
+
+ describe("and no current item", itDoesNotSelectAnyItem);
+
+ describe("and current item", function () {
+ beforeEach(function () {
+ this.subject.goto(this.firstIndex);
+ });
+
+ itSelects("item1");
+ });
+
+ describe("and item specified as argument", function () {
+ def("selectArgument", function () { return this.lastLi });
+
+ itSelects("item3");
+ });
+ });
+
+ // Shared behaviors
+
+ function itSelects(expectedTxt) {
+ it("fires awesomplete-select event", function () {
+ var handler = $.spyOnEvent(this.subject.input, "awesomplete-select");
+ this.subject.select(this.selectArgument);
+
+ expect(handler).toHaveBeenCalledWith(jasmine.objectContaining({ text: expectedTxt }));
+ });
+
+ describe("and awesomplete-select event was not prevented", function () {
+ beforeEach(function () {
+ $.on(this.subject.input, "awesomplete-select", $.noop);
+ });
+
+ it("changes the input value", function () {
+ this.subject.select(this.selectArgument);
+ expect(this.subject.input.value).toBe(expectedTxt);
+ });
+
+ it("closes completer", function () {
+ spyOn(this.subject, "close");
+ this.subject.select(this.selectArgument);
+
+ expect(this.subject.close).toHaveBeenCalled();
+ });
+
+ it("fires awesomplete-selectcomplete event", function () {
+ var handler = $.spyOnEvent(this.subject.input, "awesomplete-selectcomplete");
+ this.subject.select(this.selectArgument);
+
+ expect(handler).toHaveBeenCalled();
+ });
+ });
+
+ describe("and awesomplete-select event was prevented", function () {
+ beforeEach(function () {
+ $.on(this.subject.input, "awesomplete-select", function (evt) { evt.preventDefault() });
+ });
+
+ it("does not change the input value", function () {
+ this.subject.select(this.selectArgument);
+ expect(this.subject.input.value).toBe("ite");
+ });
+
+ it("does not close completer", function () {
+ spyOn(this.subject, "close");
+ this.subject.select(this.selectArgument);
+
+ expect(this.subject.close).not.toHaveBeenCalled();
+ });
+
+ it("does not fire awesomplete-selectcomplete event", function () {
+ var handler = $.spyOnEvent(this.subject.input, "awesomplete-selectcomplete");
+ this.subject.select(this.selectArgument);
+
+ expect(handler).not.toHaveBeenCalled();
+ });
+ });
+ }
+
+ function itDoesNotSelectAnyItem() {
+ it("does not change the input value", function () {
+ this.subject.select();
+ expect(this.subject.input.value).toBe("ite");
+ });
+
+ it("does not fire awesomplete-select event", function () {
+ var handler = $.spyOnEvent(this.subject.input, "awesomplete-select");
+ this.subject.select();
+
+ expect(handler).not.toHaveBeenCalled();
+ });
+
+ it("does not fire awesomplete-selectcomplete event", function () {
+ var handler = $.spyOnEvent(this.subject.input, "awesomplete-selectcomplete");
+ this.subject.select();
+
+ expect(handler).not.toHaveBeenCalled();
+ });
+ }
+});
diff --git a/test/api/selectedSpec.js b/test/api/selectedSpec.js
new file mode 100644
index 0000000..6e645be
--- /dev/null
+++ b/test/api/selectedSpec.js
@@ -0,0 +1,41 @@
+describe("awesomplete.selected", function () {
+
+ $.fixture("plain");
+
+ subject(function () {
+ return new Awesomplete("#plain", { list: ["item1", "item2", "item3"] });
+ });
+
+ describe("with newly created completer", function () {
+ it("is false", function () {
+ expect(this.subject.selected).toBe(false);
+ });
+ });
+
+ describe("with opened completer", function () {
+ beforeEach(function () {
+ this.subject.open();
+ $.type(this.subject.input, "ite");
+ });
+
+ describe("and no item selected", function () {
+ it("is false", function () {
+ expect(this.subject.selected).toBe(false);
+ });
+ });
+
+ describe("and some item selected", function () {
+ it("is true", function () {
+ this.subject.next();
+ expect(this.subject.selected).toBe(true);
+ });
+ });
+ });
+
+ describe("with closed completer", function () {
+ it("is false", function () {
+ this.subject.close();
+ expect(this.subject.selected).toBe(false);
+ });
+ });
+});