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

fix(tooltip): hide tooltips when content becomes empty and test changes #1058

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions src/tooltip/test/tooltip2.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
describe('tooltip directive', function () {

var $rootScope, $compile, $document, $timeout;

beforeEach(module('ui.bootstrap.tooltip'));
beforeEach(module('template/tooltip/tooltip-popup.html'));
beforeEach(inject(function (_$rootScope_, _$compile_, _$document_, _$timeout_) {
$rootScope = _$rootScope_;
$compile = _$compile_;
$document = _$document_;
$timeout = _$timeout_;
}));

beforeEach(function(){
this.addMatchers({
toHaveOpenTooltips: function(noOfOpened) {
var ttipElements = this.actual.find('div.tooltip');
noOfOpened = noOfOpened || 1;

this.message = function() {
return "Expected '" + angular.mock.dump(ttipElements) + "' to have '" + ttipElements.length + "' opened tooltips.";
};

return ttipElements.length === noOfOpened;
}
});
});

function compileTooltip(ttipMarkup) {
var fragment = $compile('<div>'+ttipMarkup+'</div>')($rootScope);
$rootScope.$digest();
return fragment;
}

function closeTooltip(hostEl, trigger, shouldNotFlush) {
hostEl.trigger(trigger || 'mouseleave' );
if (!shouldNotFlush) {
$timeout.flush();
}
}

describe('basic scenarios with default options', function () {

it('shows default tooltip on mouse enter and closes on mouse leave', function () {
var fragment = compileTooltip('<span tooltip="tooltip text">Trigger here</span>');

fragment.find('span').trigger( 'mouseenter' );
expect(fragment).toHaveOpenTooltips();

closeTooltip(fragment.find('span'));
expect(fragment).not.toHaveOpenTooltips();
});

it('should not show a tooltip when its content is empty', function () {
var fragment = compileTooltip('<span tooltip=""></span>');
fragment.find('span').trigger( 'mouseenter' );
expect(fragment).not.toHaveOpenTooltips();
});

it('should not show a tooltip when its content becomes empty', function () {

$rootScope.content = 'some text';
var fragment = compileTooltip('<span tooltip="{{ content }}"></span>');

fragment.find('span').trigger( 'mouseenter' );
expect(fragment).toHaveOpenTooltips();

$rootScope.content = '';
$rootScope.$digest();
$timeout.flush();
expect(fragment).not.toHaveOpenTooltips();
});
});

describe('option by option', function () {

describe('placement', function () {

it('can specify an alternative, valid placement', function () {
var fragment = compileTooltip('<span tooltip="tooltip text" tooltip-placement="left">Trigger here</span>');
fragment.find('span').trigger( 'mouseenter' );

var ttipElement = fragment.find('div.tooltip');
expect(fragment).toHaveOpenTooltips();
expect(ttipElement).toHaveClass('left');

closeTooltip(fragment.find('span'));
expect(fragment).not.toHaveOpenTooltips();
});

});

});
});
8 changes: 7 additions & 1 deletion src/tooltip/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,13 @@ angular.module( 'ui.bootstrap.tooltip', [ 'ui.bootstrap.position', 'ui.bootstrap
* Observe the relevant attributes.
*/
attrs.$observe( type, function ( val ) {
scope.tt_content = val;
if (val) {
scope.tt_content = val;
} else {
if ( scope.tt_isOpen ) {
hide();
}
}
});

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