Permalink
Browse files

Merge branch 'master' into user

Conflicts:
	test/index.html
  • Loading branch information...
2 parents d6df555 + 7871b7a commit 9acfd2c3e5683679998fc0c9240c90fbe60f5b93 @bengillies bengillies committed Aug 2, 2012
Showing with 108 additions and 2 deletions.
  1. +32 −0 src/search_widget.js
  2. +2 −2 src/tsbar.js
  3. +2 −0 test/index.html
  4. +72 −0 test/test_search.js
View
@@ -0,0 +1,32 @@
+function SearchWidget() {
+
+ this._widget = tsbar.Widget({
+ el: '<button>Search</button>',
+ popup: '<div class="popup">' +
+ '<span>Search:</span>' +
+ '<input id="query-text" type="text" name="query"/>' +
+ '<input id="search-button" type="submit" value="Find"/>' +
+ '<input id="clear-button" type="submit" value="Clear"/>' +
+ '<div id="search-results"></div>' +
+ '</div>'
+ });
+}
+
+SearchWidget.prototype.getWidget = function() {
+ return this._widget;
+}
+
+SearchWidget.prototype.registerListeners = function() {
+ $('#search-button').click(this._doSearch);
+ $('#clear-button').click(this._doClear);
+}
+
+SearchWidget.prototype._doSearch = function() {
+ var query = $('#query-text').val();
+ $('#search-results').load('/hsearch?q=' + query + ' #container');
+}
+
+SearchWidget.prototype._doClear = function() {
+ $('#search-results').html('');
+ $('#query-text').val('');
+}
View
@@ -6,7 +6,7 @@
* Requires jQuery
*
*/
-(function(exports) {
+(function(exports, $) {
// create the main namespace for tsbar
var tsbar = {};
@@ -119,4 +119,4 @@
// export the bar to the global object
exports.tsbar = tsbar;
-}(window));
+}(window, jQuery));
View
@@ -24,8 +24,10 @@ <h2 id="qunit-userAgent"></h2>
<!--load your scripts in here-->
<script src="../src/tsbar.js" type="text/javascript"></script>
<script src="../src/user.js" type="text/javascript"></script>
+ <script src="../src/search_widget.js" type="text/javascript"></script>
<!--load tests-->
<script src="test_main.js" type="text/javascript"></script>
+ <script src="test_search.js" type="text/javascript"></script>
</body>
</html>
View
@@ -0,0 +1,72 @@
+var searchWidget = undefined;
+
+module('search widget', {
+ setup: function() {
+ tsbar.clear();
+ searchWidget = new SearchWidget();
+ },
+ teardown: function() {
+ $('#sandbox').html('');
+ }
+});
+
+test('can create a search widget', function() {
+ strictEqual(searchWidget.getWidget().find("input[type='text']").length, 1,
+ 'the widget contains an input text field');
+ strictEqual(searchWidget.getWidget().find("input[value='Find']").length, 1,
+ 'the widget contains a find button');
+ strictEqual(searchWidget.getWidget().find("input[value='Clear']").length, 1,
+ 'the widget contains a clear button');
+ strictEqual(searchWidget.getWidget().find("div:not('.popup')").length, 1,
+ 'the widget contains a div area for search results');
+});
+
+test('can perform a search', function() {
+ fakeAjaxResponse();
+
+ tsbar.define('left', searchWidget.getWidget());
+ var toolbar = tsbar.render();
+ $('#sandbox').append(toolbar);
+ searchWidget.registerListeners();
+
+ performSearch();
+
+ strictEqual(searchWidget.getWidget().find("div:not('.popup')").html(),
+ '<div id="container"><div>some search results</div></div>', 'the search results were returned');
+});
+
+test('can clear the search results', function() {
+ fakeAjaxResponse();
+
+ tsbar.define('left', searchWidget.getWidget());
+ var toolbar = tsbar.render();
+ $('#sandbox').append(toolbar);
+ searchWidget.registerListeners();
+
+ performSearch();
+
+ searchWidget.getWidget().find("input[value='Clear']").click();
+
+ strictEqual(searchWidget.getWidget().find("div:not('.popup'):empty").length, 1,
+ 'the search results area was cleared');
+ strictEqual(searchWidget.getWidget().find("input[type='text']").val(), '',
+ 'the search query text was cleared');
+});
+
+function fakeAjaxResponse() {
+ $.ajax = function(options) {
+ strictEqual(options.url, '/hsearch?q=test', 'the correct URL was sent');
+ options.complete({
+ responseText: '<body><div id="container"><div>some search results</div></div></body>',
+ isResolved: function() { return true; },
+ done: function() {}
+ });
+ };
+}
+
+function performSearch() {
+ searchWidget.getWidget().find('.tsbar-widget button').click();
+ searchWidget.getWidget().find("input[type='text']").val('test');
+ searchWidget.getWidget().find("input[value='Find']").click();
+}
+

0 comments on commit 9acfd2c

Please sign in to comment.