|
1 |
| -import { ChangeDetectorRef, ElementRef, Renderer2, ViewContainerRef } from '@angular/core'; |
2 |
| -import { TestBed } from '@angular/core/testing'; |
3 |
| -import { IntersectionService, ListenersService } from '../services'; |
| 1 | +import { DOCUMENT } from '@angular/common'; |
| 2 | +import { |
| 3 | + ChangeDetectorRef, |
| 4 | + Component, |
| 5 | + ComponentRef, |
| 6 | + DebugElement, |
| 7 | + ElementRef, |
| 8 | + Renderer2, |
| 9 | + ViewContainerRef |
| 10 | +} from '@angular/core'; |
| 11 | +import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing'; |
| 12 | +import { By } from '@angular/platform-browser'; |
4 | 13 | import { TooltipDirective } from './tooltip.directive';
|
| 14 | +import { Triggers } from '../coreui.types'; |
| 15 | +import { ListenersService } from '../services'; |
| 16 | + |
| 17 | +@Component({ |
| 18 | + template: '<button cTooltip="content" [(cTooltipVisible)]="visible" [cTooltipTrigger]="trigger" >Test</button>', |
| 19 | + imports: [TooltipDirective] |
| 20 | +}) |
| 21 | +export class TestComponent { |
| 22 | + content = 'Test'; |
| 23 | + visible = false; |
| 24 | + trigger: Triggers[] = ['hover', 'click']; |
| 25 | +} |
5 | 26 |
|
6 | 27 | class MockElementRef extends ElementRef {}
|
7 | 28 |
|
8 | 29 | describe('TooltipDirective', () => {
|
9 |
| - it('should create an instance', () => { |
| 30 | + let component: TestComponent; |
| 31 | + let componentRef: ComponentRef<TestComponent>; |
| 32 | + let fixture: ComponentFixture<TestComponent>; |
| 33 | + let debugElement: DebugElement; |
| 34 | + let document: Document; |
| 35 | + |
| 36 | + beforeEach(() => { |
10 | 37 | TestBed.configureTestingModule({
|
| 38 | + imports: [TestComponent], |
11 | 39 | providers: [
|
12 |
| - IntersectionService, |
| 40 | + // IntersectionService, |
13 | 41 | Renderer2,
|
14 | 42 | ListenersService,
|
15 | 43 | { provide: ElementRef, useClass: MockElementRef },
|
16 | 44 | ViewContainerRef,
|
17 | 45 | ChangeDetectorRef
|
18 | 46 | ]
|
19 |
| - }); |
| 47 | + }).compileComponents(); |
| 48 | + document = TestBed.inject(DOCUMENT); |
| 49 | + fixture = TestBed.createComponent(TestComponent); |
| 50 | + component = fixture.componentInstance; |
| 51 | + debugElement = fixture.debugElement.query(By.directive(TooltipDirective)); |
| 52 | + fixture.autoDetectChanges(); |
| 53 | + }); |
20 | 54 |
|
| 55 | + it('should create an instance', () => { |
21 | 56 | TestBed.runInInjectionContext(() => {
|
22 | 57 | const directive = new TooltipDirective();
|
23 | 58 | expect(directive).toBeTruthy();
|
24 | 59 | });
|
25 | 60 | });
|
| 61 | + |
| 62 | + it('should have css classes', fakeAsync(() => { |
| 63 | + expect(document.querySelector('.tooltip.show')).toBeNull(); |
| 64 | + component.visible = true; |
| 65 | + fixture.detectChanges(); |
| 66 | + tick(500); |
| 67 | + expect(document.querySelector('.tooltip.show')).toBeTruthy(); |
| 68 | + component.visible = false; |
| 69 | + fixture.detectChanges(); |
| 70 | + tick(500); |
| 71 | + expect(document.querySelector('.tooltip.show')).toBeNull(); |
| 72 | + })); |
| 73 | + |
| 74 | + it('should set popover on and off', fakeAsync(() => { |
| 75 | + fixture.autoDetectChanges(); |
| 76 | + component.visible = false; |
| 77 | + expect(document.querySelector('.tooltip.show')).toBeNull(); |
| 78 | + debugElement.nativeElement.dispatchEvent(new Event('mouseenter')); |
| 79 | + tick(500); |
| 80 | + expect(document.querySelector('.tooltip.show')).toBeTruthy(); |
| 81 | + debugElement.nativeElement.dispatchEvent(new Event('mouseleave')); |
| 82 | + tick(500); |
| 83 | + expect(document.querySelector('.tooltip.show')).toBeNull(); |
| 84 | + })); |
| 85 | + |
| 86 | + it('should toggle popover', fakeAsync(() => { |
| 87 | + fixture.autoDetectChanges(); |
| 88 | + component.visible = false; |
| 89 | + expect(document.querySelector('.tooltip.show')).toBeNull(); |
| 90 | + debugElement.nativeElement.dispatchEvent(new Event('click')); |
| 91 | + tick(500); |
| 92 | + expect(document.querySelector('.tooltip.show')).toBeTruthy(); |
| 93 | + debugElement.nativeElement.dispatchEvent(new Event('click')); |
| 94 | + tick(500); |
| 95 | + expect(document.querySelector('.tooltip.show')).toBeNull(); |
| 96 | + })); |
26 | 97 | });
|
0 commit comments