feat: Added checkOverflow method tests

This commit is contained in:
Giacomo Ferlaino 2020-03-13 21:53:48 +01:00
parent afa67c729b
commit e0c4c6dd99
2 changed files with 46 additions and 3 deletions

View file

@ -62,7 +62,7 @@ export class Ng2FittextDirective
return Math.floor(fontSize / speed); return Math.floor(fontSize / speed);
} }
checkOverflow(parent: any, children: any) { checkOverflow(parent: any, children: any): boolean {
const overflowX = children.scrollWidth - parent.clientWidth; const overflowX = children.scrollWidth - parent.clientWidth;
const overflowY = children.clientHeight - parent.clientHeight; const overflowY = children.clientHeight - parent.clientHeight;
return overflowX > 1 || overflowY > 1; return overflowX > 1 || overflowY > 1;

View file

@ -71,7 +71,6 @@ describe('Class: Ng2FittextDirective', () => {
newFontSize = 500; newFontSize = 500;
spyOn(ng2FittextDirective.fontSizeChanged, 'emit'); spyOn(ng2FittextDirective.fontSizeChanged, 'emit');
ng2FittextDirective.setFontSize(newFontSize); ng2FittextDirective.setFontSize(newFontSize);
const currentFontSize: number = ng2FittextDirective.getFontSize();
expect(ng2FittextDirective.fontSizeChanged.emit).toHaveBeenCalledWith( expect(ng2FittextDirective.fontSizeChanged.emit).toHaveBeenCalledWith(
newFontSize newFontSize
); );
@ -81,7 +80,6 @@ describe('Class: Ng2FittextDirective', () => {
newFontSize = 500; newFontSize = 500;
spyOn(ng2FittextDirective.el.nativeElement.style, 'setProperty'); spyOn(ng2FittextDirective.el.nativeElement.style, 'setProperty');
ng2FittextDirective.setFontSize(newFontSize); ng2FittextDirective.setFontSize(newFontSize);
const currentFontSize: number = ng2FittextDirective.getFontSize();
expect( expect(
ng2FittextDirective.el.nativeElement.style.setProperty ng2FittextDirective.el.nativeElement.style.setProperty
).toHaveBeenCalledWith('font-size', `${newFontSize}px`); ).toHaveBeenCalledWith('font-size', `${newFontSize}px`);
@ -101,4 +99,49 @@ describe('Class: Ng2FittextDirective', () => {
expect(ng2FittextDirective.calculateFontSize(8, 3)).toEqual(2); 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);
});
});
}); });