diff options
-rw-r--r-- | test/api/nextSpec.js | 57 | ||||
-rw-r--r-- | test/specHelper.js | 16 |
2 files changed, 73 insertions, 0 deletions
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/specHelper.js b/test/specHelper.js index 2bd6fa2..4cddcb0 100644 --- a/test/specHelper.js +++ b/test/specHelper.js @@ -26,6 +26,22 @@ $.on = function (element, event, callback) { element.addEventListener(event, callback); }; +$.fire = function (target, type, properties) { + var evt = document.createEvent("HTMLEvents"); + evt.initEvent(type, true, true ); + for (var j in properties) { + evt[j] = properties[j]; + } + target.dispatchEvent(evt); +}; + +// simulates text input (very simple, only "input" event is fired) +$.type = function (input, text) { + input.focus(); + input.value = text; + $.fire(input, "input"); +} + // $.noop returns a new empty function each time it's being called Object.defineProperty($, "noop", { get: function () { |