Skip to content

Commit 84e0ce1

Browse files
committed
Tooltip: Unit tests for tooltip and a fix to pass through event objects to triggered events
1 parent acfc66e commit 84e0ce1

File tree

9 files changed

+206
-30
lines changed

9 files changed

+206
-30
lines changed

tests/unit/index.html

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,6 @@
1919
}
2020
</style>
2121

22-
<script type="text/javascript" src="../../jquery-1.4.2.js"></script>
23-
<script type="text/javascript" src="../../ui/jquery.ui.core.js"></script>
24-
<script type="text/javascript" src="../../ui/jquery.ui.widget.js"></script>
25-
<script type="text/javascript" src="../../ui/jquery.ui.mouse.js"></script>
26-
<script type="text/javascript" src="../../ui/jquery.ui.accordion.js"></script>
27-
<script type="text/javascript" src="../../ui/jquery.ui.autocomplete.js"></script>
28-
<script type="text/javascript" src="../../ui/jquery.ui.button.js"></script>
29-
<script type="text/javascript" src="../../ui/jquery.ui.datepicker.js"></script>
30-
<script type="text/javascript" src="../../ui/jquery.ui.dialog.js"></script>
31-
<script type="text/javascript" src="../../ui/jquery.ui.draggable.js"></script>
32-
<script type="text/javascript" src="../../ui/jquery.ui.droppable.js"></script>
33-
<script type="text/javascript" src="../../ui/jquery.ui.progressbar.js"></script>
34-
<script type="text/javascript" src="../../ui/jquery.ui.resizable.js"></script>
35-
<script type="text/javascript" src="../../ui/jquery.ui.selectable.js"></script>
36-
<script type="text/javascript" src="../../ui/jquery.ui.slider.js"></script>
37-
<script type="text/javascript" src="../../ui/jquery.ui.sortable.js"></script>
38-
<script type="text/javascript" src="../../ui/jquery.ui.tabs.js"></script>
39-
40-
<script type="text/javascript" src="../../external/jquery.cookie.js"></script>
41-
<script type="text/javascript" src="../jquery.simulate.js"></script>
42-
4322
</head>
4423
<body>
4524

@@ -69,6 +48,7 @@ <h2>Widgets</h2>
6948
<li><a href="progressbar/progressbar.html">Progressbar</a></li>
7049
<li><a href="slider/slider.html">Slider</a></li>
7150
<li><a href="tabs/tabs.html">Tabs</a></li>
51+
<li><a href="tooltip/tooltip.html">Tooltip</a></li>
7252
</ul>
7353

7454
<h2>Utilities</h2>

tests/unit/tooltip/tooltip.html

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<title>jQuery UI Tooltip Test Suite</title>
6+
7+
<link type="text/css" href="../../../themes/base/jquery.ui.tooltip.css" rel="stylesheet" />
8+
9+
<script type="text/javascript" src="../../../jquery-1.4.2.js"></script>
10+
<script type="text/javascript" src="../../../ui/jquery.ui.core.js"></script>
11+
<script type="text/javascript" src="../../../ui/jquery.ui.widget.js"></script>
12+
<script type="text/javascript" src="../../../ui/jquery.ui.tooltip.js"></script>
13+
14+
<link rel="stylesheet" href="../../../external/qunit.css" type="text/css"/>
15+
<script type="text/javascript" src="../../../external/qunit.js"></script>
16+
<script type="text/javascript" src="../../jquery.simulate.js"></script>
17+
<script type="text/javascript" src="../testsuite.js"></script>
18+
19+
<script type="text/javascript" src="tooltip_core.js"></script>
20+
<script type="text/javascript" src="tooltip_defaults.js"></script>
21+
<script type="text/javascript" src="tooltip_events.js"></script>
22+
<script type="text/javascript" src="tooltip_methods.js"></script>
23+
<script type="text/javascript" src="tooltip_options.js"></script>
24+
<script type="text/javascript" src="tooltip_tickets.js"></script>
25+
26+
</head>
27+
<body>
28+
29+
<h1 id="qunit-header">jQuery UI Tooltip Test Suite</h1>
30+
<h2 id="qunit-banner"></h2>
31+
<h2 id="qunit-userAgent"></h2>
32+
<ol id="qunit-tests">
33+
</ol>
34+
35+
<div id="main" style="position: absolute; top: -10000px; left: -10000px;">
36+
<div>
37+
<a id="tooltipped1" href="#" title="anchortitle">anchor</a>
38+
<input title="inputtitle" />
39+
</div>
40+
</div>
41+
42+
</body>
43+
</html>

tests/unit/tooltip/tooltip_core.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* tooltip_core.js
3+
*/
4+
5+
6+
(function($) {
7+
8+
module("tooltip: core");
9+
10+
11+
})(jQuery);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* tooltip_defaults.js
3+
*/
4+
5+
var tooltip_defaults = {
6+
disabled: false,
7+
content: $.ui.tooltip.prototype.options.content,
8+
tooltipClass: "ui-widget-content",
9+
position: {
10+
my: "left center",
11+
at: "right center",
12+
offset: "15 0"
13+
}
14+
};
15+
16+
commonWidgetTests('tooltip', { defaults: tooltip_defaults });

