feat: Added setFontSize method unit tests

This commit is contained in:
Giacomo Ferlaino 2020-03-13 00:52:56 +01:00
parent 889b0364a9
commit e54f881e55
2 changed files with 104 additions and 7 deletions

View file

@ -29,7 +29,7 @@ export class Ng2FittextDirective
@Input('modelToWatch') modelToWatch: any;
@Output() fontSizeChanged = new EventEmitter();
@Output() fontSizeChanged: EventEmitter<any> = new EventEmitter();
private fontSize = 1000;
private speed = 1.05;
@ -37,24 +37,27 @@ export class Ng2FittextDirective
constructor(public el: ElementRef, public renderer: Renderer2) {}
setFontSize(fontSize: number) {
if (this.isVisible() && !this.done) {
setFontSize(fontSize: number): void {
if (this.isVisible() && !this.isDone()) {
if (fontSize < this.minFontSize) {
fontSize = this.minFontSize;
}
if (fontSize > this.maxFontSize) {
fontSize = this.maxFontSize;
}
this.fontSize = fontSize;
this.fontSizeChanged.emit(fontSize);
return this.el.nativeElement.style.setProperty(
this.el.nativeElement.style.setProperty(
'font-size',
fontSize.toString() + 'px'
);
}
}
getFontSize(): number {
return this.fontSize;
}
calculateFontSize(fontSize: number, speed: number) {
return Math.floor(fontSize / speed);
}
@ -95,7 +98,7 @@ export class Ng2FittextDirective
}
ngAfterViewInit() {
if (this.isVisible() && !this.done) {
if (this.isVisible() && !this.isDone()) {
if (this.fittext) {
const overflow = this.container
? this.checkOverflow(this.container, this.el.nativeElement)
@ -146,7 +149,11 @@ export class Ng2FittextDirective
: this.el.nativeElement.parentElement.clientWidth;
}
private isVisible(): boolean {
isDone(): boolean {
return this.done;
}
isVisible(): boolean {
return this.getStartFontSizeFromHeight() > 0;
}
}