Skip to content

Commit

Permalink
TDD - added failing test for stable sort
Browse files Browse the repository at this point in the history
  • Loading branch information
pazams committed Oct 28, 2015
1 parent 6b4026e commit 81d5c6d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
22 changes: 13 additions & 9 deletions awesomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ _.prototype = {
}
}

this._currentEvaluation = []; // used for unit testing.

if (document.activeElement === this.input) {
this.evaluate();
}
Expand Down Expand Up @@ -219,16 +221,18 @@ _.prototype = {
// Populate list with options that match
this.ul.innerHTML = "";

this._list
.filter(function(item) {
return me.filter(item, value);
})
.sort(this.sort)
.every(function(text, i) {
me.ul.appendChild(me.item(text, value));
this._currentEvaluation = this._list
.filter(function(item) {
return me.filter(item, value);
})
.sort(this.sort);

return i < me.maxItems - 1;
});
this._currentEvaluation
.every(function(text, i) {
me.ul.appendChild(me.item(text, value));

return i < me.maxItems - 1;
});

if (this.ul.children.length === 0) {
this.close();
Expand Down
14 changes: 13 additions & 1 deletion test/awesompleteSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ describe("awesomplete",function(){
awesompleter = new Awesomplete(dummyInput,{minChars:3});
expect(awesompleter.minChars).toBe(3);
})

// see http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.11
// Chrome's native Array.prototype.sort does not perform stable sorts with arrays above 10 items.
it("should perform a stable sort inside evaluation chain",function(){
var origList = ["New York","Staten Island","Jamaica","Schenectady","Flushing","White Plains","Great Neck","Yonkers","Utica","Elmira","Binghamton"];

awesompleter = new Awesomplete(dummyInput,{minChars:0, sort: function(a,b){ return 0;}});
awesompleter.list = origList;
awesompleter.evaluate();
expect(awesompleter._currentEvaluation).toEqual(origList);
})

})

});
Expand Down Expand Up @@ -72,4 +84,4 @@ describe("awesomplete helpers",function(){
})
})

});
});

0 comments on commit 81d5c6d

Please sign in to comment.