@@ -6,7 +6,12 @@ import {
6
6
fakeAsync ,
7
7
flushMicrotasks
8
8
} from '@angular/core/testing' ;
9
- import { Component , DebugElement , AnimationTransitionEvent } from '@angular/core' ;
9
+ import {
10
+ Component ,
11
+ DebugElement ,
12
+ AnimationTransitionEvent ,
13
+ ChangeDetectionStrategy
14
+ } from '@angular/core' ;
10
15
import { By } from '@angular/platform-browser' ;
11
16
import { TooltipPosition , MdTooltip , MdTooltipModule } from './tooltip' ;
12
17
import { OverlayContainer } from '../core' ;
@@ -22,7 +27,7 @@ describe('MdTooltip', () => {
22
27
beforeEach ( async ( ( ) => {
23
28
TestBed . configureTestingModule ( {
24
29
imports : [ MdTooltipModule . forRoot ( ) , OverlayModule ] ,
25
- declarations : [ BasicTooltipDemo ] ,
30
+ declarations : [ BasicTooltipDemo , OnPushTooltipDemo ] ,
26
31
providers : [
27
32
{ provide : OverlayContainer , useFactory : ( ) => {
28
33
overlayContainerElement = document . createElement ( 'div' ) ;
@@ -59,6 +64,15 @@ describe('MdTooltip', () => {
59
64
expect ( tooltipDirective . _isTooltipVisible ( ) ) . toBe ( true ) ;
60
65
61
66
fixture . detectChanges ( ) ;
67
+
68
+ // wait till animation has finished
69
+ tick ( 500 ) ;
70
+
71
+ // Make sure tooltip is shown to the user and animation has finished
72
+ const tooltipElement = overlayContainerElement . querySelector ( '.md-tooltip' ) as HTMLElement ;
73
+ expect ( tooltipElement instanceof HTMLElement ) . toBe ( true ) ;
74
+ expect ( tooltipElement . style . transform ) . toBe ( 'scale(1)' ) ;
75
+
62
76
expect ( overlayContainerElement . textContent ) . toContain ( initialTooltipMessage ) ;
63
77
64
78
// After hide called, a timeout delay is created that will to hide the tooltip.
@@ -297,6 +311,53 @@ describe('MdTooltip', () => {
297
311
} ) . toThrowError ( 'Tooltip position "everywhere" is invalid.' ) ;
298
312
} ) ;
299
313
} ) ;
314
+
315
+ describe ( 'with OnPush' , ( ) => {
316
+ let fixture : ComponentFixture < OnPushTooltipDemo > ;
317
+ let buttonDebugElement : DebugElement ;
318
+ let buttonElement : HTMLButtonElement ;
319
+ let tooltipDirective : MdTooltip ;
320
+
321
+ beforeEach ( ( ) => {
322
+ fixture = TestBed . createComponent ( OnPushTooltipDemo ) ;
323
+ fixture . detectChanges ( ) ;
324
+ buttonDebugElement = fixture . debugElement . query ( By . css ( 'button' ) ) ;
325
+ buttonElement = < HTMLButtonElement > buttonDebugElement . nativeElement ;
326
+ tooltipDirective = buttonDebugElement . injector . get ( MdTooltip ) ;
327
+ } ) ;
328
+
329
+ it ( 'should show and hide the tooltip' , fakeAsync ( ( ) => {
330
+ expect ( tooltipDirective . _tooltipInstance ) . toBeUndefined ( ) ;
331
+
332
+ tooltipDirective . show ( ) ;
333
+ tick ( 0 ) ; // Tick for the show delay (default is 0)
334
+ expect ( tooltipDirective . _isTooltipVisible ( ) ) . toBe ( true ) ;
335
+
336
+ fixture . detectChanges ( ) ;
337
+
338
+ // wait till animation has finished
339
+ tick ( 500 ) ;
340
+
341
+ // Make sure tooltip is shown to the user and animation has finished
342
+ const tooltipElement = overlayContainerElement . querySelector ( '.md-tooltip' ) as HTMLElement ;
343
+ expect ( tooltipElement instanceof HTMLElement ) . toBe ( true ) ;
344
+ expect ( tooltipElement . style . transform ) . toBe ( 'scale(1)' ) ;
345
+
346
+ // After hide called, a timeout delay is created that will to hide the tooltip.
347
+ const tooltipDelay = 1000 ;
348
+ tooltipDirective . hide ( tooltipDelay ) ;
349
+ expect ( tooltipDirective . _isTooltipVisible ( ) ) . toBe ( true ) ;
350
+
351
+ // After the tooltip delay elapses, expect that the tooltip is not visible.
352
+ tick ( tooltipDelay ) ;
353
+ fixture . detectChanges ( ) ;
354
+ expect ( tooltipDirective . _isTooltipVisible ( ) ) . toBe ( false ) ;
355
+
356
+ // On animation complete, should expect that the tooltip has been detached.
357
+ flushMicrotasks ( ) ;
358
+ expect ( tooltipDirective . _tooltipInstance ) . toBeNull ( ) ;
359
+ } ) ) ;
360
+ } ) ;
300
361
} ) ;
301
362
302
363
@Component ( {
@@ -313,4 +374,17 @@ class BasicTooltipDemo {
313
374
message : string = initialTooltipMessage ;
314
375
showButton : boolean = true ;
315
376
}
377
+ @Component ( {
378
+ selector : 'app' ,
379
+ template : `
380
+ <button [mdTooltip]="message"
381
+ [mdTooltipPosition]="position">
382
+ Button
383
+ </button>` ,
384
+ changeDetection : ChangeDetectionStrategy . OnPush
385
+ } )
386
+ class OnPushTooltipDemo {
387
+ position : string = 'below' ;
388
+ message : string = initialTooltipMessage ;
389
+ }
316
390
0 commit comments