tooltip.js 46.7 KB
Newer Older
fat's avatar
fat committed
1001
1002

    setTimeout(function () {
fat's avatar
fat committed
1003
      assert.ok(!$('.tooltip').is('.fade.in'), '100ms: tooltip not faded in')
fat's avatar
fat committed
1004
      $tooltip.trigger('mouseout')
1005
    }, 100)
fat's avatar
fat committed
1006
1007

    setTimeout(function () {
fat's avatar
fat committed
1008
      assert.ok(!$('.tooltip').is('.fade.in'), '250ms: tooltip not faded in')
1009
      done()
1010
    }, 250)
fat's avatar
fat committed
1011
1012
1013
1014

    $tooltip.trigger('mouseenter')
  })

fat's avatar
fat committed
1015
1016
  QUnit.test('should wait 200ms before hiding the tooltip', function (assert) {
    assert.expect(3)
1017
    var done = assert.async()
fat's avatar
fat committed
1018

fat's avatar
fat committed
1019
    var $tooltip = $('<a href="#" rel="tooltip" title="Another tooltip"/>')
fat's avatar
fat committed
1020
      .appendTo('#qunit-fixture')
1021
      .bootstrapTooltip({ delay: { show: 0, hide: 150 }})
fat's avatar
fat committed
1022
1023

    setTimeout(function () {
fat's avatar
fat committed
1024
      assert.ok($tooltip.data('bs.tooltip').$tip.is('.fade.in'), '1ms: tooltip faded in')
fat's avatar
fat committed
1025
1026
1027
1028

      $tooltip.trigger('mouseout')

      setTimeout(function () {
fat's avatar
fat committed
1029
        assert.ok($tooltip.data('bs.tooltip').$tip.is('.fade.in'), '100ms: tooltip still faded in')
1030
      }, 100)
fat's avatar
fat committed
1031
1032

      setTimeout(function () {
fat's avatar
fat committed
1033
1034
        assert.ok(!$tooltip.data('bs.tooltip').$tip.is('.in'), '200ms: tooltip removed')
        done()
1035
      }, 200)
fat's avatar
fat committed
1036
1037
1038
1039
1040
1041

    }, 0)

    $tooltip.trigger('mouseenter')
  })

fat's avatar
fat committed
1042
  QUnit.test('should correctly position tooltips on SVG elements', function (assert) {
1043
1044
    if (!window.SVGElement) {
      // Skip IE8 since it doesn't support SVG
fat's avatar
fat committed
1045
      assert.expect(0)
1046
1047
      return
    }
fat's avatar
fat committed
1048
    assert.expect(2)
1049

1050
    var done = assert.async()
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070

    var styles = '<style>'
        + '.tooltip, .tooltip *, .tooltip *:before, .tooltip *:after { box-sizing: border-box; }'
        + '.tooltip { position: absolute; }'
        + '.tooltip .tooltip-inner { width: 24px; height: 24px; font-family: Helvetica; }'
        + '</style>'
    var $styles = $(styles).appendTo('head')

    $('#qunit-fixture').append(
        '<div style="position: fixed; top: 0; left: 0;">'
      + '  <svg width="200" height="200">'
      + '    <circle cx="100" cy="100" r="10" title="m" id="theCircle" />'
      + '  </svg>'
      + '</div>')
    var $circle = $('#theCircle')

    $circle
      .on('shown.bs.tooltip', function () {
        var offset = $('.tooltip').offset()
        $styles.remove()
fat's avatar
fat committed
1071
        assert.ok(Math.abs(offset.left - 88) <= 1, 'tooltip has correct horizontal location')
1072
        $circle.bootstrapTooltip('hide')
fat's avatar
fat committed
1073
        assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
1074
        done()
1075
1076
1077
1078
1079
1080
      })
      .bootstrapTooltip({ container: 'body', placement: 'top', trigger: 'manual' })

    $circle.bootstrapTooltip('show')
  })

fat's avatar
fat committed
1081
1082
  QUnit.test('should correctly determine auto placement based on container rather than parent', function (assert) {
    assert.expect(2)
1083
    var done = assert.async()
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104

    var styles = '<style>'
        + '.tooltip, .tooltip *, .tooltip *:before, .tooltip *:after { box-sizing: border-box; }'
        + '.tooltip { position: absolute; display: block; font-size: 12px; line-height: 1.4; }'
        + '.tooltip .tooltip-inner { max-width: 200px; padding: 3px 8px; font-family: Helvetica; text-align: center; }'
        + '#trigger-parent {'
        + '  position: fixed;'
        + '  top: 100px;'
        + '  right: 17px;'
        + '}'
        + '</style>'
    var $styles = $(styles).appendTo('head')

    $('#qunit-fixture').append('<span id="trigger-parent"><a id="tt-trigger" title="If a_larger_text is written here, it won\'t fit using older broken version of BS">HOVER OVER ME</a></span>')
    var $trigger = $('#tt-trigger')

    $trigger
      .on('shown.bs.tooltip', function () {
        var $tip = $('.tooltip-inner')
        var tipXrightEdge = $tip.offset().left + $tip.width()
        var triggerXleftEdge = $trigger.offset().left
fat's avatar
fat committed
1105
        assert.ok(tipXrightEdge < triggerXleftEdge, 'tooltip with auto left placement, when near the right edge of the viewport, gets left placement')
1106
1107
1108
1109
1110
        $trigger.bootstrapTooltip('hide')
      })
      .on('hidden.bs.tooltip', function () {
        $styles.remove()
        $(this).remove()
fat's avatar
fat committed
1111
        assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
1112
        done()
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
      })
      .bootstrapTooltip({
        container: 'body',
        placement: 'auto left',
        trigger: 'manual'
      })

    $trigger.bootstrapTooltip('show')
  })

