|
1 |
| -import {async, ComponentFixture, TestBed} from '@angular/core/testing'; |
| 1 | +import {async, ComponentFixture, TestBed, tick, fakeAsync} from '@angular/core/testing'; |
2 | 2 | import {Component, DebugElement} from '@angular/core';
|
3 | 3 | import {By} from '@angular/platform-browser';
|
4 |
| -import {TooltipPosition, MdTooltip} from './tooltip'; |
| 4 | +import {TooltipPosition, MdTooltip, MATERIAL_TOOLTIP_HIDE_DELAY} from './tooltip'; |
5 | 5 | import {OverlayContainer} from '../core';
|
6 | 6 | import {MdTooltipModule} from './tooltip';
|
7 | 7 |
|
| 8 | +const initialTooltipMessage = 'initial tooltip message'; |
8 | 9 |
|
9 |
| -describe('MdTooltip', () => { |
| 10 | +fdescribe('MdTooltip', () => { |
10 | 11 | let overlayContainerElement: HTMLElement;
|
11 | 12 |
|
| 13 | + |
12 | 14 | beforeEach(async(() => {
|
13 | 15 | TestBed.configureTestingModule({
|
14 | 16 | imports: [MdTooltipModule.forRoot()],
|
@@ -38,25 +40,66 @@ describe('MdTooltip', () => {
|
38 | 40 | tooltipDirective = buttonDebugElement.injector.get(MdTooltip);
|
39 | 41 | });
|
40 | 42 |
|
41 |
| - it('should show/hide on mouse enter/leave', () => { |
42 |
| - expect(tooltipDirective.visible).toBeFalsy(); |
| 43 | + it('should show and hide the tooltip', fakeAsync(() => { |
| 44 | + expect(tooltipDirective._tooltipRef).toBeUndefined(); |
43 | 45 |
|
44 |
| - tooltipDirective._handleMouseEnter(null); |
45 |
| - expect(tooltipDirective.visible).toBeTruthy(); |
| 46 | + tooltipDirective.show(); |
| 47 | + expect(tooltipDirective._isTooltipVisible()).toBe(true); |
46 | 48 |
|
47 | 49 | fixture.detectChanges();
|
48 |
| - expect(overlayContainerElement.textContent).toBe('some message'); |
| 50 | + expect(overlayContainerElement.textContent).toContain(initialTooltipMessage); |
| 51 | + |
| 52 | + tooltipDirective.hide(); |
| 53 | + expect(tooltipDirective._isTooltipVisible()).toBe(true); |
| 54 | + |
| 55 | + // After hidden, expect that the tooltip is not visible. |
| 56 | + tick(MATERIAL_TOOLTIP_HIDE_DELAY); |
| 57 | + expect(tooltipDirective._isTooltipVisible()).toBe(false); |
| 58 | + })); |
| 59 | + |
| 60 | + it('should remove the tooltip when changing position', () => { |
| 61 | + const initialPosition: TooltipPosition = 'below'; |
| 62 | + const changedPosition: TooltipPosition = 'above'; |
49 | 63 |
|
50 |
| - tooltipDirective._handleMouseLeave(null); |
51 |
| - expect(overlayContainerElement.textContent).toBe(''); |
| 64 | + expect(tooltipDirective._tooltipRef).toBeUndefined(); |
| 65 | + |
| 66 | + tooltipDirective.position = initialPosition; |
| 67 | + tooltipDirective.show(); |
| 68 | + expect(tooltipDirective._tooltipRef).toBeDefined(); |
| 69 | + |
| 70 | + // Same position value should not remove the tooltip |
| 71 | + tooltipDirective.position = initialPosition; |
| 72 | + expect(tooltipDirective._tooltipRef).toBeDefined(); |
| 73 | + |
| 74 | + // Different position value should destroy the tooltip |
| 75 | + tooltipDirective.position = changedPosition; |
| 76 | + expect(tooltipDirective._tooltipRef).toBeNull(); |
| 77 | + expect(tooltipDirective._overlayRef).toBeNull(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should be able to modify the tooltip message', () => { |
| 81 | + expect(tooltipDirective._tooltipRef).toBeUndefined(); |
| 82 | + |
| 83 | + tooltipDirective.show(); |
| 84 | + expect(tooltipDirective._tooltipRef._visibility).toBe('visible'); |
| 85 | + |
| 86 | + fixture.detectChanges(); |
| 87 | + expect(overlayContainerElement.textContent).toContain(initialTooltipMessage); |
| 88 | + |
| 89 | + const newMessage = 'new tooltip message'; |
| 90 | + tooltipDirective.message = newMessage; |
| 91 | + |
| 92 | + fixture.detectChanges(); |
| 93 | + expect(overlayContainerElement.textContent).toContain(newMessage); |
52 | 94 | });
|
53 | 95 | });
|
54 | 96 | });
|
55 | 97 |
|
56 | 98 | @Component({
|
57 | 99 | selector: 'app',
|
58 |
| - template: `<button md-tooltip="some message" [tooltip-position]="position">Button</button>` |
| 100 | + template: `<button [md-tooltip]="message" [tooltip-position]="position">Button</button>` |
59 | 101 | })
|
60 | 102 | class BasicTooltipDemo {
|
61 | 103 | position: TooltipPosition = 'below';
|
| 104 | + message: string = initialTooltipMessage; |
62 | 105 | }
|
0 commit comments