summaryrefslogtreecommitdiffstats
path: root/test/helpers/bindSpec.js
blob: 8eea7ff2ce8c28c7aba93fa75fde09bce4d3a6df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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"]);
		});
	});
});