Skip to content

Commit bb045a7

Browse files
committed
Add test
1 parent e246248 commit bb045a7

File tree

3 files changed

+93
-28
lines changed

3 files changed

+93
-28
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { Component } from '@angular/core';
2+
import { ComponentFixture, TestBed } from '@angular/core/testing';
3+
import { BrowserModule, By } from '@angular/platform-browser';
4+
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
5+
import { FormsModule } from '@angular/forms';
6+
import { DragDropModule } from '@angular/cdk/drag-drop';
7+
import { OverlayModule } from '@angular/cdk/overlay';
8+
import { TreeModule } from 'primeng/tree';
9+
import { HttpClientModule } from '@angular/common/http';
10+
import { TooltipComponent } from './tooltip.component';
11+
import { TooltipDirective } from './tooltip.directive';
12+
13+
@Component({
14+
selector: 'systelab-tabs-test',
15+
template: `
16+
<div class="m-4">
17+
<systelab-button id="my-component" (action)="doSomething()" systelabTooltip="Tooltip on top"
18+
[systelabTooltipDelay]="0" [systelabTooltipHideDelay]="0">Tooltip on top</systelab-button>
19+
</div>
20+
`,
21+
styles: []
22+
})
23+
export class TooltipTestComponent {
24+
public doSomething() {
25+
console.log('Button clicked');
26+
}
27+
}
28+
29+
describe('Systelab Tooltip', () => {
30+
let fixture: ComponentFixture<TooltipTestComponent>;
31+
32+
beforeEach(async () => {
33+
await TestBed.configureTestingModule({
34+
imports: [BrowserModule,
35+
BrowserAnimationsModule,
36+
FormsModule,
37+
DragDropModule,
38+
OverlayModule,
39+
TreeModule,
40+
HttpClientModule],
41+
declarations: [TooltipDirective, TooltipComponent, TooltipTestComponent]
42+
})
43+
.compileComponents();
44+
});
45+
46+
beforeEach(() => {
47+
fixture = TestBed.createComponent(TooltipTestComponent);
48+
fixture.detectChanges();
49+
});
50+
51+
it('should instantiate', () => {
52+
expect(fixture.componentInstance)
53+
.toBeDefined();
54+
});
55+
56+
it('should show and hide the tooltip', async () => {
57+
await fixture.whenStable();
58+
let button = fixture.debugElement.query(By.css('#my-component'));
59+
60+
expect(isPopupVisible(fixture)).toBeFalsy();
61+
62+
button.triggerEventHandler('mouseenter', {});
63+
fixture.detectChanges();
64+
65+
expect(isPopupVisible(fixture)).toBeTruthy();
66+
67+
button.triggerEventHandler('mouseleave', {});
68+
fixture.detectChanges();
69+
70+
expect(isPopupVisible(fixture)).toBeFalsy();
71+
})
72+
});
73+
74+
function isPopupVisible(fixture: ComponentFixture<TooltipTestComponent>) {
75+
return (document.querySelector('tooltip') !== null);
76+
}

projects/systelab-components/src/lib/tooltip/tooltip.component.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Component, OnInit} from '@angular/core';
1+
import {Component} from '@angular/core';
22

33
@Component({
44
selector: 'tooltip',
@@ -11,5 +11,4 @@ export class TooltipComponent {
1111
public left = 0;
1212
public top = 0;
1313
public visible = false;
14-
1514
}

projects/systelab-components/src/lib/tooltip/tooltip.directive.ts

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,38 @@
1-
import { ApplicationRef, ComponentRef, Directive, ElementRef, EmbeddedViewRef, HostListener, Input, ViewContainerRef } from '@angular/core';
1+
import { ApplicationRef, ComponentRef, Directive, ElementRef, EmbeddedViewRef, HostListener, Input, OnDestroy, ViewContainerRef } from '@angular/core';
22
import { TooltipComponent } from "./tooltip.component";
33

44
@Directive({
55
selector: '[systelabTooltip],[systelabTooltipHtml]'
66
})
7-
export class TooltipDirective {
7+
export class TooltipDirective implements OnDestroy {
88

99
public static readonly DEFAULT_DELAY = 200;
1010

1111
@Input() public systelabTooltip: string;
1212
@Input() public systelabTooltipHtml: string;
13-
@Input() systelabTooltipPlacement: undefined | 'top' | 'right' | 'bottom' | 'left';
14-
@Input() systelabTooltipDelay = TooltipDirective.DEFAULT_DELAY;
15-
@Input() systelabTooltipHideDelay = TooltipDirective.DEFAULT_DELAY;
13+
@Input() public systelabTooltipPlacement: undefined | 'top' | 'right' | 'bottom' | 'left';
14+
@Input() public systelabTooltipDelay = TooltipDirective.DEFAULT_DELAY;
15+
@Input() public systelabTooltipHideDelay = TooltipDirective.DEFAULT_DELAY;
1616

1717
private componentRef: ComponentRef<any> | null = null;
1818
private showTimeout?: number;
1919
private hideTimeout?: number;
20-
private touchTimeout?: number;
2120

2221
constructor(private elementRef: ElementRef, private appRef: ApplicationRef, private viewContainerRef: ViewContainerRef) {
2322
}
2423

2524
@HostListener('mouseenter')
26-
onMouseEnter(): void {
25+
public onMouseEnter(): void {
2726
this.initializeTooltip();
2827
}
2928

3029
@HostListener('mouseleave')
31-
onMouseLeave(): void {
32-
this.setHideTooltipTimeout();
33-
}
34-
35-
@HostListener('touchstart', ['$event'])
36-
onTouchStart($event: TouchEvent): void {
37-
$event.preventDefault();
38-
window.clearTimeout(this.touchTimeout);
39-
this.touchTimeout = window.setTimeout(this.initializeTooltip.bind(this), 500);
40-
}
41-
42-
@HostListener('touchend')
43-
onTouchEnd(): void {
44-
window.clearTimeout(this.touchTimeout);
45-
this.setHideTooltipTimeout();
30+
public onMouseLeave(): void {
31+
if (this.systelabTooltipHideDelay) {
32+
this.hideTimeout = this.setHideTooltipTimeout();
33+
} else {
34+
this.destroy();
35+
}
4636
}
4737

4838
private initializeTooltip() {
@@ -105,15 +95,15 @@ export class TooltipDirective {
10595
}
10696
}
10797

108-
private setHideTooltipTimeout() {
109-
this.hideTimeout = window.setTimeout(this.destroy.bind(this), this.systelabTooltipHideDelay);
98+
private setHideTooltipTimeout():number {
99+
return window.setTimeout(this.destroy.bind(this), this.systelabTooltipHideDelay);
110100
}
111101

112-
ngOnDestroy(): void {
102+
public ngOnDestroy(): void {
113103
this.destroy();
114104
}
115105

116-
destroy(): void {
106+
public destroy(): void {
117107
if (this.componentRef !== null) {
118108
window.clearInterval(this.showTimeout);
119109
window.clearInterval(this.systelabTooltipHideDelay);

0 commit comments

Comments
 (0)