fat's avatar
fat committed
1123
1124
  QUnit.test('should not reload the tooltip on subsequent mouseenter events', function (assert) {
    assert.expect(1)
1125
    var titleHtml = function () {
fat's avatar
fat committed
1126
      var uid = $.fn.bootstrapTooltip.Constructor.prototype.getUID('tooltip')
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
      return '<p id="tt-content">' + uid + '</p><p>' + uid + '</p><p>' + uid + '</p>'
    }

    var $tooltip = $('<span id="tt-outer" rel="tooltip" data-trigger="hover" data-placement="top">some text</span>')
      .appendTo('#qunit-fixture')

    $tooltip.bootstrapTooltip({
      html: true,
      animation: false,
      trigger: 'hover',
      delay: { show: 0, hide: 500 },
      container: $tooltip,
      title: titleHtml
    })

    $('#tt-outer').trigger('mouseenter')

    var currentUid = $('#tt-content').text()

    $('#tt-content').trigger('mouseenter')
fat's avatar
fat committed
1147
    assert.strictEqual(currentUid, $('#tt-content').text())
1148
1149
  })

fat's avatar
fat committed
1150
1151
  QUnit.test('should not reload the tooltip if the mouse leaves and re-enters before hiding', function (assert) {
    assert.expect(4)
1152
    var titleHtml = function () {
fat's avatar
fat committed
1153
      var uid = $.fn.bootstrapTooltip.Constructor.prototype.getUID('tooltip')
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
      return '<p id="tt-content">' + uid + '</p><p>' + uid + '</p><p>' + uid + '</p>'
    }

    var $tooltip = $('<span id="tt-outer" rel="tooltip" data-trigger="hover" data-placement="top">some text</span>')
      .appendTo('#qunit-fixture')

    $tooltip.bootstrapTooltip({
      html: true,
      animation: false,
      trigger: 'hover',
      delay: { show: 0, hide: 500 },
      container: $tooltip,
      title: titleHtml
    })

    var obj = $tooltip.data('bs.tooltip')

    $('#tt-outer').trigger('mouseenter')

    var currentUid = $('#tt-content').text()

    $('#tt-outer').trigger('mouseleave')
fat's avatar
fat committed
1176
    assert.strictEqual(currentUid, $('#tt-content').text())
fat's avatar
fat committed
1177

fat's avatar
fat committed
1178
    assert.ok(obj.hoverState == 'out', 'the tooltip hoverState should be set to "out"')
1179
1180

    $('#tt-content').trigger('mouseenter')
fat's avatar
fat committed
1181
    assert.ok(obj.hoverState == 'in', 'the tooltip hoverState should be set to "in"')
1182

fat's avatar
fat committed
1183
    assert.strictEqual(currentUid, $('#tt-content').text())
1184
1185
  })

fat's avatar
fat committed
1186
1187
  QUnit.test('should position arrow correctly when tooltip is moved to not appear offscreen', function (assert) {
    assert.expect(2)
1188
    var done = assert.async()
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200

    var styles = '<style>'
        + '.tooltip, .tooltip *, .tooltip *:before, .tooltip *:after { box-sizing: border-box; }'
        + '.tooltip { position: absolute; }'
        + '.tooltip-arrow { position: absolute; width: 0; height: 0; }'
        + '.tooltip .tooltip-inner { max-width: 200px; padding: 3px 8px; }'
        + '</style>'
    var $styles = $(styles).appendTo('head')

    $('<a href="#" title="tooltip title" style="position: absolute; bottom: 0; right: 0;">Foobar</a>')
      .appendTo('body')
      .on('shown.bs.tooltip', function () {
fat's avatar
fat committed
1201
1202
        var arrowStyles = $(this).data('bs.tooltip').$tip.find('.tooltip-arrow').attr('style')
        assert.ok(/left/i.test(arrowStyles) && !/top/i.test(arrowStyles), 'arrow positioned correctly')
1203
1204
1205
1206
1207
        $(this).bootstrapTooltip('hide')
      })
      .on('hidden.bs.tooltip', function () {
        $styles.remove()
        $(this).remove()
fat's avatar
fat committed
1208
        assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom')
1209
        done()
1210
1211
1212
1213
1214
1215
1216
1217
1218
      })
      .bootstrapTooltip({
        container: 'body',
        placement: 'top',
        trigger: 'manual'
      })
      .bootstrapTooltip('show')
  })

