summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--test/api/closeSpec.js9
-rw-r--r--test/api/gotoSpec.js6
-rw-r--r--test/api/openSpec.js7
-rw-r--r--test/api/selectSpec.js32
-rw-r--r--test/events/submitSpec.js4
-rw-r--r--test/specHelper.js15
6 files changed, 31 insertions, 42 deletions
diff --git a/test/api/closeSpec.js b/test/api/closeSpec.js
index 6661af6..b1de6bc 100644
--- a/test/api/closeSpec.js
+++ b/test/api/closeSpec.js
@@ -4,8 +4,6 @@ describe("awesomplete.close", function () {
subject(function () { return new Awesomplete("#plain") });
- def("events", function () { return { closed: $.noop } });
-
beforeEach(function () {
this.subject.open();
this.subject.next();
@@ -24,10 +22,9 @@ describe("awesomplete.close", function () {
});
it("fires awesomplete-close event", function () {
- spyOn(this.events, "closed")
- $.on(this.subject.input, "awesomplete-close", this.events.closed);
-
+ var handler = $.spyOnEvent(this.subject.input, "awesomplete-close");
this.subject.close();
- expect(this.events.closed).toHaveBeenCalled();
+
+ expect(handler).toHaveBeenCalled();
});
});
diff --git a/test/api/gotoSpec.js b/test/api/gotoSpec.js
index 9e382c0..da96cf0 100644
--- a/test/api/gotoSpec.js
+++ b/test/api/gotoSpec.js
@@ -31,12 +31,10 @@ describe("awesomplete.goto", function () {
});
it("fires awesomplete-highlight event", function () {
- var events = { highlight: $.noop};
- spyOn(events, "highlight");
- $.on(this.subject.input, "awesomplete-highlight", events.highlight);
+ var handler = $.spyOnEvent(this.subject.input, "awesomplete-highlight");
this.subject.goto(1);
- expect(events.highlight).toHaveBeenCalled();
+ expect(handler).toHaveBeenCalled();
});
describe("with item index > -1", function () {
diff --git a/test/api/openSpec.js b/test/api/openSpec.js
index e79c721..848b960 100644
--- a/test/api/openSpec.js
+++ b/test/api/openSpec.js
@@ -4,8 +4,6 @@ describe("awesomplete.open", function () {
subject(function () { return new Awesomplete("#plain", this.options) });
- def("events", function () { return { opened: $.noop } });
-
it("opens completer", function () {
this.subject.open();
expect(this.subject.ul.hasAttribute("hidden")).toBe(false);
@@ -22,11 +20,10 @@ describe("awesomplete.open", function () {
});
it("fires awesomplete-open event", function () {
- spyOn(this.events, "opened")
- $.on(this.subject.input, "awesomplete-open", this.events.opened);
+ var handler = $.spyOnEvent(this.subject.input, "awesomplete-open");
this.subject.open();
- expect(this.events.opened).toHaveBeenCalled();
+ expect(handler).toHaveBeenCalled();
});
describe("with autoFirst: true", function () {
diff --git a/test/api/selectSpec.js b/test/api/selectSpec.js
index 617c4f6..82b4069 100644
--- a/test/api/selectSpec.js
+++ b/test/api/selectSpec.js
@@ -6,7 +6,6 @@ describe("awesomplete.select", function () {
return new Awesomplete("#plain", { list: ["item1", "item2", "item3"] });
});
- def("events", function () { return { select: $.noop, complete: $.noop} });
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] });
@@ -43,17 +42,15 @@ describe("awesomplete.select", function () {
function itSelects(expectedTxt) {
it("fires awesomplete-select event", function () {
- spyOn(this.events, "select");
- $.on(this.subject.input, "awesomplete-select", this.events.select);
+ var handler = $.spyOnEvent(this.subject.input, "awesomplete-select");
this.subject.select(this.selectArgument);
- expect(this.events.select).toHaveBeenCalledWith(jasmine.objectContaining({ text: expectedTxt }));
+ expect(handler).toHaveBeenCalledWith(jasmine.objectContaining({ text: expectedTxt }));
});
describe("and awesomplete-select event was not prevented", function () {
beforeEach(function () {
- this.events.select = $.noop;
- $.on(this.subject.input, "awesomplete-select", this.events.select);
+ $.on(this.subject.input, "awesomplete-select", $.noop);
});
it("changes the input value", function () {
@@ -69,18 +66,16 @@ describe("awesomplete.select", function () {
});
it("fires awesomplete-selectcomplete event", function () {
- spyOn(this.events, "complete");
- this.subject.input.addEventListener("awesomplete-selectcomplete", this.events.complete);
+ var handler = $.spyOnEvent(this.subject.input, "awesomplete-selectcomplete");
this.subject.select(this.selectArgument);
- expect(this.events.complete).toHaveBeenCalled();
+ expect(handler).toHaveBeenCalled();
});
});
describe("and awesomplete-select event was prevented", function () {
beforeEach(function () {
- this.events.select = function(evt) { evt.preventDefault() };
- $.on(this.subject.input, "awesomplete-select", this.events.select);
+ $.on(this.subject.input, "awesomplete-select", function (evt) { evt.preventDefault() });
});
it("does not change the input value", function () {
@@ -96,11 +91,10 @@ describe("awesomplete.select", function () {
});
it("does not fire awesomplete-selectcomplete event", function () {
- spyOn(this.events, "complete");
- this.subject.input.addEventListener("awesomplete-selectcomplete", this.events.complete);
+ var handler = $.spyOnEvent(this.subject.input, "awesomplete-selectcomplete");
this.subject.select(this.selectArgument);
- expect(this.events.complete).not.toHaveBeenCalled();
+ expect(handler).not.toHaveBeenCalled();
});
});
}
@@ -112,19 +106,17 @@ describe("awesomplete.select", function () {
});
it("does not fire awesomplete-select event", function () {
- spyOn(this.events, "select");
- $.on(this.subject.input, "awesomplete-select", this.events.select);
+ var handler = $.spyOnEvent(this.subject.input, "awesomplete-select");
this.subject.select();
- expect(this.events.select).not.toHaveBeenCalled();
+ expect(handler).not.toHaveBeenCalled();
});
it("does not fire awesomplete-selectcomplete event", function () {
- spyOn(this.events, "complete");
- this.subject.input.addEventListener("awesomplete-selectcomplete", this.events.complete);
+ var handler = $.spyOnEvent(this.subject.input, "awesomplete-selectcomplete");
this.subject.select();
- expect(this.events.complete).not.toHaveBeenCalled();
+ expect(handler).not.toHaveBeenCalled();
});
}
});
diff --git a/test/events/submitSpec.js b/test/events/submitSpec.js
index ea0fa22..bacd726 100644
--- a/test/events/submitSpec.js
+++ b/test/events/submitSpec.js
@@ -11,9 +11,7 @@ describe("form submit event", function () {
this.subject.input.focus();
this.subject.open();
// prevent full page reload in Firefox, which causes tests to stop running
- $.on(this.subject.input.form, "submit", function(evt) {
- evt.preventDefault();
- });
+ $.on(this.subject.input.form, "submit", function (evt) { evt.preventDefault() });
});
it("closes completer", function () {
diff --git a/test/specHelper.js b/test/specHelper.js
index 6157253..58282e5 100644
--- a/test/specHelper.js
+++ b/test/specHelper.js
@@ -22,8 +22,15 @@ $.fixture = function (fixtureName) {
});
};
-$.on = function (element, event, callback) {
- element.addEventListener(event, callback);
+// spy to check if event was fired or not
+$.spyOnEvent = function (target, type) {
+ var handler = jasmine.createSpy(type);
+ $.on(target, type, handler);
+ return handler;
+};
+
+$.on = function (target, type, callback) {
+ target.addEventListener(type, callback);
};
$.fire = function (target, type, properties) {
@@ -40,10 +47,10 @@ $.type = function (input, text) {
input.focus();
input.value = text;
$.fire(input, "input");
-}
+};
// simulates keydown events
-$.keydown = function(target, keyCode) {
+$.keydown = function (target, keyCode) {
$.fire(target, "keydown", { keyCode: keyCode });
};
$.k = {