From b14455b03a977cbbf99112d39b55461785666a3f Mon Sep 17 00:00:00 2001
From: Jacob Thornton <jacobthornton@gmail.com>
Date: Sat, 14 Apr 2012 23:10:03 -0700
Subject: [PATCH] add tests for mouseout delay in tooltip

---
 js/bootstrap-tooltip.js            | 36 +++++++++------------
 js/tests/unit/bootstrap-tooltip.js | 50 ++++++++++++++++++++++++++++++
 2 files changed, 64 insertions(+), 22 deletions(-)

diff --git a/js/bootstrap-tooltip.js b/js/bootstrap-tooltip.js
index 454e7a4484..af2e589687 100644
--- a/js/bootstrap-tooltip.js
+++ b/js/bootstrap-tooltip.js
@@ -72,33 +72,25 @@
   , enter: function (e) {
       var self = $(e.currentTarget)[this.type](this._options).data(this.type)
 
-      if (!self.options.delay || !self.options.delay.show) {
-        self.show()
-      } else {
-        clearTimeout(this.timeout)
-        self.hoverState = 'in'
-        this.timeout = setTimeout(function() {
-          if (self.hoverState == 'in') {
-            self.show()
-          }
-        }, self.options.delay.show)
-      }
+      if (!self.options.delay || !self.options.delay.show) return self.show()
+
+      clearTimeout(this.timeout)
+      self.hoverState = 'in'
+      this.timeout = setTimeout(function() {
+        if (self.hoverState == 'in') self.show()
+      }, self.options.delay.show)
     }
 
   , leave: function (e) {
       var self = $(e.currentTarget)[this.type](this._options).data(this.type)
 
-      if (!self.options.delay || !self.options.delay.hide) {
-        self.hide()
-      } else {
-        clearTimeout(this.timeout)
-        self.hoverState = 'out'
-        this.timeout = setTimeout(function() {
-          if (self.hoverState == 'out') {
-            self.hide()
-          }
-        }, self.options.delay.hide)
-      }
+      if (!self.options.delay || !self.options.delay.hide) return self.hide()
+
+      clearTimeout(this.timeout)
+      self.hoverState = 'out'
+      this.timeout = setTimeout(function() {
+        if (self.hoverState == 'out') self.hide()
+      }, self.options.delay.hide)
     }
 
   , show: function () {
diff --git a/js/tests/unit/bootstrap-tooltip.js b/js/tests/unit/bootstrap-tooltip.js
index f6e00089b1..63f4f0b07c 100644
--- a/js/tests/unit/bootstrap-tooltip.js
+++ b/js/tests/unit/bootstrap-tooltip.js
@@ -59,6 +59,56 @@ $(function () {
         ok(!$(".tooltip").length, 'tooltip removed')
       })
 
+      test("should not show tooltip if leave event occurs before delay expires", function () {
+        var tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"></a>')
+          .appendTo('#qunit-fixture')
+          .tooltip({ delay: 200 })
+
+        stop()
+
+        tooltip.trigger('mouseenter')
+
+        setTimeout(function () {
+          ok(!$(".tooltip").hasClass('fade in'), 'tooltip is not faded in')
+          tooltip.trigger('mouseout')
+          setTimeout(function () {
+            ok(!$(".tooltip").hasClass('fade in'), 'tooltip is not faded in')
+            start()
+          }, 200)
+        }, 100)
+      })
+
+      test("should not show tooltip if leave event occurs before delay expires", function () {
+        var tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"></a>')
+          .appendTo('#qunit-fixture')
+          .tooltip({ delay: 100 })
+        stop()
+        tooltip.trigger('mouseenter')
+        setTimeout(function () {
+          ok(!$(".tooltip").hasClass('fade in'), 'tooltip is not faded in')
+          tooltip.trigger('mouseout')
+          setTimeout(function () {
+            ok(!$(".tooltip").hasClass('fade in'), 'tooltip is not faded in')
+            start()
+          }, 100)
+        }, 50)
+      })
+
+      test("should show tooltip if leave event hasn't occured before delay expires", function () {
+        var tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"></a>')
+          .appendTo('#qunit-fixture')
+          .tooltip({ delay: 200 })
+        stop()
+        tooltip.trigger('mouseenter')
+        setTimeout(function () {
+          ok(!$(".tooltip").hasClass('fade in'), 'tooltip is not faded in')
+          setTimeout(function () {
+            ok(!$(".tooltip").hasClass('fade in'), 'tooltip has faded in')
+            start()
+          }, 200)
+        }, 100)
+      })
+
       test("should detect if title string is html or text: foo", function () {
         ok(!$.fn.tooltip.Constructor.prototype.isHTML('foo'), 'correctly detected html')
       })
-- 
GitLab