Skip to content

Commit 8cdbb9e

Browse files
committed
refactor(modal-toggle): host binding, cleanup, tests
1 parent e976a2a commit 8cdbb9e

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,44 @@
1+
import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
2+
import { Component, DebugElement } from '@angular/core';
3+
import { By } from '@angular/platform-browser';
14
import { ModalToggleDirective } from './modal-toggle.directive';
2-
import { TestBed } from '@angular/core/testing';
5+
6+
@Component({
7+
template: '<button cModalToggle></button>',
8+
imports: [ModalToggleDirective]
9+
})
10+
class TestComponent {}
311

412
describe('ModalDismissDirective', () => {
13+
let component: TestComponent;
14+
let fixture: ComponentFixture<TestComponent>;
15+
let debugElement: DebugElement;
16+
17+
beforeEach(() => {
18+
TestBed.configureTestingModule({
19+
imports: [TestComponent]
20+
}).compileComponents();
21+
22+
fixture = TestBed.createComponent(TestComponent);
23+
component = fixture.componentInstance;
24+
debugElement = fixture.debugElement.query(By.directive(ModalToggleDirective));
25+
fixture.detectChanges();
26+
});
27+
528
it('should create an instance', () => {
629
TestBed.runInInjectionContext(() => {
730
const directive = new ModalToggleDirective();
831
expect(directive).toBeTruthy();
932
});
1033
});
34+
35+
it('should handle click', fakeAsync(() => {
36+
const directive = debugElement.injector.get(ModalToggleDirective);
37+
const spy = spyOn(directive, 'dismiss');
38+
debugElement.nativeElement.dispatchEvent(new Event('click'));
39+
directive.dismiss(new Event('click'));
40+
tick();
41+
fixture.detectChanges();
42+
expect(spy).toHaveBeenCalledTimes(2);
43+
}));
1144
});

projects/coreui-angular/src/lib/modal/modal-dismiss/modal-toggle.directive.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import { Directive, HostListener, inject, input } from '@angular/core';
1+
import { Directive, inject, input } from '@angular/core';
22

33
import { ModalService } from '../modal.service';
44

55
@Directive({
6-
selector: '[cModalToggle]'
6+
selector: '[cModalToggle]',
7+
host: {
8+
'(click)': 'dismiss($event)'
9+
}
710
})
811
export class ModalToggleDirective {
912
readonly #modalService = inject(ModalService);
@@ -14,8 +17,7 @@ export class ModalToggleDirective {
1417
*/
1518
readonly toggle = input<string>(undefined, { alias: 'cModalToggle' });
1619

17-
@HostListener('click', ['$event'])
18-
dismiss($event: any): void {
20+
dismiss($event: Event): void {
1921
$event.preventDefault();
2022
this.#modalService.toggle({ show: 'toggle', id: this.toggle() });
2123
}

projects/coreui-angular/src/lib/modal/modal/modal.component.spec.ts

+10
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,14 @@ describe('ModalComponent', () => {
2727
expect(fixture.nativeElement).toHaveClass('modal');
2828
expect(fixture.nativeElement).toHaveClass('fade');
2929
});
30+
31+
// it('should be visible', fakeAsync(() => {
32+
// fixture.componentRef.setInput('visible', true);
33+
// fixture.detectChanges();
34+
// expect(fixture.nativeElement).toHaveClass('show');
35+
// }));
36+
37+
// it('should call event handling functions', fakeAsync(() => {
38+
//
39+
// }));
3040
});

0 commit comments

Comments
 (0)