-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
@@ -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'); | ||
|
@@ -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. | ||
|
@@ -76,6 +90,43 @@ describe('MdTooltip', () => { | |
expect(tooltipDirective._tooltipInstance).toBeNull(); | ||
})); | ||
|
||
it('should show and hide the tooltip if changeDetection is OnPush', fakeAsync(() => { | ||
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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer the |
||
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(); | ||
|
||
|
@@ -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; | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 thebeforeEach
.