@@ -53,7 +53,7 @@ describe('MatTooltip', () => {
53
53
return { getContainerElement : ( ) => overlayContainerElement } ;
54
54
} } ,
55
55
{ provide : Directionality , useFactory : ( ) => {
56
- return dir = { value : 'ltr' } ;
56
+ return dir = { value : 'ltr' } ;
57
57
} }
58
58
]
59
59
} ) ;
@@ -80,7 +80,7 @@ describe('MatTooltip', () => {
80
80
} ) ;
81
81
82
82
it ( 'should show and hide the tooltip' , fakeAsync ( ( ) => {
83
- expect ( tooltipDirective . _tooltipInstance ) . toBeUndefined ( ) ;
83
+ assertTooltipInstance ( tooltipDirective , false ) ;
84
84
85
85
tooltipDirective . show ( ) ;
86
86
tick ( 0 ) ; // Tick for the show delay (default is 0)
@@ -110,7 +110,7 @@ describe('MatTooltip', () => {
110
110
111
111
// On animation complete, should expect that the tooltip has been detached.
112
112
flushMicrotasks ( ) ;
113
- expect ( tooltipDirective . _tooltipInstance ) . toBeNull ( ) ;
113
+ assertTooltipInstance ( tooltipDirective , false ) ;
114
114
} ) ) ;
115
115
116
116
it ( 'should be able to re-open a tooltip if it was closed by detaching the overlay' ,
@@ -126,15 +126,15 @@ describe('MatTooltip', () => {
126
126
fixture . detectChanges ( ) ;
127
127
expect ( tooltipDirective . _isTooltipVisible ( ) ) . toBe ( false ) ;
128
128
flushMicrotasks ( ) ;
129
- expect ( tooltipDirective . _tooltipInstance ) . toBeNull ( ) ;
129
+ assertTooltipInstance ( tooltipDirective , false ) ;
130
130
131
131
tooltipDirective . show ( ) ;
132
132
tick ( 0 ) ;
133
133
expect ( tooltipDirective . _isTooltipVisible ( ) ) . toBe ( true ) ;
134
134
} ) ) ;
135
135
136
136
it ( 'should show with delay' , fakeAsync ( ( ) => {
137
- expect ( tooltipDirective . _tooltipInstance ) . toBeUndefined ( ) ;
137
+ assertTooltipInstance ( tooltipDirective , false ) ;
138
138
139
139
const tooltipDelay = 1000 ;
140
140
tooltipDirective . show ( tooltipDelay ) ;
@@ -192,7 +192,7 @@ describe('MatTooltip', () => {
192
192
} ) ) ;
193
193
194
194
it ( 'should not show if hide is called before delay finishes' , async ( ( ) => {
195
- expect ( tooltipDirective . _tooltipInstance ) . toBeUndefined ( ) ;
195
+ assertTooltipInstance ( tooltipDirective , false ) ;
196
196
197
197
const tooltipDelay = 1000 ;
198
198
@@ -209,27 +209,27 @@ describe('MatTooltip', () => {
209
209
} ) ) ;
210
210
211
211
it ( 'should not show tooltip if message is not present or empty' , ( ) => {
212
- expect ( tooltipDirective . _tooltipInstance ) . toBeUndefined ( ) ;
212
+ assertTooltipInstance ( tooltipDirective , false ) ;
213
213
214
214
tooltipDirective . message = undefined ! ;
215
215
fixture . detectChanges ( ) ;
216
216
tooltipDirective . show ( ) ;
217
- expect ( tooltipDirective . _tooltipInstance ) . toBeUndefined ( ) ;
217
+ assertTooltipInstance ( tooltipDirective , false ) ;
218
218
219
219
tooltipDirective . message = null ! ;
220
220
fixture . detectChanges ( ) ;
221
221
tooltipDirective . show ( ) ;
222
- expect ( tooltipDirective . _tooltipInstance ) . toBeUndefined ( ) ;
222
+ assertTooltipInstance ( tooltipDirective , false ) ;
223
223
224
224
tooltipDirective . message = '' ;
225
225
fixture . detectChanges ( ) ;
226
226
tooltipDirective . show ( ) ;
227
- expect ( tooltipDirective . _tooltipInstance ) . toBeUndefined ( ) ;
227
+ assertTooltipInstance ( tooltipDirective , false ) ;
228
228
229
229
tooltipDirective . message = ' ' ;
230
230
fixture . detectChanges ( ) ;
231
231
tooltipDirective . show ( ) ;
232
- expect ( tooltipDirective . _tooltipInstance ) . toBeUndefined ( ) ;
232
+ assertTooltipInstance ( tooltipDirective , false ) ;
233
233
} ) ;
234
234
235
235
it ( 'should not follow through with hide if show is called after' , fakeAsync ( ( ) => {
@@ -252,7 +252,7 @@ describe('MatTooltip', () => {
252
252
const initialPosition : TooltipPosition = 'below' ;
253
253
const changedPosition : TooltipPosition = 'above' ;
254
254
255
- expect ( tooltipDirective . _tooltipInstance ) . toBeUndefined ( ) ;
255
+ assertTooltipInstance ( tooltipDirective , false ) ;
256
256
257
257
tooltipDirective . position = initialPosition ;
258
258
tooltipDirective . show ( ) ;
@@ -264,12 +264,12 @@ describe('MatTooltip', () => {
264
264
265
265
// Different position value should destroy the tooltip
266
266
tooltipDirective . position = changedPosition ;
267
- expect ( tooltipDirective . _tooltipInstance ) . toBeNull ( ) ;
267
+ assertTooltipInstance ( tooltipDirective , false ) ;
268
268
expect ( tooltipDirective . _overlayRef ) . toBeNull ( ) ;
269
269
} ) ;
270
270
271
271
it ( 'should be able to modify the tooltip message' , fakeAsync ( ( ) => {
272
- expect ( tooltipDirective . _tooltipInstance ) . toBeUndefined ( ) ;
272
+ assertTooltipInstance ( tooltipDirective , false ) ;
273
273
274
274
tooltipDirective . show ( ) ;
275
275
tick ( 0 ) ; // Tick for the show delay (default is 0)
@@ -286,7 +286,7 @@ describe('MatTooltip', () => {
286
286
} ) ) ;
287
287
288
288
it ( 'should allow extra classes to be set on the tooltip' , fakeAsync ( ( ) => {
289
- expect ( tooltipDirective . _tooltipInstance ) . toBeUndefined ( ) ;
289
+ assertTooltipInstance ( tooltipDirective , false ) ;
290
290
291
291
tooltipDirective . show ( ) ;
292
292
tick ( 0 ) ; // Tick for the show delay (default is 0)
@@ -510,7 +510,7 @@ describe('MatTooltip', () => {
510
510
} ) ) ;
511
511
512
512
it ( 'should not show the tooltip on progammatic focus' , fakeAsync ( ( ) => {
513
- expect ( tooltipDirective . _tooltipInstance ) . toBeUndefined ( ) ;
513
+ assertTooltipInstance ( tooltipDirective , false ) ;
514
514
515
515
buttonElement . focus ( ) ;
516
516
tick ( 0 ) ;
@@ -585,7 +585,7 @@ describe('MatTooltip', () => {
585
585
} ) ;
586
586
587
587
it ( 'should hide tooltip if clipped after changing positions' , fakeAsync ( ( ) => {
588
- expect ( tooltipDirective . _tooltipInstance ) . toBeUndefined ( ) ;
588
+ assertTooltipInstance ( tooltipDirective , false ) ;
589
589
590
590
// Show the tooltip and tick for the show delay (default is 0)
591
591
tooltipDirective . show ( ) ;
@@ -626,7 +626,7 @@ describe('MatTooltip', () => {
626
626
} ) ;
627
627
628
628
it ( 'should show and hide the tooltip' , fakeAsync ( ( ) => {
629
- expect ( tooltipDirective . _tooltipInstance ) . toBeUndefined ( ) ;
629
+ assertTooltipInstance ( tooltipDirective , false ) ;
630
630
631
631
tooltipDirective . show ( ) ;
632
632
tick ( 0 ) ; // Tick for the show delay (default is 0)
@@ -654,7 +654,7 @@ describe('MatTooltip', () => {
654
654
655
655
// On animation complete, should expect that the tooltip has been detached.
656
656
flushMicrotasks ( ) ;
657
- expect ( tooltipDirective . _tooltipInstance ) . toBeNull ( ) ;
657
+ assertTooltipInstance ( tooltipDirective , false ) ;
658
658
} ) ) ;
659
659
660
660
it ( 'should have rendered the tooltip text on init' , fakeAsync ( ( ) => {
@@ -751,3 +751,11 @@ class DynamicTooltipsDemo {
751
751
return this . _elementRef . nativeElement . querySelectorAll ( 'button' ) ;
752
752
}
753
753
}
754
+
755
+ /** Asserts whether a tooltip directive has a tooltip instance. */
756
+ function assertTooltipInstance ( tooltip : MatTooltip , shouldExist : boolean ) : void {
757
+ // Note that we have to cast this to a boolean, because Jasmine will go into an infinite loop
758
+ // if it tries to stringify the `_tooltipInstance` when an assertion fails. The infinite loop
759
+ // happens due to the `_tooltipInstance` having a circular structure.
760
+ expect ( ! ! tooltip . _tooltipInstance ) . toBe ( shouldExist ) ;
761
+ }
0 commit comments