Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 9 additions & 1 deletion src/lib/tooltip/tooltip.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ describe('MdTooltip', () => {

fixture.detectChanges();

// wait till animation has finished
// wait until animation has finished
tick(500);

// Make sure tooltip is shown to the user and animation has finished
Expand All @@ -432,6 +432,14 @@ describe('MdTooltip', () => {
flushMicrotasks();
expect(tooltipDirective._tooltipInstance).toBeNull();
}));

it('should have rendered the tooltip text on init', fakeAsync(() => {
dispatchFakeEvent(buttonElement, 'mouseenter');
tick(0);

const tooltipElement = overlayContainerElement.querySelector('.mat-tooltip') as HTMLElement;
expect(tooltipElement.textContent).toContain('initial tooltip message');
}));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does there need to be a test component w/ OnPush that would have exhibited the original behavior?

Copy link
Member Author

@crisbeto crisbeto Mar 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

});

describe('destroy', () => {
Expand Down
21 changes: 16 additions & 5 deletions src/lib/tooltip/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
OnDestroy,
Renderer,
OnInit,
ChangeDetectorRef
ChangeDetectorRef,
} from '@angular/core';
import {
Overlay,
Expand Down Expand Up @@ -155,7 +155,7 @@ export class MdTooltip implements OnInit, OnDestroy {
@Optional() private _dir: Dir) {

// The mouse events shouldn't be bound on iOS devices, because
// they can prevent the first tap from firing it's click event.
// they can prevent the first tap from firing its click event.
if (!_platform.IOS) {
_renderer.listen(_elementRef.nativeElement, 'mouseenter', () => this.show());
_renderer.listen(_elementRef.nativeElement, 'mouseleave', () => this.hide());
Expand Down Expand Up @@ -311,6 +311,8 @@ export class MdTooltip implements OnInit, OnDestroy {
// Must wait for the message to be painted to the tooltip so that the overlay can properly
// calculate the correct positioning based on the size of the text.
this._tooltipInstance.message = message;
this._tooltipInstance._updateView();

this._ngZone.onMicrotaskEmpty.first().subscribe(() => {
if (this._tooltipInstance) {
this._overlayRef.updatePosition();
Expand Down Expand Up @@ -393,7 +395,7 @@ export class TooltipComponent {
// Mark for check so if any parent component has set the
// ChangeDetectionStrategy to OnPush it will be checked anyways
this._changeDetectorRef.markForCheck();
setTimeout(() => { this._closeOnInteraction = true; }, 0);
setTimeout(() => this._closeOnInteraction = true, 0);
}, delay);
}

Expand Down Expand Up @@ -439,8 +441,8 @@ export class TooltipComponent {
case 'after': this._transformOrigin = isLtr ? 'left' : 'right'; break;
case 'left': this._transformOrigin = 'right'; break;
case 'right': this._transformOrigin = 'left'; break;
case 'above': this._transformOrigin = 'bottom'; break;
case 'below': this._transformOrigin = 'top'; break;
case 'above': this._transformOrigin = 'bottom'; break;
case 'below': this._transformOrigin = 'top'; break;
default: throw new MdTooltipInvalidPositionError(value);
}
}
Expand All @@ -461,4 +463,13 @@ export class TooltipComponent {
this.hide(0);
}
}

/**
* Ensures that the tooltip text has rendered. Mainly used for rendering the initial text
* before positioning a tooltip, which can be problematic in components with OnPush change
* detection.
*/
_updateView(): void {
this._changeDetectorRef.detectChanges();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason why you use detectChanges instead of markForCheck?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I thought I tried it and it wasn't working. Looking at it again, it seems like it's the same. I'll change it.

}
}