summaryrefslogtreecommitdiffstats
path: root/test/javascripts/repository_search_test.js
blob: 989141313e3907d6361860ebb85bdfce8e0272f1 (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
  #--
  #   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 <http://www.gnu.org/licenses/>.
  #--
*/

TestCase("Live search for repositories", {
    "test should create the container if it doesn't exist": function() {
        /*:DOC += <div id="repo_search"><input type="text" /></div>*/
        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 += <div id="repo_search"><ol class="live-search-results"></ol></div> */
        jQuery("#repo_search").liveSearch();
        assertEquals(1, jQuery("#repo_search").find(".live-search-results").length);
    },

    "test should allow a custom result container": function() {
        /*:DOC += <div id="repo_search"><ol class="results"></ol></div> */
        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 += <div id="repo_search"></div>*/
        jQuery("#no_such_domid").liveSearch();
        assertEquals(0, jQuery(".live-search-results").length);
    },

    "test should hide the result container": function() {
        /*:DOC += <div id="repo_search"><li class="live-search-results"></li></div> */
        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 += <div id="repo_search"></div>*/
        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 += <div id="repo_search"></div>*/
        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 += <div id="repo_search"></div>*/
        var api = jQuery("#repo_search").liveSearch({});
        assertThrows(TypeError, function (){
            api.populate("testing");
        })
    },
    "test should use the renderer": function () {
        /*:DOC += <div id="s"></div> */
        var renderer = {
            render: function(person) {
                row = jQuery('<li class="foo"></li>');
                (jQuery('<h2 title="' + person.nick + '">' + person.firstName + '</h2>')).appendTo(row);
                (jQuery("<addr>" + person.address + "</addr>")).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 += <div id="s"></div> */
        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 += <div id="s"></div> */
        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 += <div id="s"><div class="reset" style="display:block"></div></div>*/
        var api = jQuery("#s").liveSearch();
        assertEquals(1, jQuery("#s .reset:hidden").length);
    },
    "test should display the reset element when populating": function() {
        /*:DOC += <div id="_s"><div class="reset" style="display:block"></div></div>*/
        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 += <div id="s"></div> */
        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 += <div id="s"><div class="no-results-found"></div></div> */
        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 += <div id="s"><div class="no-results-found"></div></div> */
        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);
        api.populate([{name:"John"}]);
        assertEquals(0, jQuery("#s .no-results-found:visible").length);
    }
});