Comment écrire des tests unitaires pour alerter composant
Je suis nouveau à l'écriture de scénarios de test unitaire. Je suis en train d'écrire des cas de test pour mon alerte composant. Je suis à l'aide angulaire 7. Mon alerte composant et mon alerte modèle de code comme ci-dessous.
Mon alerte.composante.ts
import { Component, OnInit, OnDestroy, Input } from '@angular/core';
import { Alert, AlertType } from './models/alert.model';
import { AlertService } from './service/alert.service';
import { Subscription } from 'rxjs';
@Component({
selector: 'app-alert',
templateUrl: './alert.component.html',
styleUrls: ['./alert.component.css']
})
export class AlertComponent implements OnInit, OnDestroy {
alerts: Alert[] = [];
subscription: Subscription;
constructor(private alertService: AlertService) {}
ngOnInit() {
this.subscription = this.alertService.onAlert().subscribe(alert => {
if (!alert.message) {
this.alerts = [];
return;
}
this.alerts.push(alert);
});
}
ngOnDestroy() {
this.subscription.unsubscribe();
}
removeAlert(alert: Alert) {
this.alerts = this.alerts.filter(x => x !== alert);
}
cssClass(alert: Alert) {
if (!alert) {
return;
}
// return css class based on alert type
switch (alert.type) {
case AlertType.Success:
return 'alert alert-success';
case AlertType.Error:
return 'alert alert-danger';
case AlertType.Info:
return 'alert alert-info';
case AlertType.Warning:
return 'alert alert-warning';
}
}
}
Mon alerte.de modèle.ts
export class Alert {
constructor(public message: string, public type: AlertType) {}
}
export enum AlertType {
Success,
Error,
Info,
Warning
}
0
0 réponses
Questions connexes
Voir d'autres questions sur les étiquettes angular unit-testing