summaryrefslogtreecommitdiffstats
path: root/test/api/previousSpec.js
blob: c1d7daf20776f5f7d9b050a1f3229008f0eea036 (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
describe("awesomplete.previous", 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.previous();
			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 last item ", function () {
				this.subject.previous();
				expect(this.subject.index).toBe(this.lastIndex);
			});
		});

		describe("and some item was already selected", function () {
			it("selects the second item from the end", function () {
				this.subject.goto(this.lastIndex);
				this.subject.previous();
				expect(this.subject.index).toBe(this.lastIndex - 1);
			});

			it("selects the first item", function () {
				this.subject.goto(this.firstIndex + 1);
				this.subject.previous();
				expect(this.subject.index).toBe(this.firstIndex);
			});

			it("selects no item after reaching the start", function () {
				this.subject.goto(this.firstIndex);
				this.subject.previous();
				expect(this.subject.selected).toBe(false);
			});
		});
	});
});