Mercurial > wikked
comparison static/bootstrap/js/tests/unit/bootstrap-tooltip.js @ 88:a5a3d454eac9
Updated Bootstrap.
| author | Ludovic Chabant <ludovic@chabant.com> |
|---|---|
| date | Fri, 05 Apr 2013 08:08:12 -0700 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 87:c0cf67362fb1 | 88:a5a3d454eac9 |
|---|---|
| 1 $(function () { | |
| 2 | |
| 3 module("bootstrap-tooltip") | |
| 4 | |
| 5 test("should provide no conflict", function () { | |
| 6 var tooltip = $.fn.tooltip.noConflict() | |
| 7 ok(!$.fn.tooltip, 'tooltip was set back to undefined (org value)') | |
| 8 $.fn.tooltip = tooltip | |
| 9 }) | |
| 10 | |
| 11 test("should be defined on jquery object", function () { | |
| 12 var div = $("<div></div>") | |
| 13 ok(div.tooltip, 'popover method is defined') | |
| 14 }) | |
| 15 | |
| 16 test("should return element", function () { | |
| 17 var div = $("<div></div>") | |
| 18 ok(div.tooltip() == div, 'document.body returned') | |
| 19 }) | |
| 20 | |
| 21 test("should expose default settings", function () { | |
| 22 ok(!!$.fn.tooltip.defaults, 'defaults is defined') | |
| 23 }) | |
| 24 | |
| 25 test("should empty title attribute", function () { | |
| 26 var tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"></a>').tooltip() | |
| 27 ok(tooltip.attr('title') === '', 'title attribute was emptied') | |
| 28 }) | |
| 29 | |
| 30 test("should add data attribute for referencing original title", function () { | |
| 31 var tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"></a>').tooltip() | |
| 32 equals(tooltip.attr('data-original-title'), 'Another tooltip', 'original title preserved in data attribute') | |
| 33 }) | |
| 34 | |
| 35 test("should place tooltips relative to placement option", function () { | |
| 36 $.support.transition = false | |
| 37 var tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"></a>') | |
| 38 .appendTo('#qunit-fixture') | |
| 39 .tooltip({placement: 'bottom'}) | |
| 40 .tooltip('show') | |
| 41 | |
| 42 ok($(".tooltip").is('.fade.bottom.in'), 'has correct classes applied') | |
| 43 tooltip.tooltip('hide') | |
| 44 }) | |
| 45 | |
| 46 test("should allow html entities", function () { | |
| 47 $.support.transition = false | |
| 48 var tooltip = $('<a href="#" rel="tooltip" title="<b>@fat</b>"></a>') | |
| 49 .appendTo('#qunit-fixture') | |
| 50 .tooltip({html: true}) | |
| 51 .tooltip('show') | |
| 52 | |
| 53 ok($('.tooltip b').length, 'b tag was inserted') | |
| 54 tooltip.tooltip('hide') | |
| 55 ok(!$(".tooltip").length, 'tooltip removed') | |
| 56 }) | |
| 57 | |
| 58 test("should respect custom classes", function () { | |
| 59 var tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"></a>') | |
| 60 .appendTo('#qunit-fixture') | |
| 61 .tooltip({ template: '<div class="tooltip some-class"><div class="tooltip-arrow"/><div class="tooltip-inner"/></div>'}) | |
| 62 .tooltip('show') | |
| 63 | |
| 64 ok($('.tooltip').hasClass('some-class'), 'custom class is present') | |
| 65 tooltip.tooltip('hide') | |
| 66 ok(!$(".tooltip").length, 'tooltip removed') | |
| 67 }) | |
| 68 | |
| 69 test("should fire show event", function () { | |
| 70 stop() | |
| 71 var tooltip = $('<div title="tooltip title"></div>') | |
| 72 .bind("show", function() { | |
| 73 ok(true, "show was called") | |
| 74 start() | |
| 75 }) | |
| 76 .tooltip('show') | |
| 77 }) | |
| 78 | |
| 79 test("should fire shown event", function () { | |
| 80 stop() | |
| 81 var tooltip = $('<div title="tooltip title"></div>') | |
| 82 .bind("shown", function() { | |
| 83 ok(true, "shown was called") | |
| 84 start() | |
| 85 }) | |
| 86 .tooltip('show') | |
| 87 }) | |
| 88 | |
| 89 test("should not fire shown event when default prevented", function () { | |
| 90 stop() | |
| 91 var tooltip = $('<div title="tooltip title"></div>') | |
| 92 .bind("show", function(e) { | |
| 93 e.preventDefault() | |
| 94 ok(true, "show was called") | |
| 95 start() | |
| 96 }) | |
| 97 .bind("shown", function() { | |
| 98 ok(false, "shown was called") | |
| 99 }) | |
| 100 .tooltip('show') | |
| 101 }) | |
| 102 | |
| 103 test("should fire hide event", function () { | |
| 104 stop() | |
| 105 var tooltip = $('<div title="tooltip title"></div>') | |
| 106 .bind("shown", function() { | |
| 107 $(this).tooltip('hide') | |
| 108 }) | |
| 109 .bind("hide", function() { | |
| 110 ok(true, "hide was called") | |
| 111 start() | |
| 112 }) | |
| 113 .tooltip('show') | |
| 114 }) | |
| 115 | |
| 116 test("should fire hidden event", function () { | |
| 117 stop() | |
| 118 var tooltip = $('<div title="tooltip title"></div>') | |
| 119 .bind("shown", function() { | |
| 120 $(this).tooltip('hide') | |
| 121 }) | |
| 122 .bind("hidden", function() { | |
| 123 ok(true, "hidden was called") | |
| 124 start() | |
| 125 }) | |
| 126 .tooltip('show') | |
| 127 }) | |
| 128 | |
| 129 test("should not fire hidden event when default prevented", function () { | |
| 130 stop() | |
| 131 var tooltip = $('<div title="tooltip title"></div>') | |
| 132 .bind("shown", function() { | |
| 133 $(this).tooltip('hide') | |
| 134 }) | |
| 135 .bind("hide", function(e) { | |
| 136 e.preventDefault() | |
| 137 ok(true, "hide was called") | |
| 138 start() | |
| 139 }) | |
| 140 .bind("hidden", function() { | |
| 141 ok(false, "hidden was called") | |
| 142 }) | |
| 143 .tooltip('show') | |
| 144 }) | |
| 145 | |
| 146 test("should not show tooltip if leave event occurs before delay expires", function () { | |
| 147 var tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"></a>') | |
| 148 .appendTo('#qunit-fixture') | |
| 149 .tooltip({ delay: 200 }) | |
| 150 | |
| 151 stop() | |
| 152 | |
| 153 tooltip.trigger('mouseenter') | |
| 154 | |
| 155 setTimeout(function () { | |
| 156 ok(!$(".tooltip").is('.fade.in'), 'tooltip is not faded in') | |
| 157 tooltip.trigger('mouseout') | |
| 158 setTimeout(function () { | |
| 159 ok(!$(".tooltip").is('.fade.in'), 'tooltip is not faded in') | |
| 160 start() | |
| 161 }, 200) | |
| 162 }, 100) | |
| 163 }) | |
| 164 | |
| 165 test("should not show tooltip if leave event occurs before delay expires, even if hide delay is 0", function () { | |
| 166 var tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"></a>') | |
| 167 .appendTo('#qunit-fixture') | |
| 168 .tooltip({ delay: { show: 200, hide: 0} }) | |
| 169 | |
| 170 stop() | |
| 171 | |
| 172 tooltip.trigger('mouseenter') | |
| 173 | |
| 174 setTimeout(function () { | |
| 175 ok(!$(".tooltip").is('.fade.in'), 'tooltip is not faded in') | |
| 176 tooltip.trigger('mouseout') | |
| 177 setTimeout(function () { | |
| 178 ok(!$(".tooltip").is('.fade.in'), 'tooltip is not faded in') | |
| 179 start() | |
| 180 }, 200) | |
| 181 }, 100) | |
| 182 }) | |
| 183 | |
| 184 test("should not show tooltip if leave event occurs before delay expires", function () { | |
| 185 var tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"></a>') | |
| 186 .appendTo('#qunit-fixture') | |
| 187 .tooltip({ delay: 100 }) | |
| 188 stop() | |
| 189 tooltip.trigger('mouseenter') | |
| 190 setTimeout(function () { | |
| 191 ok(!$(".tooltip").is('.fade.in'), 'tooltip is not faded in') | |
| 192 tooltip.trigger('mouseout') | |
| 193 setTimeout(function () { | |
| 194 ok(!$(".tooltip").is('.fade.in'), 'tooltip is not faded in') | |
| 195 start() | |
| 196 }, 100) | |
| 197 }, 50) | |
| 198 }) | |
| 199 | |
| 200 test("should show tooltip if leave event hasn't occured before delay expires", function () { | |
| 201 var tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"></a>') | |
| 202 .appendTo('#qunit-fixture') | |
| 203 .tooltip({ delay: 150 }) | |
| 204 stop() | |
| 205 tooltip.trigger('mouseenter') | |
| 206 setTimeout(function () { | |
| 207 ok(!$(".tooltip").is('.fade.in'), 'tooltip is not faded in') | |
| 208 }, 100) | |
| 209 setTimeout(function () { | |
| 210 ok($(".tooltip").is('.fade.in'), 'tooltip has faded in') | |
| 211 start() | |
| 212 }, 200) | |
| 213 }) | |
| 214 | |
| 215 test("should destroy tooltip", function () { | |
| 216 var tooltip = $('<div/>').tooltip().on('click.foo', function(){}) | |
| 217 ok(tooltip.data('tooltip'), 'tooltip has data') | |
| 218 ok($._data(tooltip[0], 'events').mouseover && $._data(tooltip[0], 'events').mouseout, 'tooltip has hover event') | |
| 219 ok($._data(tooltip[0], 'events').click[0].namespace == 'foo', 'tooltip has extra click.foo event') | |
| 220 tooltip.tooltip('show') | |
| 221 tooltip.tooltip('destroy') | |
| 222 ok(!tooltip.hasClass('in'), 'tooltip is hidden') | |
| 223 ok(!$._data(tooltip[0], 'tooltip'), 'tooltip does not have data') | |
| 224 ok($._data(tooltip[0], 'events').click[0].namespace == 'foo', 'tooltip still has click.foo') | |
| 225 ok(!$._data(tooltip[0], 'events').mouseover && !$._data(tooltip[0], 'events').mouseout, 'tooltip does not have any events') | |
| 226 }) | |
| 227 | |
| 228 test("should show tooltip with delegate selector on click", function () { | |
| 229 var div = $('<div><a href="#" rel="tooltip" title="Another tooltip"></a></div>') | |
| 230 var tooltip = div.appendTo('#qunit-fixture') | |
| 231 .tooltip({ selector: 'a[rel=tooltip]', | |
| 232 trigger: 'click' }) | |
| 233 div.find('a').trigger('click') | |
| 234 ok($(".tooltip").is('.fade.in'), 'tooltip is faded in') | |
| 235 }) | |
| 236 | |
| 237 test("should show tooltip when toggle is called", function () { | |
| 238 var tooltip = $('<a href="#" rel="tooltip" title="tooltip on toggle"></a>') | |
| 239 .appendTo('#qunit-fixture') | |
| 240 .tooltip({trigger: 'manual'}) | |
| 241 .tooltip('toggle') | |
| 242 ok($(".tooltip").is('.fade.in'), 'tooltip should be toggled in') | |
| 243 }) | |
| 244 | |
| 245 test("should place tooltips inside the body", function () { | |
| 246 var tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"></a>') | |
| 247 .appendTo('#qunit-fixture') | |
| 248 .tooltip({container:'body'}) | |
| 249 .tooltip('show') | |
| 250 ok($("body > .tooltip").length, 'inside the body') | |
| 251 ok(!$("#qunit-fixture > .tooltip").length, 'not found in parent') | |
| 252 tooltip.tooltip('hide') | |
| 253 }) | |
| 254 | |
| 255 test("should place tooltip inside window", function(){ | |
| 256 var container = $("<div />").appendTo("body") | |
| 257 .css({position: "absolute", width: 200, height: 200, bottom: 0, left: 0}) | |
| 258 , tooltip = $("<a href='#' title='Very very very very very very very very long tooltip'>Hover me</a>") | |
| 259 .css({position: "absolute", top:0, left: 0}) | |
| 260 .appendTo(container) | |
| 261 .tooltip({placement: "top", animate: false}) | |
| 262 .tooltip("show") | |
| 263 | |
| 264 stop() | |
| 265 | |
| 266 setTimeout(function(){ | |
| 267 ok($(".tooltip").offset().left >= 0) | |
| 268 | |
| 269 start() | |
| 270 container.remove() | |
| 271 }, 100) | |
| 272 }) | |
| 273 | |
| 274 test("should place tooltip on top of element", function(){ | |
| 275 var container = $("<div />").appendTo("body") | |
| 276 .css({position: "absolute", bottom: 0, left: 0, textAlign: "right", width: 300, height: 300}) | |
| 277 , p = $("<p style='margin-top:200px' />").appendTo(container) | |
| 278 , tooltiped = $("<a href='#' title='very very very very very very very long tooltip'>Hover me</a>") | |
| 279 .css({marginTop: 200}) | |
| 280 .appendTo(p) | |
| 281 .tooltip({placement: "top", animate: false}) | |
| 282 .tooltip("show") | |
| 283 | |
| 284 stop() | |
| 285 | |
| 286 setTimeout(function(){ | |
| 287 var tooltip = container.find(".tooltip") | |
| 288 | |
| 289 start() | |
| 290 ok(tooltip.offset().top + tooltip.outerHeight() <= tooltiped.offset().top) | |
| 291 container.remove() | |
| 292 }, 100) | |
| 293 }) | |
| 294 }) |
