Skip to content

fix(tooltip): not working properly with ChangeDetectionStrategy.OnPush #2721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 31, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
3 changes: 2 additions & 1 deletion src/demo-app/tooltip/tooltip-demo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Component} from '@angular/core';
import {Component, ChangeDetectionStrategy} from '@angular/core';
import {TooltipPosition} from '@angular/material';


Expand All @@ -7,6 +7,7 @@ import {TooltipPosition} from '@angular/material';
selector: 'tooltip-demo',
templateUrl: 'tooltip-demo.html',
styleUrls: ['tooltip-demo.css'],
changeDetection: ChangeDetectionStrategy.OnPush // make sure tooltip also works OnPush
})
export class TooltipDemo {
position: TooltipPosition = 'below';
Expand Down
68 changes: 66 additions & 2 deletions src/lib/tooltip/tooltip.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import {
fakeAsync,
flushMicrotasks
} from '@angular/core/testing';
import {Component, DebugElement, AnimationTransitionEvent} from '@angular/core';
import {
Component,
DebugElement,
AnimationTransitionEvent,
ChangeDetectionStrategy
} from '@angular/core';
import {By} from '@angular/platform-browser';
import {TooltipPosition, MdTooltip, MdTooltipModule} from './tooltip';
import {OverlayContainer} from '../core';
Expand All @@ -22,7 +27,7 @@ describe('MdTooltip', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [MdTooltipModule.forRoot(), OverlayModule],
declarations: [BasicTooltipDemo],
declarations: [BasicTooltipDemo, OnPushTooltipDemo],
providers: [
{provide: OverlayContainer, useFactory: () => {
overlayContainerElement = document.createElement('div');
Expand Down Expand Up @@ -59,6 +64,15 @@ describe('MdTooltip', () => {
expect(tooltipDirective._isTooltipVisible()).toBe(true);

fixture.detectChanges();

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

// Make sure tooltip is shown to the user and animation has finished
const tooltipElement = <HTMLElement>overlayContainerElement.querySelector('.md-tooltip');
expect(tooltipElement instanceof HTMLElement).toBe(true);
expect(tooltipElement.style.transform).toBe('scale(1)');

expect(overlayContainerElement.textContent).toContain(initialTooltipMessage);

// After hide called, a timeout delay is created that will to hide the tooltip.
Expand All @@ -76,6 +90,43 @@ describe('MdTooltip', () => {
expect(tooltipDirective._tooltipInstance).toBeNull();
}));

it('should show and hide the tooltip if changeDetection is OnPush', fakeAsync(() => {
Copy link
Member

Choose a reason for hiding this comment

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

Can you move this test to a separate describe block called "with OnPush" as a sibling to the existing "basic usage" block? Then you can do the createComponent in the beforeEach.

expect(tooltipDirective._tooltipInstance).toBeUndefined();

const fixtureOnPush = TestBed.createComponent(OnPushTooltipDemo);
fixtureOnPush.detectChanges();
const buttonDebugElementOnPush = fixtureOnPush.debugElement.query(By.css('button'));
const tooltipDirectiveOnPush = buttonDebugElementOnPush.injector.get(MdTooltip);

tooltipDirectiveOnPush.show();
tick(0); // Tick for the show delay (default is 0)
expect(tooltipDirectiveOnPush._isTooltipVisible()).toBe(true);

fixtureOnPush.detectChanges();

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

// Make sure tooltip is shown to the user and animation has finished
const tooltipElement = <HTMLElement>overlayContainerElement.querySelector('.md-tooltip');
Copy link
Member

Choose a reason for hiding this comment

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

Prefer the as HTMLElement syntax

expect(tooltipElement instanceof HTMLElement).toBe(true);
expect(tooltipElement.style.transform).toBe('scale(1)');

// After hide called, a timeout delay is created that will to hide the tooltip.
const tooltipDelay = 1000;
tooltipDirectiveOnPush.hide(tooltipDelay);
expect(tooltipDirectiveOnPush._isTooltipVisible()).toBe(true);

// After the tooltip delay elapses, expect that the tooltip is not visible.
tick(tooltipDelay);
fixtureOnPush.detectChanges();
expect(tooltipDirectiveOnPush._isTooltipVisible()).toBe(false);

// On animation complete, should expect that the tooltip has been detached.
flushMicrotasks();
expect(tooltipDirectiveOnPush._tooltipInstance).toBeNull();
}));

it('should show with delay', fakeAsync(() => {
expect(tooltipDirective._tooltipInstance).toBeUndefined();

Expand Down Expand Up @@ -313,4 +364,17 @@ class BasicTooltipDemo {
message: string = initialTooltipMessage;
showButton: boolean = true;
}
@Component({
selector: 'app',
template: `
<button [mdTooltip]="message"
[mdTooltipPosition]="position">
Button
</button>`,
changeDetection: ChangeDetectionStrategy.OnPush
})
class OnPushTooltipDemo {
position: string = 'below';
message: string = initialTooltipMessage;
}

13 changes: 11 additions & 2 deletions src/lib/tooltip/tooltip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
AnimationTransitionEvent,
NgZone,
Optional,
OnDestroy
OnDestroy,
ChangeDetectorRef
} from '@angular/core';
import {
Overlay,
Expand Down Expand Up @@ -286,7 +287,7 @@ export class TooltipComponent {
/** Subject for notifying that the tooltip has been hidden from the view */
private _onHide: Subject<any> = new Subject();

constructor(@Optional() private _dir: Dir) {}
constructor(@Optional() private _dir: Dir, private _changeDetectorRef: ChangeDetectorRef) {}

/**
* Shows the tooltip with an animation originating from the provided origin
Expand All @@ -309,6 +310,10 @@ export class TooltipComponent {
// If this was set to true immediately, then a body click that triggers show() would
// trigger interaction and close the tooltip right after it was displayed.
this._closeOnInteraction = false;

// 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);
}, delay);
}
Expand All @@ -326,6 +331,10 @@ export class TooltipComponent {
this._hideTimeoutId = setTimeout(() => {
this._visibility = 'hidden';
this._closeOnInteraction = false;

// Mark for check so if any parent component has set the
// ChangeDetectionStrategy to OnPush it will be checked anyways
this._changeDetectorRef.markForCheck();
}, delay);
}

Expand Down