Skip to content

Commit f1d6d44

Browse files
committed
test(popover): coverage
1 parent 8deeabf commit f1d6d44

File tree

3 files changed

+86
-14
lines changed

3 files changed

+86
-14
lines changed
Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,97 @@
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';
13+
import { ListenersService } from '../services';
414
import { PopoverDirective } from './popover.directive';
15+
import { Triggers } from '../coreui.types';
16+
17+
@Component({
18+
template: '<button cPopover="content" [(cPopoverVisible)]="visible" [cPopoverTrigger]="trigger" >Test</button>',
19+
imports: [PopoverDirective]
20+
})
21+
export class TestComponent {
22+
content = 'Test';
23+
visible = false;
24+
trigger: Triggers[] = ['hover', 'click'];
25+
}
526

627
class MockElementRef extends ElementRef {}
728

829
describe('PopoverDirective', () => {
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(() => {
1037
TestBed.configureTestingModule({
38+
imports: [TestComponent],
1139
providers: [
12-
IntersectionService,
40+
// IntersectionService,
1341
Renderer2,
1442
ListenersService,
1543
{ provide: ElementRef, useClass: MockElementRef },
1644
ViewContainerRef,
1745
ChangeDetectorRef
1846
]
19-
});
47+
}).compileComponents();
48+
document = TestBed.inject(DOCUMENT);
49+
fixture = TestBed.createComponent(TestComponent);
50+
component = fixture.componentInstance;
51+
debugElement = fixture.debugElement.query(By.directive(PopoverDirective));
52+
fixture.autoDetectChanges();
53+
});
54+
55+
it('should create an instance', () => {
2056
TestBed.runInInjectionContext(() => {
2157
const directive = new PopoverDirective();
2258
expect(directive).toBeTruthy();
2359
});
2460
});
61+
62+
it('should have css classes', fakeAsync(() => {
63+
expect(document.querySelector('.popover.show')).toBeNull();
64+
component.visible = true;
65+
fixture.detectChanges();
66+
tick(500);
67+
expect(document.querySelector('.popover.show')).toBeTruthy();
68+
component.visible = false;
69+
fixture.detectChanges();
70+
tick(500);
71+
expect(document.querySelector('.popover.show')).toBeNull();
72+
}));
73+
74+
it('should set popover on and off', fakeAsync(() => {
75+
fixture.autoDetectChanges();
76+
component.visible = false;
77+
expect(document.querySelector('.popover.show')).toBeNull();
78+
debugElement.nativeElement.dispatchEvent(new Event('mouseenter'));
79+
tick(500);
80+
expect(document.querySelector('.popover.show')).toBeTruthy();
81+
debugElement.nativeElement.dispatchEvent(new Event('mouseleave'));
82+
tick(500);
83+
expect(document.querySelector('.popover.show')).toBeNull();
84+
}));
85+
86+
it('should toggle popover', fakeAsync(() => {
87+
fixture.autoDetectChanges();
88+
component.visible = false;
89+
expect(document.querySelector('.popover.show')).toBeNull();
90+
debugElement.nativeElement.dispatchEvent(new Event('click'));
91+
tick(500);
92+
expect(document.querySelector('.popover.show')).toBeTruthy();
93+
debugElement.nativeElement.dispatchEvent(new Event('click'));
94+
tick(500);
95+
expect(document.querySelector('.popover.show')).toBeNull();
96+
}));
2597
});

projects/coreui-angular/src/lib/popover/popover.directive.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export class PopoverDirective implements OnDestroy, OnInit, AfterViewInit {
4444

4545
/**
4646
* Content of popover
47-
* @type {string | TemplateRef}
47+
* @return {string | TemplateRef}
4848
*/
4949
readonly content = input<string | TemplateRef<any> | undefined>(undefined, { alias: 'cPopover' });
5050

@@ -74,14 +74,14 @@ export class PopoverDirective implements OnDestroy, OnInit, AfterViewInit {
7474

7575
/**
7676
* Describes the placement of your component after Popper.js has applied all the modifiers that may have flipped or altered the originally provided placement property.
77-
* @type: 'top' | 'bottom' | 'left' | 'right'
77+
* @return: 'top' | 'bottom' | 'left' | 'right'
7878
* @default: 'top'
7979
*/
8080
readonly placement = input<'top' | 'bottom' | 'left' | 'right'>('top', { alias: 'cPopoverPlacement' });
8181

8282
/**
8383
* ElementRefDirective for positioning the tooltip on reference element
84-
* @type: ElementRefDirective
84+
* @return: ElementRefDirective
8585
* @default: undefined
8686
*/
8787
readonly reference = input<ElementRefDirective | undefined>(undefined, { alias: 'cTooltipRef' });
@@ -90,13 +90,13 @@ export class PopoverDirective implements OnDestroy, OnInit, AfterViewInit {
9090

9191
/**
9292
* Sets which event handlers you’d like provided to your toggle prop. You can specify one trigger or an array of them.
93-
* @type: 'Triggers | Triggers[]
93+
* @return: Triggers | Triggers[]
9494
*/
9595
readonly trigger = input<Triggers | Triggers[]>('hover', { alias: 'cPopoverTrigger' });
9696

9797
/**
9898
* Toggle the visibility of popover component.
99-
* @type boolean
99+
* @return boolean
100100
*/
101101
readonly visible = model(false, { alias: 'cPopoverVisible' });
102102

@@ -142,7 +142,7 @@ export class PopoverDirective implements OnDestroy, OnInit, AfterViewInit {
142142
hostElement: this.#hostElement,
143143
trigger: this.trigger(),
144144
callbackToggle: () => {
145-
this.visible.set(!this.visible());
145+
this.visible.update((visible) => !visible);
146146
},
147147
callbackOff: () => {
148148
this.visible.set(false);

projects/coreui-angular/src/lib/popover/popover/popover.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class PopoverComponent implements OnDestroy {
2929

3030
/**
3131
* Content of popover
32-
* @type {string | TemplateRef}
32+
* @return {string | TemplateRef}
3333
*/
3434
readonly content = input<string | TemplateRef<any>>('');
3535

@@ -39,7 +39,7 @@ export class PopoverComponent implements OnDestroy {
3939

4040
/**
4141
* Toggle the visibility of popover component.
42-
* @type boolean
42+
* @return boolean
4343
*/
4444
readonly visible = input(false, { transform: booleanAttribute });
4545
readonly id = input<string>();

0 commit comments

Comments
 (0)