/*
#--
# Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see .
#--
*/
TestCase("Live search for repositories", {
"test should create the container if it doesn't exist": function() {
/*:DOC +=
*/
jQuery("#repo_search").liveSearch();
assertEquals(1, jQuery("#repo_search").find(".live-search-results").length);
},
"test should not create the container if it exists": function() {
/*:DOC +=
*/
jQuery("#repo_search").liveSearch();
assertEquals(1, jQuery("#repo_search").find(".live-search-results").length);
},
"test should allow a custom result container": function() {
/*:DOC +=
*/
jQuery("#repo_search").liveSearch({resultContainer: ".results"});
assertEquals(1, jQuery("#repo_search").find(".results").length);
},
"test should do nothing if the selector doesn't exist": function() {
/*:DOC += */
jQuery("#no_such_domid").liveSearch();
assertEquals(0, jQuery(".live-search-results").length);
},
"test should hide the result container": function() {
/*:DOC +=
*/
assertEquals(0, jQuery("#repo_search .live-search-results:hidden").length);
jQuery("#repo_search").liveSearch();
assertEquals(1, jQuery("#repo_search .live-search-results:hidden").length);
},
"test should call the backend's get func when searching": function() {
/*:DOC += */
var result;
var backend = {
get: function(uri, phrase, callback) {
result = phrase;
}
};
var api = jQuery("#repo_search").liveSearch(backend);
api.performSearch("Foo");
assertEquals("Foo", result);
},
"test should append search results to result container": function() {
/*:DOC += */
var backend = {
get: function(uri, phrase, callback) {
result = [{"name":"gitorious"}];
callback(result);
}
};
var api = jQuery("#repo_search").liveSearch(backend, {itemClass: "item"});
api.performSearch("Foo");
// The default renderer renders with li class="item"
assertEquals(1, jQuery("#repo_search .item").length);
},
"test should handle non-JSON or invalid responses": function (){
/*:DOC += */
var api = jQuery("#repo_search").liveSearch({});
assertThrows(TypeError, function (){
api.populate("testing");
})
},
"test should use the renderer": function () {
/*:DOC += */
var renderer = {
render: function(person) {
row = jQuery('');
(jQuery('
' + person.firstName + '
')).appendTo(row);
(jQuery("" + person.address + "")).appendTo(row);
return row;
}
};
var api = jQuery("#s").liveSearch({}, {renderer: renderer});
var ourData = [{
firstName: "Winnie",
address: "Hundred yard forest",
nick: "pooh"
}];
api.populate(ourData);
assertEquals("Winnie", jQuery("li.foo h2").html());
},
"test should call the optional onDisplay when displaying results": function() {
/*:DOC += */
var othersHidden = false;
var hideOthers = function () {
othersHidden = true;
}
var api = jQuery("#s").liveSearch({}, {onDisplay: hideOthers});
api.populate({});
assertTrue(othersHidden);
},
"test should call the optional onReset when resetting": function() {
/*:DOC += */
var othersDisplayed = false;
var displayOthers = function () {
othersDisplayed = true;
}
var api = jQuery("#s").liveSearch({}, {onReset: displayOthers});
api.reset();
assertTrue(othersDisplayed);
},
"test should hide the reset element if one exists": function() {
/*:DOC +=
*/
var api = jQuery("#s").liveSearch();
assertEquals(1, jQuery("#s .reset:hidden").length);
},
"test should display the reset element when populating": function() {
/*:DOC +=
*/
var api = jQuery("#_s").liveSearch();
assertEquals(0, jQuery("#_s .reset:visible").length);
api.populate([{name: "John Doe"}]);
assertEquals(1, jQuery("#_s .reset:visible").length);
},
"test should reset itself when an empty query is entered": function (){
/*:DOC += */
var api = jQuery("#s").liveSearch();
var reset = false;
api.reset = function() {
reset = true;
};
api.queueSearch("");
assertTrue(reset);
},
"test should insert a text when no matches found": function (){
/*:DOC +=
*/
assertEquals(1, jQuery("#s .no-results-found:visible").length);
var api = jQuery("#s").liveSearch();
assertEquals(0, jQuery("#s .no-results-found:visible").length);
api.populate([]);
assertEquals(1, jQuery("#s .no-results-found:visible").length);
},
"test should hide the no-results container when matches found": function (){
/*:DOC +=