Skip to content

Commit

Permalink
feat(tooltip): init add animations
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewseguin committed Oct 28, 2016
1 parent 04e2201 commit b4d733a
Show file tree
Hide file tree
Showing 5 changed files with 267 additions and 133 deletions.
24 changes: 21 additions & 3 deletions src/demo-app/tooltip/tooltip-demo.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<div class="demo-tooltip">
<h1>Tooltip Demo</h1>

<p class="centered">
<button
<button #tooltip="mdTooltip"
md-raised-button
color="primary"
md-tooltip="to see a tooltip"
[md-tooltip]="message"
[tooltip-position]="position">
Mouse over this link
Mouse over to see the tooltip
</button>
</p>

<p>
<md-radio-group [(ngModel)]="position">
<md-radio-button value="below">Below</md-radio-button>
Expand All @@ -17,4 +19,20 @@ <h1>Tooltip Demo</h1>
<md-radio-button value="after">After</md-radio-button>
</md-radio-group>
</p>

<p>
<strong>Message: </strong>
<md-input type="text" [(ngModel)]="message"></md-input>
</p>

<strong>Mouse over to</strong>
<button md-raised-button color="primary" (mouseenter)="tooltip.show()">
Show tooltip
</button>
<button md-raised-button color="primary" (mouseenter)="tooltip.hide(0)">
Hide tooltip
</button>
<button md-raised-button color="primary" (mouseenter)="tooltip.toggle()">
Toggle tooltip
</button>
</div>
1 change: 1 addition & 0 deletions src/demo-app/tooltip/tooltip-demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ import {TooltipPosition} from '@angular/material';
})
export class TooltipDemo {
position: TooltipPosition = 'below';
message: string = 'Here is the tooltip';
}
6 changes: 6 additions & 0 deletions src/lib/tooltip/tooltip.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div class="md-tooltip"
[style.transform-origin]="_transformOrigin"
[@state]="_visibility"
(@state.done)="this._afterVisibilityAnimation($event)">
{{message}}
</div>
65 changes: 54 additions & 11 deletions src/lib/tooltip/tooltip.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
import {async, ComponentFixture, TestBed, tick, fakeAsync} from '@angular/core/testing';
import {Component, DebugElement} from '@angular/core';
import {By} from '@angular/platform-browser';
import {TooltipPosition, MdTooltip} from './tooltip';
import {TooltipPosition, MdTooltip, MATERIAL_TOOLTIP_HIDE_DELAY} from './tooltip';
import {OverlayContainer} from '../core';
import {MdTooltipModule} from './tooltip';

const initialTooltipMessage = 'initial tooltip message';

describe('MdTooltip', () => {
fdescribe('MdTooltip', () => {
let overlayContainerElement: HTMLElement;


beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [MdTooltipModule.forRoot()],
Expand Down Expand Up @@ -38,25 +40,66 @@ describe('MdTooltip', () => {
tooltipDirective = buttonDebugElement.injector.get(MdTooltip);
});

it('should show/hide on mouse enter/leave', () => {
expect(tooltipDirective.visible).toBeFalsy();
it('should show and hide the tooltip', fakeAsync(() => {
expect(tooltipDirective._tooltipRef).toBeUndefined();

tooltipDirective._handleMouseEnter(null);
expect(tooltipDirective.visible).toBeTruthy();
tooltipDirective.show();
expect(tooltipDirective._isTooltipVisible()).toBe(true);

fixture.detectChanges();
expect(overlayContainerElement.textContent).toBe('some message');
expect(overlayContainerElement.textContent).toContain(initialTooltipMessage);

tooltipDirective.hide();
expect(tooltipDirective._isTooltipVisible()).toBe(true);

// After hidden, expect that the tooltip is not visible.
tick(MATERIAL_TOOLTIP_HIDE_DELAY);
expect(tooltipDirective._isTooltipVisible()).toBe(false);
}));

it('should remove the tooltip when changing position', () => {
const initialPosition: TooltipPosition = 'below';
const changedPosition: TooltipPosition = 'above';

tooltipDirective._handleMouseLeave(null);
expect(overlayContainerElement.textContent).toBe('');
expect(tooltipDirective._tooltipRef).toBeUndefined();

tooltipDirective.position = initialPosition;
tooltipDirective.show();
expect(tooltipDirective._tooltipRef).toBeDefined();

// Same position value should not remove the tooltip
tooltipDirective.position = initialPosition;
expect(tooltipDirective._tooltipRef).toBeDefined();

// Different position value should destroy the tooltip
tooltipDirective.position = changedPosition;
expect(tooltipDirective._tooltipRef).toBeNull();
expect(tooltipDirective._overlayRef).toBeNull();
});

it('should be able to modify the tooltip message', () => {
expect(tooltipDirective._tooltipRef).toBeUndefined();

tooltipDirective.show();
expect(tooltipDirective._tooltipRef._visibility).toBe('visible');

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

const newMessage = 'new tooltip message';
tooltipDirective.message = newMessage;

fixture.detectChanges();
expect(overlayContainerElement.textContent).toContain(newMessage);
});
});
});

@Component({
selector: 'app',
template: `<button md-tooltip="some message" [tooltip-position]="position">Button</button>`
template: `<button [md-tooltip]="message" [tooltip-position]="position">Button</button>`
})
class BasicTooltipDemo {
position: TooltipPosition = 'below';
message: string = initialTooltipMessage;
}
Loading

0 comments on commit b4d733a

Please sign in to comment.