Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve test coverage #16785

Merged
merged 40 commits into from
Dec 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
317efcb
Update devDependencies
vlazar Nov 23, 2015
bb8f148
Add karma-jasmine-def for def() and subject() test helpers
vlazar Nov 23, 2015
1aa72f7
Add karma-fixture for loading fixtures from files
vlazar Nov 23, 2015
b6e5716
Set correct fixtures path
vlazar Nov 23, 2015
eff8334
Constructor options tests
vlazar Nov 24, 2015
ba73e77
List initialization tests
vlazar Nov 24, 2015
267d2b8
Fix bug exposed by tests: Can't init list from list html attribute.
vlazar Nov 23, 2015
6bee532
Html modifications tests
vlazar Nov 23, 2015
336c21e
Fix bug exposed by tests: List isn't hidden by default.
vlazar Nov 23, 2015
9c4e25e
Awesomplete.all tests
vlazar Nov 24, 2015
fff2793
Awesomplete.FILTER_CONTAINS tests
vlazar Nov 24, 2015
c97c34f
Awesomplete.FILTER_STARTSWITH tests
vlazar Nov 24, 2015
2b2e3ed
Awesomplete.SORT_BYLENGTH tests
vlazar Nov 24, 2015
e3d15e3
Awesomplete.$.bind tests
vlazar Nov 24, 2015
375a452
Awesomplete.$.create tests
vlazar Nov 24, 2015
2017243
Awesomplete.$ tests
vlazar Nov 24, 2015
e85128e
Awesomplete.$$ tests
vlazar Nov 24, 2015
643ccfb
Awesomplete.$.fire tests
vlazar Nov 24, 2015
4a0e723
Awesomplete.$.regExpEscape tests
vlazar Nov 24, 2015
dcd4620
awesomplete.close tests
vlazar Nov 24, 2015
4b7a449
awesomplete.open tests
vlazar Nov 24, 2015
4fc68f6
awesomplete.next tests
vlazar Nov 24, 2015
93a56a3
awesomplete.previous tests
vlazar Nov 24, 2015
8781595
awesomplete.goto tests
vlazar Nov 24, 2015
ca9c91a
awesomplete.select tests
vlazar Nov 24, 2015
132906c
awesomplete.evaluate tests
vlazar Nov 24, 2015
03bb15a
awesomplete.opened tests
vlazar Nov 24, 2015
6cd69b2
awesomplete.selected tests
vlazar Nov 24, 2015
625cd57
blur event tests
vlazar Nov 24, 2015
c1c5e13
input event tests
vlazar Nov 24, 2015
94bb12f
mousedown event tests
vlazar Nov 24, 2015
1323113
form submit event tests
vlazar Nov 24, 2015
b08fd22
keydown event tests
vlazar Nov 24, 2015
df992f7
Fix tests run in Firefox
vlazar Nov 25, 2015
f4cef99
Simplify tests by inlining and using $.spyOnEvent helper
vlazar Nov 25, 2015
00f0630
Covered some missed branches
vlazar Nov 25, 2015
db50ff8
Progress reporter is too noisy on TravisCI. Use dots reporter instead.
vlazar Nov 25, 2015
f793f05
Remove unused dependency
vlazar Nov 25, 2015
17934ef
Merge remote-tracking branch 'upstream/gh-pages' into features/improv…
vlazar Dec 5, 2015
f9d6cca
Tests for merged #16791 Don't select items on mouse right-click
vlazar Dec 5, 2015
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions awesomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var _ = function (input, o) {
});

this.ul = $.create("ul", {
hidden: "",
hidden: "hidden",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$.create when working with attributes would do:

element['hidden'] = '';

Which is for boolean attribute would be the same as:

element['hidden'] = false;

So completer would not be hidden by default.

I don't remember why I didn't used hidden: false here. Maybe there was some bug with it. I'll check this later.

inside: this.container
});

Expand Down Expand Up @@ -102,8 +102,8 @@ var _ = function (input, o) {
}});

