|
| 1 | +import { Component, ViewChild } from '@angular/core'; |
1 | 2 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; |
2 | | -import { MatDialogModule } from '@angular/material/dialog'; |
| 3 | +import { MatDialog, MatDialogModule } from '@angular/material/dialog'; |
| 4 | +import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; |
| 5 | +import { Observable, of } from 'rxjs'; |
3 | 6 |
|
4 | 7 | import { AdminEventFormPreviewComponent } from './admin-event-form-preview.component'; |
5 | 8 |
|
| 9 | +@Component({ |
| 10 | + template: '<admin-event-form-preview [formDefinition]="formDefinition"></admin-event-form-preview>' |
| 11 | +}) |
| 12 | +class TestHostComponent { |
| 13 | + formDefinition: any |
| 14 | + |
| 15 | + @ViewChild(AdminEventFormPreviewComponent, { static: true }) |
| 16 | + public adminEventFormPreviewComponent: AdminEventFormPreviewComponent |
| 17 | +} |
| 18 | + |
| 19 | +class MatDialogMock { |
| 20 | + open(): any { |
| 21 | + return { |
| 22 | + afterClosed: (): Observable<boolean> => of(true) |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | + |
6 | 27 | describe('AdminEventFormPreviewComponent', () => { |
7 | | - let component: AdminEventFormPreviewComponent; |
8 | | - let fixture: ComponentFixture<AdminEventFormPreviewComponent>; |
| 28 | + let component: AdminEventFormPreviewComponent |
| 29 | + let hostComponent: TestHostComponent |
| 30 | + let fixture: ComponentFixture<TestHostComponent> |
| 31 | + let dialog: MatDialog |
9 | 32 |
|
10 | 33 | beforeEach(async(() => { |
11 | 34 | TestBed.configureTestingModule({ |
12 | | - imports: [MatDialogModule], |
13 | | - declarations: [ AdminEventFormPreviewComponent ] |
| 35 | + imports: [BrowserAnimationsModule, MatDialogModule], |
| 36 | + declarations: [AdminEventFormPreviewComponent, TestHostComponent], |
| 37 | + providers: [{ |
| 38 | + provide: MatDialog, useClass: MatDialogMock |
| 39 | + }] |
14 | 40 | }) |
15 | | - .compileComponents(); |
| 41 | + .compileComponents() |
16 | 42 | })); |
17 | 43 |
|
18 | 44 | beforeEach(() => { |
19 | | - fixture = TestBed.createComponent(AdminEventFormPreviewComponent); |
20 | | - component = fixture.componentInstance; |
21 | | - fixture.detectChanges(); |
| 45 | + fixture = TestBed.createComponent(TestHostComponent) |
| 46 | + hostComponent = fixture.componentInstance |
| 47 | + component = hostComponent.adminEventFormPreviewComponent |
| 48 | + dialog = TestBed.inject(MatDialog) |
| 49 | + fixture.detectChanges() |
22 | 50 | }); |
23 | 51 |
|
24 | 52 | it('should create', () => { |
25 | 53 | expect(component).toBeTruthy(); |
26 | 54 | }); |
| 55 | + |
| 56 | + it('should open dialog', () => { |
| 57 | + spyOn(dialog, 'open').and.callThrough() |
| 58 | + |
| 59 | + hostComponent.formDefinition = { |
| 60 | + fields: [] |
| 61 | + } |
| 62 | + |
| 63 | + fixture.detectChanges() |
| 64 | + |
| 65 | + expect(dialog.open).toHaveBeenCalled() |
| 66 | + }); |
| 67 | + |
| 68 | + it('should close dialog', async(async () => { |
| 69 | + spyOn(component.onClose, 'emit') |
| 70 | + spyOn(component.dialog, 'open').and.callThrough() |
| 71 | + |
| 72 | + hostComponent.formDefinition = { |
| 73 | + name: 'Mock Form 1', |
| 74 | + fields: [] |
| 75 | + } |
| 76 | + |
| 77 | + fixture.detectChanges() |
| 78 | + |
| 79 | + expect(dialog.open).toHaveBeenCalled() |
| 80 | + expect(component.onClose.emit).toHaveBeenCalled() |
| 81 | + })); |
27 | 82 | }); |
0 commit comments