Пройдя много тем в StackOverflow и на других форумах, я сдаюсь, чтобы попытаться опубликовать мою проблему в виде вопроса.
У меня есть компонент, который использует диалог материалов для отображения предупреждений, таких как всплывающие окна подтверждения или всплывающие окна информации для моего приложения. Я создал компонент с именем AlertsComponent и использовал его в своих родительских компонентах везде, где я хочу показывать предупреждения. У меня есть своя модель для обработки информации. Все это работает нормально, но spec.ts (тестовый пример) не работает даже при событии create / initialize.
My AlertsComponent.ts:
import { Component, OnInit, Optional, Inject } from '@angular/core';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
import { AlertInfo } from 'src/Model/common/alert-info.model';
@Component({
selector: 'app-alerts',
templateUrl: './alerts.component.html',
styleUrls: ['./alerts.component.css']
})
export class AlertsComponent implements OnInit {
constructor(
private dialogRef: MatDialogRef<AlertsComponent>,
@Optional() @Inject(MAT_DIALOG_DATA) public alertInfo?: AlertInfo
) {
console.log('Alert Data: ' + JSON.stringify(alertInfo));
if (alertInfo.ConfirmPopup) {
alertInfo.Header = 'Confirm ?';
} else { alertInfo.Header = 'Alert'; }
this.dialogRef.disableClose = true;
}
ngOnInit() {
}
ConfirmResponse(response: boolean): void {
this.dialogRef.close(response);
}
CloseAlert() {
this.dialogRef.close();
}
}
Мой HTML выглядит так:
<div>
<h2 mat-dialog-title>{{alertInfo.Header}}</h2>
<hr/>
<mat-dialog-content>
<strong>{{alertInfo.Body}}</strong>
<br>
<br>
<!-- <strong>{{data}}</strong> -->
</mat-dialog-content>
<hr>
<mat-dialog-actions>
<div>
<ng-container *ngIf="alertInfo.ConfirmPopup; else alertOnly">
<button mat-button class="align-self-center" color="primary" class="button-space" (click)="ConfirmResponse(true);">YES</button>
<button mat-button class="align-self-center" color="primary" class="button-space" (click)="ConfirmResponse(false);">NO</button>
</ng-container>
<ng-template #alertOnly>
<button mat-button color="primary" class="button-space" (click)="CloseAlert();">OK</button>
</ng-template>
</div>
</mat-dialog-actions>
</div>
И мои спецификации:
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { AlertsComponent } from './alerts.component';
import { MatDialogRef, MAT_DIALOG_DATA, MatDialogModule } from '@angular/material';
import { AlertInfo } from 'src/Model/common/alert-info.model';
import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { BrowserDynamicTestingModule } from '@angular/platform-browser-dynamic/testing';
describe('AlertsComponent', () => {
let component: AlertsComponent;
let fixture: ComponentFixture<AlertsComponent>;
let mockDialogRef: MatDialogRef<AlertsComponent>;
let mockAlertInfoObj: AlertInfo;
// const MY_MAT_MOCK_TOKEN = new InjectionToken<AlertInfo>('Mock Injection Token', {
// providedIn: 'root',
// factory: () => new AlertInfo()
// });
@Component({
selector: 'app-alerts',
template: '<div><mat-dialog-content></mat-dialog-content></div>'
})
class MockAlertsComponent { }
mockDialogRef = TestBed.get(MatDialogRef);
mockAlertInfoObj = new AlertInfo();
mockAlertInfoObj.ConfirmPopup = false;
mockAlertInfoObj.Body = 'test alert';
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ AlertsComponent, MockAlertsComponent ],
imports: [MatDialogModule],
providers: [
{provide: MatDialogRef, useValue: mockDialogRef},
{provide: MAT_DIALOG_DATA, useValue: mockAlertInfoObj},
],
schemas: [ CUSTOM_ELEMENTS_SCHEMA ]
})
.compileComponents();
}));
TestBed.overrideModule(BrowserDynamicTestingModule, {
set: {
entryComponents: [AlertsComponent]
}
})
beforeEach(() => {
fixture = TestBed.createComponent(AlertsComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
Когда я запускаю «ng test», этот тестовый пример компонента завершается с ошибкой:
AlertsComponent encountered a declaration exception
Error: Cannot call Promise.then from within a sync test.
Error: Cannot call Promise.then from within a sync test.
at SyncTestZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.SyncTestZoneSpec.onScheduleTask (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:366:1)
at ZoneDelegate../node_modules/zone.js/dist/zone.js.ZoneDelegate.scheduleTask (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:404:1)
at Zone../node_modules/zone.js/dist/zone.js.Zone.scheduleTask (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:238:1)
at Zone../node_modules/zone.js/dist/zone.js.Zone.scheduleMicroTask (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:258:1)
at scheduleResolveOrReject (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:879:1)
at ZoneAwarePromise.then (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone.js:1012:1)
at ApplicationInitStatus.push../node_modules/@angular/core/fesm5/core.js.ApplicationInitStatus.runInitializers (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm5/core.js:15618:1)
at TestBedViewEngine.push../node_modules/@angular/core/fesm5/testing.js.TestBedViewEngine._initIfNeeded (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm5/testing.js:1702:59)
at TestBedViewEngine.push../node_modules/@angular/core/fesm5/testing.js.TestBedViewEngine.get (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm5/testing.js:1766:1)
at Function.push../node_modules/@angular/core/fesm5/testing.js.TestBedViewEngine.get (http://localhost:9876/_karma_webpack_/webpack:/node_modules/@angular/core/fesm5/testing.js:1551:1)
Я не знаю или не могу понять, где я делаю что-то не так или где? Может кто-нибудь, пожалуйста, помогите мне?