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+ } ) ;
0 commit comments