tests/unit/tooltip/tooltip_events.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* tooltip_events.js
3+
*/
4+
(function($) {
5+
6+
module("tooltip: events");
7+
8+
test("programmatic triggers", function() {
9+
expect(2);
10+
var e = $("#tooltipped1").tooltip({
11+
open: function(event, ui) {
12+
same( event.type, "tooltipopen" );
13+
},
14+
close: function(event, ui) {
15+
same( event.type, "tooltipclose" );
16+
}
17+
});
18+
e.tooltip("open").tooltip("close");
19+
e.tooltip("destroy");
20+
});
21+
22+
test("mouse events", function() {
23+
expect(4);
24+
var e = $("#tooltipped1").tooltip({
25+
open: function(event, ui) {
26+
same( event.type, "tooltipopen" );
27+
same( event.originalEvent.type, "mouseenter" );
28+
},
29+
close: function(event, ui) {
30+
same( event.type, "tooltipclose" );
31+
same( event.originalEvent.type, "mouseleave" );
32+
}
33+
});
34+
e.trigger("mouseover").trigger("mouseout");
35+
e.tooltip("destroy");
36+
});
37+
38+
test("focus events", function() {
39+
expect(4);
40+
var e = $("#tooltipped1").tooltip({
41+
open: function(event, ui) {
42+
same( event.type, "tooltipopen" );
43+
same( event.originalEvent.type, "focus" );
44+
},
45+
close: function(event, ui) {
46+
same( event.type, "tooltipclose" );
47+
same( event.originalEvent.type, "blur" );
48+
}
49+
});
50+
e.trigger("focus").trigger("blur");
51+
e.tooltip("destroy");
52+
});
53+
54+
})(jQuery);

tests/unit/tooltip/tooltip_methods.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* tooltip_methods.js
3+
*/
4+
(function($) {
5+
6+
7+
module("tooltip: methods");
8+
9+
test("destroy", function() {
10+
var beforeHtml = $("#tooltipped1").parent().html();
11+
var afterHtml = $("#tooltipped1").tooltip().tooltip("destroy").parent().html();
12+
equal( afterHtml, beforeHtml );
13+
});
14+
15+
test("open", function() {
16+
var e = $("#tooltipped1").tooltip();
17+
ok( $(".ui-tooltip").is(":hidden") );
18+
e.tooltip("open");
19+
ok( $(".ui-tooltip").is(":visible") );
20+
$(":ui-tooltip").tooltip("destroy");
21+
});
22+
23+
})(jQuery);

tests/unit/tooltip/tooltip_options.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* tooltip_options.js
3+
*/
4+
(function($) {
5+
6+
module("tooltip: options");
7+
8+
function contentTest(name, expected, impl) {
9+
test(name, function() {
10+
$("#tooltipped1").tooltip({
11+
content: impl
12+
}).tooltip("open");
13+
same( $(".ui-tooltip").text(), expected );
14+
$(":ui-tooltip").tooltip("destroy");
15+
});
16+
}
17+
18+
contentTest("content: default", "anchortitle");
19+
contentTest("content: return string", "customstring", function() {
20+
return "customstring";
21+
});
22+
contentTest("content: callback string", "customstring2", function(response) {
23+
response("customstring2");
24+
});
25+
26+
test("tooltipClass, default", function() {
27+
$("#tooltipped1").tooltip().tooltip("open");
28+
same( $(".ui-tooltip").attr("class"), "ui-tooltip ui-widget ui-corner-all ui-widget-content");
29+
$(":ui-tooltip").tooltip("destroy");
30+
});
31+
test("tooltipClass, custom", function() {
32+
$("#tooltipped1").tooltip({
33+
tooltipClass: "pretty fancy"
34+
}).tooltip("open");
35+
same( $(".ui-tooltip").attr("class"), "ui-tooltip ui-widget ui-corner-all pretty fancy");
36+
$(":ui-tooltip").tooltip("destroy");
37+
});
38+
39+
})(jQuery);

tests/unit/tooltip/tooltip_tickets.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* tooltip_tickets.js
3+
*/
4+
(function($) {
5+
6+
module("tooltip: tickets");
7+
8+
9+
10+
})(jQuery);

ui/jquery.ui.tooltip.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ $.widget("ui.tooltip", {
4949
this.opacity = this.tooltip.css("opacity");
5050
this.element
5151
.bind("focus.tooltip mouseenter.tooltip", function(event) {
52-
self.open();
52+
self.open( event );
5353
})
5454
.bind("blur.tooltip mouseleave.tooltip", function(event) {
55-
self.close();
55+
self.close( event );
5656
});
5757
},
5858

@@ -73,7 +73,7 @@ $.widget("ui.tooltip", {
7373
return this.tooltip;
7474
},
7575

76-
open: function() {
76+
open: function(event) {
7777
var target = this.element;
7878
// already visible? possible when both focus and mouseover events occur
7979
if (this.current && this.current[0] == target[0])
@@ -84,14 +84,14 @@ $.widget("ui.tooltip", {
8484
var content = this.options.content.call(target[0], function(response) {
8585
// ignore async responses that come in after the tooltip is already hidden
8686
if (self.current == target)
87-
self._show(target, response);
87+
self._show(event, target, response);
8888
});
8989
if (content) {
90-
self._show(target, content);
90+
self._show(event, target, content);
9191
}
9292
},
9393

94-
_show: function(target, content) {
94+
_show: function(event, target, content) {
9595
if (!content)
9696
return;
9797

@@ -116,10 +116,10 @@ $.widget("ui.tooltip", {
116116
else
117117
this.tooltip.is(':visible') ? this.tooltip.fadeTo("normal", this.opacity) : this.tooltip.fadeIn();
118118

119-
this._trigger( "open" );
119+
this._trigger( "open", event );
120120
},
121121

122-
close: function() {
122+
close: function(event) {
123123
if (!this.current)
124124
return;
125125

@@ -137,7 +137,7 @@ $.widget("ui.tooltip", {
137137
else
138138
this.tooltip.stop().fadeOut();
139139

140-
this._trigger( "close" );
140+
this._trigger( "close", event );
141141
}
142142

143143
});

0 commit comments

Comments
 (0)