From 0cd17ab15b963d31d23c91ca2da220b71efc6be2 Mon Sep 17 00:00:00 2001 From: Giacomo Ferlaino Date: Wed, 18 Mar 2020 02:00:36 +0100 Subject: [PATCH] ref: Created a dedicated method to determine if the element has overflow or not --- src/lib/directives/ng2-fittext.directive.ts | 17 ++++---- .../specs/ng2-fittext.directive.spec.ts | 39 +++++++++++++++++++ 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/src/lib/directives/ng2-fittext.directive.ts b/src/lib/directives/ng2-fittext.directive.ts index 3bc6410..331783c 100644 --- a/src/lib/directives/ng2-fittext.directive.ts +++ b/src/lib/directives/ng2-fittext.directive.ts @@ -109,13 +109,7 @@ export class Ng2FittextDirective ngAfterViewInit() { if (this.isVisible() && !this.isDone()) { if (this.fittext) { - const overflow = this.container - ? this.checkOverflow(this.container, this.el.nativeElement) - : this.checkOverflow( - this.el.nativeElement.parentElement, - this.el.nativeElement - ); - if (overflow) { + if (this.hasOverflow()) { if (this.fontSize > this.minFontSize) { // iterate only until font size is bigger than minimal value this.setFontSize(this.calculateFontSize(this.fontSize, this.speed)); @@ -165,4 +159,13 @@ export class Ng2FittextDirective isVisible(): boolean { return this.getStartFontSizeFromHeight() > 0; } + + hasOverflow(): boolean { + return this.container + ? this.checkOverflow(this.container, this.el.nativeElement) + : this.checkOverflow( + this.el.nativeElement.parentElement, + this.el.nativeElement + ); + } } diff --git a/src/lib/directives/specs/ng2-fittext.directive.spec.ts b/src/lib/directives/specs/ng2-fittext.directive.spec.ts index b8877c4..f1f26ba 100644 --- a/src/lib/directives/specs/ng2-fittext.directive.spec.ts +++ b/src/lib/directives/specs/ng2-fittext.directive.spec.ts @@ -268,4 +268,43 @@ describe('Class: Ng2FittextDirective', () => { ).toBe(true); }); }); + + describe('Method: hasOverflow', () => { + let containerMock: any; + let parentElementMock: any; + + beforeEach(() => { + containerMock = { + isContainer: true, + }; + parentElementMock = { + isParentElement: true, + }; + ng2FittextDirective.container = { ...containerMock }; + ng2FittextDirective.el.nativeElement = { + parentElement: { ...parentElementMock }, + } as HTMLElement; + }); + + it('Should calculate the overflow using the container if is present', () => { + spyOn(ng2FittextDirective, 'checkOverflow').and.callFake( + (parentElement: any, childrenElement: any) => { + expect(parentElement).toEqual(containerMock); + return true; + } + ); + expect(ng2FittextDirective.hasOverflow()).toBe(true); + }); + + it('Should calculate the overflow using the parent element if the container is not present', () => { + delete ng2FittextDirective.container; + spyOn(ng2FittextDirective, 'checkOverflow').and.callFake( + (parentElement: any, childrenElement: any) => { + expect(parentElement).toEqual(parentElementMock); + return true; + } + ); + expect(ng2FittextDirective.hasOverflow()).toBe(true); + }); + }); });