From e0c4c6dd997d49152906ada2bca22831575be76f Mon Sep 17 00:00:00 2001 From: Giacomo Ferlaino Date: Fri, 13 Mar 2020 21:53:48 +0100 Subject: [PATCH] feat: Added checkOverflow method tests --- src/lib/directives/ng2-fittext.directive.ts | 2 +- .../specs/ng2-fittext.directive.spec.ts | 47 ++++++++++++++++++- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/lib/directives/ng2-fittext.directive.ts b/src/lib/directives/ng2-fittext.directive.ts index 8922fbd..2c435b8 100644 --- a/src/lib/directives/ng2-fittext.directive.ts +++ b/src/lib/directives/ng2-fittext.directive.ts @@ -62,7 +62,7 @@ export class Ng2FittextDirective return Math.floor(fontSize / speed); } - checkOverflow(parent: any, children: any) { + checkOverflow(parent: any, children: any): boolean { const overflowX = children.scrollWidth - parent.clientWidth; const overflowY = children.clientHeight - parent.clientHeight; return overflowX > 1 || overflowY > 1; diff --git a/src/lib/directives/specs/ng2-fittext.directive.spec.ts b/src/lib/directives/specs/ng2-fittext.directive.spec.ts index 4dca2a1..84ecdc3 100644 --- a/src/lib/directives/specs/ng2-fittext.directive.spec.ts +++ b/src/lib/directives/specs/ng2-fittext.directive.spec.ts @@ -71,7 +71,6 @@ describe('Class: Ng2FittextDirective', () => { newFontSize = 500; spyOn(ng2FittextDirective.fontSizeChanged, 'emit'); ng2FittextDirective.setFontSize(newFontSize); - const currentFontSize: number = ng2FittextDirective.getFontSize(); expect(ng2FittextDirective.fontSizeChanged.emit).toHaveBeenCalledWith( newFontSize ); @@ -81,7 +80,6 @@ describe('Class: Ng2FittextDirective', () => { newFontSize = 500; spyOn(ng2FittextDirective.el.nativeElement.style, 'setProperty'); ng2FittextDirective.setFontSize(newFontSize); - const currentFontSize: number = ng2FittextDirective.getFontSize(); expect( ng2FittextDirective.el.nativeElement.style.setProperty ).toHaveBeenCalledWith('font-size', `${newFontSize}px`); @@ -101,4 +99,49 @@ describe('Class: Ng2FittextDirective', () => { expect(ng2FittextDirective.calculateFontSize(8, 3)).toEqual(2); }); }); + + describe('Method: checkOverflow', () => { + let parentElementMock: any; + let childrenElementMock: any; + + beforeEach(() => { + parentElementMock = { + clientWidth: 0, + clientHeight: 0, + }; + childrenElementMock = { + scrollWidth: 0, + clientHeight: 0, + }; + }); + + it('Should return false if no overflow is present', () => { + expect( + ng2FittextDirective.checkOverflow( + parentElementMock, + childrenElementMock + ) + ).toBe(false); + }); + + it('Should return true if x axis has overflow', () => { + childrenElementMock.scrollWidth = 2; + expect( + ng2FittextDirective.checkOverflow( + parentElementMock, + childrenElementMock + ) + ).toBe(true); + }); + + it('Should return true if y axis has overflow', () => { + childrenElementMock.clientHeight = 2; + expect( + ng2FittextDirective.checkOverflow( + parentElementMock, + childrenElementMock + ) + ).toBe(true); + }); + }); });