summaryrefslogtreecommitdiffstats
path: root/test/api/selectSpec.js
blob: f972142237fe50bf6f2f6a4ffaff3cb967cce5f4 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
describe("awesomplete.select", function () {

	$.fixture("plain");

	subject(function () {
		return new Awesomplete("#plain", { list: ["item1", "item2", "item3"] });
	});

	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(0);
			});

			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: jasmine.objectContaining({ label: expectedTxt, value: expectedTxt }),
					origin: this.selectArgument || this.subject.ul.children[0]
				})
			);
		});

		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).toHaveBeenCalledWith(
					jasmine.objectContaining({
						text: jasmine.objectContaining({ label: expectedTxt, value: expectedTxt })
					})
				);
			});
		});

		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();
		});
	}
});