|
1 | 1 | import { ComponentFixture, TestBed } from '@angular/core/testing'; |
2 | | - |
3 | 2 | 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'; |
4 | 6 |
|
5 | 7 | describe('TextSelectionTooltipComponent', () => { |
6 | 8 | let component: TextSelectionTooltipComponent; |
7 | 9 | let fixture: ComponentFixture<TextSelectionTooltipComponent>; |
| 10 | + let changeDetectorRef: ChangeDetectorRef; |
| 11 | + let ngZone: NgZone; |
| 12 | + let translateService: TranslateService; |
8 | 13 |
|
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 | + }); |
14 | 24 |
|
15 | 25 | fixture = TestBed.createComponent(TextSelectionTooltipComponent); |
16 | 26 | component = fixture.componentInstance; |
17 | | - fixture.detectChanges(); |
| 27 | + changeDetectorRef = TestBed.inject(ChangeDetectorRef); |
| 28 | + ngZone = TestBed.inject(NgZone); |
| 29 | + translateService = TestBed.inject(TranslateService); |
18 | 30 | }); |
19 | 31 |
|
20 | 32 | it('should create', () => { |
21 | 33 | expect(component).toBeTruthy(); |
22 | 34 | }); |
| 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 | + }); |
23 | 78 | }); |
0 commit comments