Skip to content
Snippets Groups Projects
Commit 5c39c056 authored by Alexander Reifschneider's avatar Alexander Reifschneider
Browse files

OZG-7619 opt tests

parent 1d5d07e7
No related branches found
No related tags found
1 merge request!64OZG-7619 Add role hints
...@@ -273,24 +273,25 @@ describe('TooltipDirective', () => { ...@@ -273,24 +273,25 @@ describe('TooltipDirective', () => {
}); });
describe('setWidth', () => { describe('setWidth', () => {
it('should set max width', () => { function setWidthOnComponentRef(width: number): void {
directive.componentRef = Object.assign(mockComponentRef, { directive.componentRef = Object.assign(mockComponentRef, {
location: { location: {
nativeElement: { children: [{ getBoundingClientRect: jest.fn().mockReturnValue({ width: MAX_WIDTH * REM + 100 }) }] }, nativeElement: { children: [{ getBoundingClientRect: jest.fn().mockReturnValue({ width }) }] },
}, },
}); });
}
const maxTooltipWidth: number = MAX_WIDTH * REM;
it('should set width to max width', () => {
setWidthOnComponentRef(maxTooltipWidth + 100);
directive.setWidth(); directive.setWidth();
expect(directive.componentRef.instance.width).toBe(MAX_WIDTH); expect(directive.componentRef.instance.width).toBe(MAX_WIDTH);
}); });
it('should unset width', () => { it('should set width to null', () => {
directive.componentRef = Object.assign(mockComponentRef, { setWidthOnComponentRef(maxTooltipWidth - 100);
location: {
nativeElement: { children: [{ getBoundingClientRect: jest.fn().mockReturnValue({ width: MAX_WIDTH * REM - 100 }) }] },
},
});
directive.setWidth(); directive.setWidth();
...@@ -327,7 +328,7 @@ describe('TooltipDirective', () => { ...@@ -327,7 +328,7 @@ describe('TooltipDirective', () => {
}); });
}); });
describe('setLeftOffset', () => { describe('calculateLeftOffset', () => {
beforeEach(() => { beforeEach(() => {
directive.leftOffset = 0; directive.leftOffset = 0;
}); });
...@@ -335,40 +336,40 @@ describe('TooltipDirective', () => { ...@@ -335,40 +336,40 @@ describe('TooltipDirective', () => {
it('should set 0 offset, if parent wider than tooltip', () => { it('should set 0 offset, if parent wider than tooltip', () => {
const rectForStandard: DOMRect = { ...parentRect, left: 50, right: 500, width: 100 }; const rectForStandard: DOMRect = { ...parentRect, left: 50, right: 500, width: 100 };
directive.setLeftOffset(36, rectForStandard, 1000); const result: number = directive.calculateLeftOffset(36, rectForStandard, 1000);
expect(directive.leftOffset).toBe(0); expect(result).toBe(0);
}); });
it('should set positive left offset', () => { it('should set positive left offset', () => {
const rectForLeft: DOMRect = { ...parentRect, left: 10 }; const rectForLeft: DOMRect = { ...parentRect, left: 10 };
directive.setLeftOffset(50, rectForLeft, 1000); const result: number = directive.calculateLeftOffset(50, rectForLeft, 1000);
expect(directive.leftOffset).toBe(15); expect(result).toBe(15);
}); });
it('should set negative left offset', () => { it('should set negative left offset', () => {
const rectForRight: DOMRect = { ...parentRect, left: 50, right: 990 }; const rectForRight: DOMRect = { ...parentRect, left: 50, right: 990 };
directive.setLeftOffset(50, rectForRight, 1000); const result: number = directive.calculateLeftOffset(50, rectForRight, 1000);
expect(directive.leftOffset).toBe(-15); expect(result).toBe(-15);
}); });
it('should set 0 offset', () => { it('should set 0 offset', () => {
const rectForStandard: DOMRect = { ...parentRect, left: 50, right: 500 }; const rectForStandard: DOMRect = { ...parentRect, left: 50, right: 500 };
directive.setLeftOffset(36, rectForStandard, 1000); const result: number = directive.calculateLeftOffset(36, rectForStandard, 1000);
expect(directive.leftOffset).toBe(0); expect(result).toBe(0);
}); });
}); });
describe('setTooltipOffsetAndPosition', () => { describe('setTooltipOffsetAndPosition', () => {
beforeEach(() => { beforeEach(() => {
directive.setAutoPosition = jest.fn(); directive.setAutoPosition = jest.fn();
directive.setLeftOffset = jest.fn(); directive.calculateLeftOffset = jest.fn();
directive.componentRef = Object.assign(mockComponentRef, { directive.componentRef = Object.assign(mockComponentRef, {
location: { location: {
nativeElement: { children: [{ getBoundingClientRect: jest.fn().mockReturnValue({ height: 100, width: 200 }) }] }, nativeElement: { children: [{ getBoundingClientRect: jest.fn().mockReturnValue({ height: 100, width: 200 }) }] },
...@@ -385,10 +386,10 @@ describe('TooltipDirective', () => { ...@@ -385,10 +386,10 @@ describe('TooltipDirective', () => {
expect(directive.setAutoPosition).toHaveBeenCalledWith(100, {}, 404); expect(directive.setAutoPosition).toHaveBeenCalledWith(100, {}, 404);
}); });
it('should set tooltip left offset', () => { it('should calculate^ tooltip left offset', () => {
directive.setTooltipOffsetAndPosition(); directive.setTooltipOffsetAndPosition();
expect(directive.setLeftOffset).toHaveBeenCalledWith(200, {}, 503); expect(directive.calculateLeftOffset).toHaveBeenCalledWith(200, {}, 503);
}); });
}); });
......
...@@ -167,32 +167,29 @@ export class TooltipDirective implements OnDestroy { ...@@ -167,32 +167,29 @@ export class TooltipDirective implements OnDestroy {
this.position = this.tooltipPosition; this.position = this.tooltipPosition;
} }
setLeftOffset(tooltipWidth: number, parentRect: DOMRect, windowWidth: number): void { calculateLeftOffset(tooltipWidth: number, parentRect: DOMRect, windowWidth: number): number {
const { left, right, width } = parentRect; const { left, right, width } = parentRect;
const halfTooltipWidth: number = tooltipWidth / 2; const halfTooltipWidth: number = tooltipWidth / 2;
if (tooltipWidth < width) { if (tooltipWidth < width) {
this.leftOffset = 0; return 0;
return;
} }
if (halfTooltipWidth > left) { if (halfTooltipWidth > left) {
this.leftOffset = halfTooltipWidth - left; return halfTooltipWidth - left;
return;
} }
if (halfTooltipWidth > windowWidth - right) { if (halfTooltipWidth > windowWidth - right) {
this.leftOffset = windowWidth - right - halfTooltipWidth; return windowWidth - right - halfTooltipWidth;
return;
} }
this.leftOffset = 0; return 0;
} }
setTooltipOffsetAndPosition(): void { setTooltipOffsetAndPosition(): void {
const { width, height } = this.componentRef.location.nativeElement.children[0].getBoundingClientRect(); const { width, height } = this.componentRef.location.nativeElement.children[0].getBoundingClientRect();
const parentRect: DOMRect = this.elementRef.nativeElement.getBoundingClientRect(); const parentRect: DOMRect = this.elementRef.nativeElement.getBoundingClientRect();
this.setLeftOffset(width, parentRect, window.innerWidth); this.leftOffset = this.calculateLeftOffset(width, parentRect, window.innerWidth);
this.setAutoPosition(height, parentRect, window.innerHeight); this.setAutoPosition(height, parentRect, window.innerHeight);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment