Skip to content

Commit 8a4016f

Browse files
author
Andrea Barbasso
committed
[UXP-126] add tests for text-selection-tooltip.component
1 parent 85da672 commit 8a4016f

2 files changed

Lines changed: 66 additions & 9 deletions

File tree

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,78 @@
11
import { ComponentFixture, TestBed } from '@angular/core/testing';
2-
32
import { TextSelectionTooltipComponent } from './text-selection-tooltip.component';
3+
import { ChangeDetectorRef, NgZone } from '@angular/core';
4+
import { TranslateService } from '@ngx-translate/core';
5+
import { NativeWindowService } from '../../../core/services/window.service';
46

57
describe('TextSelectionTooltipComponent', () => {
68
let component: TextSelectionTooltipComponent;
79
let fixture: ComponentFixture<TextSelectionTooltipComponent>;
10+
let changeDetectorRef: ChangeDetectorRef;
11+
let ngZone: NgZone;
12+
let translateService: TranslateService;
813

9-
beforeEach(async () => {
10-
await TestBed.configureTestingModule({
11-
declarations: [ TextSelectionTooltipComponent ]
12-
})
13-
.compileComponents();
14+
beforeEach(() => {
15+
TestBed.configureTestingModule({
16+
imports: [ TextSelectionTooltipComponent ],
17+
providers: [
18+
{ provide: ChangeDetectorRef, useValue: {detectChanges: () => fixture.detectChanges()} },
19+
{ provide: NgZone, useValue: new NgZone({}) },
20+
{ provide: TranslateService, useValue: { currentLang: 'en' } },
21+
{ provide: NativeWindowService, useValue: { nativeWindow: window } },
22+
]
23+
});
1424

1525
fixture = TestBed.createComponent(TextSelectionTooltipComponent);
1626
component = fixture.componentInstance;
17-
fixture.detectChanges();
27+
changeDetectorRef = TestBed.inject(ChangeDetectorRef);
28+
ngZone = TestBed.inject(NgZone);
29+
translateService = TestBed.inject(TranslateService);
1830
});
1931

2032
it('should create', () => {
2133
expect(component).toBeTruthy();
2234
});
35+
36+
it('should set up event listener on ngOnInit', () => {
37+
spyOn(window, 'addEventListener');
38+
component.ngOnInit();
39+
expect(window.addEventListener).toHaveBeenCalledWith('scroll', component.boundCheckPosition);
40+
});
41+
42+
it('should remove event listener on ngOnDestroy', () => {
43+
spyOn(window, 'removeEventListener');
44+
component.ngOnDestroy();
45+
expect(window.removeEventListener).toHaveBeenCalledWith('scroll', component.boundCheckPosition);
46+
});
47+
48+
it('should check position correctly', () => {
49+
spyOn(changeDetectorRef, 'detectChanges');
50+
component.elementRectangleTop = 100;
51+
component.elementRectangleHeight = 50;
52+
spyOnProperty(window, 'scrollY').and.returnValue(200);
53+
component.checkPosition();
54+
expect(component.top).toBe(156);
55+
expect(component.bottomPlacement).toBeTrue();
56+
});
57+
58+
it('should handle text to speech correctly', () => {
59+
spyOn(window.speechSynthesis, 'speak');
60+
component.text = 'test';
61+
component.textToSpeech();
62+
expect(window.speechSynthesis.speak).toHaveBeenCalled();
63+
});
64+
65+
it('should handle pause text to speech correctly', () => {
66+
spyOn(window.speechSynthesis, 'pause');
67+
component.pauseTextToSpeech();
68+
expect(window.speechSynthesis.pause).toHaveBeenCalled();
69+
expect(component.isPaused).toBeTrue();
70+
});
71+
72+
it('should handle resume text to speech correctly', () => {
73+
spyOn(window.speechSynthesis, 'resume');
74+
component.resumeTextToSpeech();
75+
expect(window.speechSynthesis.resume).toHaveBeenCalled();
76+
expect(component.isPaused).toBeFalse();
77+
});
2378
});

src/app/directives/text-select/text-selection-tooltip/text-selection-tooltip.component.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import { TranslateService } from '@ngx-translate/core';
2626
})
2727
export class TextSelectionTooltipComponent implements OnInit, OnDestroy {
2828

29+
private readonly tooltipOffset = 6;
30+
2931
@Input()
3032
showTTSControls = true;
3133

@@ -80,10 +82,10 @@ export class TextSelectionTooltipComponent implements OnInit, OnDestroy {
8082

8183
checkPosition() {
8284
if (this.elementRectangleTop < this._window.nativeWindow.scrollY) {
83-
this.top = this.elementRectangleTop + this.elementRectangleHeight + 6;
85+
this.top = this.elementRectangleTop + this.elementRectangleHeight + this.tooltipOffset;
8486
this.bottomPlacement = true;
8587
} else {
86-
this.top = this.elementRectangleTop - 6;
88+
this.top = this.elementRectangleTop - this.tooltipOffset;
8789
this.bottomPlacement = false;
8890
}
8991
this.changeDetectorRef.detectChanges();

0 commit comments

Comments
 (0)