if (this.input.hasAttribute("list")) {
this.list = "#" + input.getAttribute("list");
input.removeAttribute("list");
this.list = "#" + this.input.getAttribute("list");
this.input.removeAttribute("list");
}
else {
this.list = this.input.getAttribute("data-list") || o.list || [];
Expand Down
12 changes: 9 additions & 3 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ module.exports = function(config) {

// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],
frameworks: ['jasmine', 'jasmine-def', 'fixture'],

// list of files / patterns to load in the browser
files: [
'awesomplete.js',
'test/**/*Shared.js',
'test/specHelper.js',
{
pattern: 'test/fixtures/**/*.html',
watched: true, included: true, served: true
},
'test/**/*Shared.js',
'test/**/*Spec.js'
],

Expand All @@ -22,12 +27,13 @@ module.exports = function(config) {
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'**/*.html' : ['html2js']
},

// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
reporters: ['dots'],

// web server port
port: 9876,
Expand Down
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
"homepage": "https://leaverou.github.io/awesomplete/",
"devDependencies": {
"jasmine-core": "^2.3.0",
"karma": "^0.12.31",
"karma-chrome-launcher": "^0.1.8",
"karma-jasmine": "^0.3.5",
"karma-phantomjs-launcher": "^0.1.4"
"karma": "^0.13.15",
"karma-chrome-launcher": "^0.2.1",
"karma-fixture": "^0.2.5",
"karma-html2js-preprocessor": "^0.1.0",
"karma-jasmine": "^0.3.6",
"karma-jasmine-def": "^0.1.0"
}
}
30 changes: 30 additions & 0 deletions test/api/closeSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
describe("awesomplete.close", function () {

$.fixture("plain");

subject(function () { return new Awesomplete("#plain") });

beforeEach(function () {
this.subject.open();
this.subject.next();
});

it("closes completer", function () {
this.subject.close();
expect(this.subject.ul.hasAttribute("hidden")).toBe(true);
});

it("makes no item selected", function () {
this.subject.close();

expect(this.subject.selected).toBe(false);
expect(this.subject.index).toBe(-1);
});

it("fires awesomplete-close event", function () {
var handler = $.spyOnEvent(this.subject.input, "awesomplete-close");
this.subject.close();

expect(handler).toHaveBeenCalled();
});
});
57 changes: 57 additions & 0 deletions test/api/evaluateSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
describe("awesomplete.evaluate", function () {

$.fixture("plain");

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

describe("with too short input value", function () {
beforeEach(function () {
$.type(this.subject.input, "i");
});

it("closes completer", function () {
spyOn(this.subject, "close");
this.subject.evaluate();

expect(this.subject.close).toHaveBeenCalled();
});
});

describe("with no items found", function () {
beforeEach(function () {
$.type(this.subject.input, "nosuchitem");
});

it("closes completer", function () {
spyOn(this.subject, "close");
this.subject.evaluate();

expect(this.subject.close).toHaveBeenCalled();
});
});

describe("with some items found", function () {
beforeEach(function () {
$.type(this.subject.input, "ite");
});

it("opens completer", function () {
spyOn(this.subject, "open");
this.subject.evaluate();

expect(this.subject.open).toHaveBeenCalled();
});

it("fills completer with found items", function () {
this.subject.evaluate();
expect(this.subject.ul.children.length).toBe(3);
});

it("makes no item selected", function () {
this.subject.evaluate();
expect(this.subject.selected).toBe(false);
});
});
});
64 changes: 64 additions & 0 deletions test/api/gotoSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
describe("awesomplete.goto", 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 });

beforeEach(function () {
$.type(this.subject.input, "ite");
});

it("clears previous aria-selected", function () {
this.subject.goto(this.firstIndex);
this.subject.goto(this.lastIndex);

expect(this.subject.ul.children[this.firstIndex].getAttribute("aria-selected")).toBe("false");
});

it("goes to first item", function () {
this.subject.goto(this.firstIndex);
expect(this.subject.index).toBe(this.firstIndex);
});

it("goes to last item", function () {
this.subject.goto(this.lastIndex);
expect(this.subject.index).toBe(this.lastIndex);
});

it("fires awesomplete-highlight event", function () {
var handler = $.spyOnEvent(this.subject.input, "awesomplete-highlight");
this.subject.goto(1);

expect(handler).toHaveBeenCalled();
});

describe("with item index > -1", function () {
beforeEach(function () {
this.subject.goto(this.firstIndex);
});

it("sets aria-selected", function () {
expect(this.subject.ul.children[this.firstIndex].getAttribute("aria-selected")).toBe("true");
});

it("updates status", function () {
expect(this.subject.status.textContent).toBe("item1");
});
});

describe("with item index = -1", function () {
beforeEach(function () {
this.subject.goto(this.firstIndex);
this.subject.goto(-1);
});

it("does not update status", function () {
expect(this.subject.status.textContent).toBe("item1");
});
});
});
57 changes: 57 additions & 0 deletions test/api/nextSpec.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
});
});
50 changes: 50 additions & 0 deletions test/api/openSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
describe("awesomplete.open", function () {

$.fixture("plain");

subject(function () { return new Awesomplete("#plain", this.options) });

it("opens completer", function () {
this.subject.open();
expect(this.subject.ul.hasAttribute("hidden")).toBe(false);
});

// Exposes this bug https://github.com/LeaVerou/awesomplete/pull/16740
// FIXME better fix is probably required as discussed in PR above
xit("fills in the list on creation", function () {
$("#plain").value = "ite";
this.options = { list: "item1, item2" };
this.subject.open();

expect(this.subject.ul.children.length).toBe(2);
});

it("fires awesomplete-open event", function () {
var handler = $.spyOnEvent(this.subject.input, "awesomplete-open");
this.subject.open();

expect(handler).toHaveBeenCalled();
});

describe("with autoFirst: true", function () {
def("options", function () { return { autoFirst: true } });

it("automatically selects first item", function () {
spyOn(this.subject, "goto");
this.subject.open();

expect(this.subject.goto).toHaveBeenCalledWith(0);
});
});

describe("with autoFirst: false", function () {
def("options", function () { return { autoFirst: false } });

it("does not select any item", function () {
this.subject.open();

expect(this.subject.selected).toBe(false);
expect(this.subject.index).toBe(-1);
});
});
});
26 changes: 26 additions & 0 deletions test/api/openedSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
describe("awesomplete.opened", function () {

$.fixture("plain");

subject(function () { return new Awesomplete("#plain") });

describe("with newly created completer", function () {
it("is false", function () {
expect(this.subject.opened).toBe(false);
});
});

describe("with opened completer", function () {
it("is true", function () {
this.subject.open();
expect(this.subject.opened).toBe(true);
});
});

describe("with closed completer", function () {
it("is false", function () {
this.subject.close();
expect(this.subject.opened).toBe(false);
});
});
});
Loading