fat's avatar
fat committed
1219
  QUnit.test('should correctly position tooltips on transformed elements', function (assert) {
1220
1221
    var styleProps = document.documentElement.style
    if (!('transform' in styleProps) && !('webkitTransform' in styleProps) && !('msTransform' in styleProps)) {
fat's avatar
fat committed
1222
      assert.expect(0)
1223
1224
      return
    }
fat's avatar
fat committed
1225
    assert.expect(2)
1226

1227
    var done = assert.async()
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237

    var styles = '<style>'
        + '#qunit-fixture { top: 0; left: 0; }'
        + '.tooltip, .tooltip *, .tooltip *:before, .tooltip *:after { box-sizing: border-box; }'
        + '.tooltip { position: absolute; }'
        + '.tooltip .tooltip-inner { width: 24px; height: 24px; font-family: Helvetica; }'
        + '#target { position: absolute; top: 100px; left: 50px; width: 100px; height: 200px; -webkit-transform: rotate(270deg); -ms-transform: rotate(270deg); transform: rotate(270deg); }'
        + '</style>'
    var $styles = $(styles).appendTo('head')

fat's avatar
fat committed
1238
    var $element = $('<div id="target" title="1"/>').appendTo('#qunit-fixture')
1239
1240
1241
1242
1243

    $element
      .on('shown.bs.tooltip', function () {
        var offset = $('.tooltip').offset()
        $styles.remove()
fat's avatar
fat committed
1244
1245
        assert.ok(Math.abs(offset.left - 88) <= 1, 'tooltip has correct horizontal location')
        assert.ok(Math.abs(offset.top - 126) <= 1, 'tooltip has correct vertical location')
1246
        $element.bootstrapTooltip('hide')
1247
        done()
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
      })
      .bootstrapTooltip({
        container: 'body',
        placement: 'top',
        trigger: 'manual'
      })

    $element.bootstrapTooltip('show')
  })

fat's avatar
fat committed
1258
1259
1260
1261
1262
1263
1264
1265
  QUnit.test('should throw an error when initializing tooltip on the document object without specifying a delegation selector', function (assert) {
    assert.expect(1)
    assert.throws(function () {
      $(document).bootstrapTooltip({ title: 'What am I on?' })
    }, new Error('`selector` option must be specified when initializing tooltip on the window.document object!'))
  })

  QUnit.test('should do nothing when an attempt is made to hide an uninitialized tooltip', function (assert) {
1266
1267
    assert.expect(1)

fat's avatar
fat committed
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
    var $tooltip = $('<span data-toggle="tooltip" title="some tip">some text</span>')
      .appendTo('#qunit-fixture')
      .on('hidden.bs.tooltip shown.bs.tooltip', function () {
        assert.ok(false, 'should not fire any tooltip events')
      })
      .bootstrapTooltip('hide')
    assert.strictEqual($tooltip.data('bs.tooltip'), undefined, 'should not initialize the tooltip')
  })

  QUnit.test('should throw an error when template contains multiple top-level elements', function (assert) {
    assert.expect(1)
1279
    assert.throws(function () {
fat's avatar
fat committed
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
      $('<a href="#" data-toggle="tooltip" title="Another tooltip"></a>')
        .appendTo('#qunit-fixture')
        .bootstrapTooltip({ template: '<div>Foo</div><div>Bar</div>' })
        .bootstrapTooltip('show')
    }, new Error('tooltip `template` option must consist of exactly 1 top-level element!'))
  })

  QUnit.test('should not remove tooltip if multiple triggers are set and one is still active', function (assert) {
    assert.expect(41)
    var $el = $('<button>Trigger</button>')
      .appendTo('#qunit-fixture')
      .bootstrapTooltip({ trigger: 'click hover focus', animation: false })
    var tooltip = $el.data('bs.tooltip')
    var $tooltip = tooltip.tip()

    function showingTooltip() { return $tooltip.hasClass('in') || tooltip.hoverState == 'in' }

    var tests = [
        ['mouseenter', 'mouseleave'],
1299

fat's avatar
fat committed
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
        ['focusin', 'focusout'],

        ['click', 'click'],

        ['mouseenter', 'focusin', 'focusout', 'mouseleave'],
        ['mouseenter', 'focusin', 'mouseleave', 'focusout'],

        ['focusin', 'mouseenter', 'mouseleave', 'focusout'],
        ['focusin', 'mouseenter', 'focusout', 'mouseleave'],

        ['click', 'focusin', 'mouseenter', 'focusout', 'mouseleave', 'click'],
        ['mouseenter', 'click', 'focusin', 'focusout', 'mouseleave', 'click'],
        ['mouseenter', 'focusin', 'click', 'click', 'mouseleave', 'focusout']
    ]

    assert.ok(!showingTooltip())

    $.each(tests, function (idx, triggers) {
      for (var i = 0, len = triggers.length; i < len; i++) {
        $el.trigger(triggers[i]);
        assert.equal(i < (len - 1), showingTooltip())
      }
    })
1323
1324
  })

1325
})
For faster browsing, not all history is shown. View entire blame