Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit cf5c27a

Browse files
fix(tooltip): hide tooltips when content becomes empty
Closes #875
1 parent 0149eff commit cf5c27a

File tree

2 files changed

+101
-1
lines changed

2 files changed

+101
-1
lines changed

src/tooltip/test/tooltip2.spec.js

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
describe('tooltip directive', function () {
2+
3+
var $rootScope, $compile, $document, $timeout;
4+
5+
beforeEach(module('ui.bootstrap.tooltip'));
6+
beforeEach(module('template/tooltip/tooltip-popup.html'));
7+
beforeEach(inject(function (_$rootScope_, _$compile_, _$document_, _$timeout_) {
8+
$rootScope = _$rootScope_;
9+
$compile = _$compile_;
10+
$document = _$document_;
11+
$timeout = _$timeout_;
12+
}));
13+
14+
beforeEach(function(){
15+
this.addMatchers({
16+
toHaveOpenTooltips: function(noOfOpened) {
17+
var ttipElements = this.actual.find('div.tooltip');
18+
noOfOpened = noOfOpened || 1;
19+
20+
this.message = function() {
21+
return "Expected '" + angular.mock.dump(ttipElements) + "' to have '" + ttipElements.length + "' opened tooltips.";
22+
};
23+
24+
return ttipElements.length === noOfOpened;
25+
}
26+
});
27+
});
28+
29+
function compileTooltip(ttipMarkup) {
30+
var fragment = $compile('<div>'+ttipMarkup+'</div>')($rootScope);
31+
$rootScope.$digest();
32+
return fragment;
33+
}
34+
35+
function closeTooltip(hostEl, trigger, shouldNotFlush) {
36+
hostEl.trigger(trigger || 'mouseleave' );
37+
if (!shouldNotFlush) {
38+
$timeout.flush();
39+
}
40+
}
41+
42+
describe('basic scenarios with default options', function () {
43+
44+
it('shows default tooltip on mouse enter and closes on mouse leave', function () {
45+
var fragment = compileTooltip('<span tooltip="tooltip text">Trigger here</span>');
46+
47+
fragment.find('span').trigger( 'mouseenter' );
48+
expect(fragment).toHaveOpenTooltips();
49+
50+
closeTooltip(fragment.find('span'));
51+
expect(fragment).not.toHaveOpenTooltips();
52+
});
53+
54+
it('should not show a tooltip when its content is empty', function () {
55+
var fragment = compileTooltip('<span tooltip=""></span>');
56+
fragment.find('span').trigger( 'mouseenter' );
57+
expect(fragment).not.toHaveOpenTooltips();
58+
});
59+
60+
it('should not show a tooltip when its content becomes empty', function () {
61+
62+
$rootScope.content = 'some text';
63+
var fragment = compileTooltip('<span tooltip="{{ content }}"></span>');
64+
65+
fragment.find('span').trigger( 'mouseenter' );
66+
expect(fragment).toHaveOpenTooltips();
67+
68+
$rootScope.content = '';
69+
$rootScope.$digest();
70+
$timeout.flush();
71+
expect(fragment).not.toHaveOpenTooltips();
72+
});
73+
});
74+
75+
describe('option by option', function () {
76+
77+
describe('placement', function () {
78+
79+
it('can specify an alternative, valid placement', function () {
80+
var fragment = compileTooltip('<span tooltip="tooltip text" tooltip-placement="left">Trigger here</span>');
81+
fragment.find('span').trigger( 'mouseenter' );
82+
83+
var ttipElement = fragment.find('div.tooltip');
84+
expect(fragment).toHaveOpenTooltips();
85+
expect(ttipElement).toHaveClass('left');
86+
87+
closeTooltip(fragment.find('span'));
88+
expect(fragment).not.toHaveOpenTooltips();
89+
});
90+
91+
});
92+
93+
});
94+
});

src/tooltip/tooltip.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,13 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
242242
* Observe the relevant attributes.
243243
*/
244244
attrs.$observe( type, function ( val ) {
245-
scope.tt_content = val;
245+
if (val) {
246+
scope.tt_content = val;
247+
} else {
248+
if ( scope.tt_isOpen ) {
249+
hide();
250+
}
251+
}
246252
});
247253

248254
attrs.$observe( prefix+'Title', function ( val ) {

0 commit comments

Comments
 (0)