1
- import { async , ComponentFixture , TestBed } from '@angular/core/testing' ;
1
+ import {
2
+ async , ComponentFixture , TestBed , tick , fakeAsync ,
3
+ flushMicrotasks
4
+ } from '@angular/core/testing' ;
2
5
import { Component , DebugElement } from '@angular/core' ;
3
6
import { By } from '@angular/platform-browser' ;
4
- import { TooltipPosition , MdTooltip } from './tooltip' ;
7
+ import { TooltipPosition , MdTooltip , TOOLTIP_HIDE_DELAY , MdTooltipModule } from './tooltip' ;
5
8
import { OverlayContainer } from '../core' ;
6
- import { MdTooltipModule } from './tooltip' ;
7
9
10
+ const initialTooltipMessage = 'initial tooltip message' ;
8
11
9
12
describe ( 'MdTooltip' , ( ) => {
10
13
let overlayContainerElement : HTMLElement ;
11
14
15
+
12
16
beforeEach ( async ( ( ) => {
13
17
TestBed . configureTestingModule ( {
14
18
imports : [ MdTooltipModule . forRoot ( ) ] ,
@@ -38,22 +42,83 @@ describe('MdTooltip', () => {
38
42
tooltipDirective = buttonDebugElement . injector . get ( MdTooltip ) ;
39
43
} ) ;
40
44
41
- it ( 'should show/hide on mouse enter/leave ' , ( ) => {
42
- expect ( tooltipDirective . visible ) . toBeFalsy ( ) ;
45
+ it ( 'should show and hide the tooltip ' , fakeAsync ( ( ) => {
46
+ expect ( tooltipDirective . _tooltipInstance ) . toBeUndefined ( ) ;
43
47
44
- tooltipDirective . _handleMouseEnter ( null ) ;
45
- expect ( tooltipDirective . visible ) . toBeTruthy ( ) ;
48
+ tooltipDirective . show ( ) ;
49
+ expect ( tooltipDirective . _isTooltipVisible ( ) ) . toBe ( true ) ;
46
50
47
51
fixture . detectChanges ( ) ;
48
- expect ( overlayContainerElement . textContent ) . toBe ( 'some message' ) ;
52
+ expect ( overlayContainerElement . textContent ) . toContain ( initialTooltipMessage ) ;
49
53
50
- tooltipDirective . _handleMouseLeave ( null ) ;
51
- expect ( overlayContainerElement . textContent ) . toBe ( '' ) ;
54
+ // After hide called, a timeout delay is created that will to hide the tooltip.
55
+ tooltipDirective . hide ( ) ;
56
+ expect ( tooltipDirective . _isTooltipVisible ( ) ) . toBe ( true ) ;
57
+
58
+ // After the tooltip delay elapses, expect that the tooltip is not visible.
59
+ tick ( TOOLTIP_HIDE_DELAY ) ;
60
+ fixture . detectChanges ( ) ;
61
+ expect ( tooltipDirective . _isTooltipVisible ( ) ) . toBe ( false ) ;
62
+
63
+ // On animation complete, should expect that the tooltip has been detached.
64
+ flushMicrotasks ( ) ;
65
+ expect ( tooltipDirective . _tooltipInstance ) . toBeNull ( ) ;
66
+ } ) ) ;
67
+
68
+ it ( 'should not follow through with hide if show is called after' , fakeAsync ( ( ) => {
69
+ tooltipDirective . show ( ) ;
70
+ expect ( tooltipDirective . _isTooltipVisible ( ) ) . toBe ( true ) ;
71
+
72
+ // After hide called, a timeout delay is created that will to hide the tooltip.
73
+ tooltipDirective . hide ( ) ;
74
+ expect ( tooltipDirective . _isTooltipVisible ( ) ) . toBe ( true ) ;
75
+
76
+ // Before delay time has passed, call show which should cancel intent to hide tooltip.
77
+ tooltipDirective . show ( ) ;
78
+ tick ( TOOLTIP_HIDE_DELAY ) ;
79
+ expect ( tooltipDirective . _isTooltipVisible ( ) ) . toBe ( true ) ;
80
+ } ) ) ;
81
+
82
+ it ( 'should remove the tooltip when changing position' , ( ) => {
83
+ const initialPosition : TooltipPosition = 'below' ;
84
+ const changedPosition : TooltipPosition = 'above' ;
85
+
86
+ expect ( tooltipDirective . _tooltipInstance ) . toBeUndefined ( ) ;
87
+
88
+ tooltipDirective . position = initialPosition ;
89
+ tooltipDirective . show ( ) ;
90
+ expect ( tooltipDirective . _tooltipInstance ) . toBeDefined ( ) ;
91
+
92
+ // Same position value should not remove the tooltip
93
+ tooltipDirective . position = initialPosition ;
94
+ expect ( tooltipDirective . _tooltipInstance ) . toBeDefined ( ) ;
95
+
96
+ // Different position value should destroy the tooltip
97
+ tooltipDirective . position = changedPosition ;
98
+ expect ( tooltipDirective . _tooltipInstance ) . toBeNull ( ) ;
99
+ expect ( tooltipDirective . _overlayRef ) . toBeNull ( ) ;
100
+ } ) ;
101
+
102
+ it ( 'should be able to modify the tooltip message' , ( ) => {
103
+ expect ( tooltipDirective . _tooltipInstance ) . toBeUndefined ( ) ;
104
+
105
+ tooltipDirective . show ( ) ;
106
+ expect ( tooltipDirective . _tooltipInstance . _visibility ) . toBe ( 'visible' ) ;
107
+
108
+ fixture . detectChanges ( ) ;
109
+ expect ( overlayContainerElement . textContent ) . toContain ( initialTooltipMessage ) ;
110
+
111
+ const newMessage = 'new tooltip message' ;
112
+ tooltipDirective . message = newMessage ;
113
+
114
+ fixture . detectChanges ( ) ;
115
+ expect ( overlayContainerElement . textContent ) . toContain ( newMessage ) ;
52
116
} ) ;
53
117
54
118
it ( 'should be removed after parent destroyed' , ( ) => {
55
- tooltipDirective . _handleMouseEnter ( null ) ;
56
- expect ( tooltipDirective . visible ) . toBeTruthy ( ) ;
119
+ tooltipDirective . show ( ) ;
120
+ expect ( tooltipDirective . _isTooltipVisible ( ) ) . toBe ( true ) ;
121
+
57
122
fixture . destroy ( ) ;
58
123
expect ( overlayContainerElement . childNodes . length ) . toBe ( 0 ) ;
59
124
expect ( overlayContainerElement . textContent ) . toBe ( '' ) ;
@@ -63,8 +128,9 @@ describe('MdTooltip', () => {
63
128
64
129
@Component ( {
65
130
selector : 'app' ,
66
- template : `<button md-tooltip="some message" [tooltip-position]="position">Button</button>`
131
+ template : `<button [ md-tooltip]=" message" [tooltip-position]="position">Button</button>`
67
132
} )
68
133
class BasicTooltipDemo {
69
134
position : TooltipPosition = 'below' ;
135
+ message : string = initialTooltipMessage ;
70
136
}
0 commit comments