|
| 1 | +import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing'; |
| 2 | +import { Component, DebugElement } from '@angular/core'; |
| 3 | +import { By } from '@angular/platform-browser'; |
1 | 4 | 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 {} |
3 | 11 |
|
4 | 12 | 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 | + |
5 | 28 | it('should create an instance', () => {
|
6 | 29 | TestBed.runInInjectionContext(() => {
|
7 | 30 | const directive = new ModalToggleDirective();
|
8 | 31 | expect(directive).toBeTruthy();
|
9 | 32 | });
|
10 | 33 | });
|
| 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 | + })); |
11 | 44 | });
|
0 commit comments