summaryrefslogtreecommitdiffstats
path: root/test/api/selectSpec.js
diff options
context:
space:
mode:
authorVladislav Zarakovsky <vlad.zar@gmail.com>2015-12-28 09:31:07 +0300
committerVladislav Zarakovsky <vlad.zar@gmail.com>2015-12-28 09:31:07 +0300
commit0b38bb595b9a4b8a6353fd4748dfb3c6530b57ac (patch)
treeeaea8096b6ffa7f04fb218f5e9fea7942edfb668 /test/api/selectSpec.js
parent730f82a2bbe7aa3ac65f574e3ce1c481fd953f66 (diff)
parent5dbfd450fb5d5ce0c1b8886baed047f91d926691 (diff)
downloadawesomplete-0b38bb595b9a4b8a6353fd4748dfb3c6530b57ac.zip
awesomplete-0b38bb595b9a4b8a6353fd4748dfb3c6530b57ac.tar.gz
awesomplete-0b38bb595b9a4b8a6353fd4748dfb3c6530b57ac.tar.bz2
Merge remote-tracking branch 'upstream/gh-pages' into features/code-climate
Diffstat (limited to 'test/api/selectSpec.js')
-rw-r--r--test/api/selectSpec.js122
1 files changed, 122 insertions, 0 deletions
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();
+ });
+ }
+});