fix: add error handling to CashboxExportSubscriptionDialog
This commit is contained in:
@@ -1,5 +1,11 @@
|
||||
<h2 mat-dialog-title>Automatischen Versand einrichten</h2>
|
||||
<mat-dialog-content>
|
||||
@if (loadError()) {
|
||||
<p class="error">{{ loadError() }}</p>
|
||||
}
|
||||
@if (saveError()) {
|
||||
<p class="error">{{ saveError() }}</p>
|
||||
}
|
||||
<mat-form-field appearance="outline">
|
||||
<mat-label>E-Mail-Adresse hinzufügen</mat-label>
|
||||
<input matInput #recipientInput (keydown.enter)="addRecipient(recipientInput.value); recipientInput.value = ''" />
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
|
||||
import { of } from 'rxjs';
|
||||
import { of, throwError } from 'rxjs';
|
||||
import { CashboxExportApi } from '../../../../core/team/cashbox-export-api';
|
||||
import { CashboxExportSubscriptionDialog } from './cashbox-export-subscription-dialog';
|
||||
|
||||
@@ -59,4 +59,34 @@ describe('CashboxExportSubscriptionDialog', () => {
|
||||
fixture.componentInstance['removeRecipient']('a@example.com');
|
||||
expect(fixture.componentInstance['recipients']()).toEqual([]);
|
||||
});
|
||||
|
||||
it('handles getSubscription error by setting loadError', async () => {
|
||||
getSubscription.mockReturnValueOnce(throwError(() => new Error('API error')));
|
||||
|
||||
await TestBed.resetTestingModule();
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [CashboxExportSubscriptionDialog],
|
||||
providers: [
|
||||
{ provide: MAT_DIALOG_DATA, useValue: { teamId: 5 } },
|
||||
{ provide: MatDialogRef, useValue: dialogRef },
|
||||
{ provide: CashboxExportApi, useValue: { getSubscription, updateSubscription } },
|
||||
],
|
||||
}).compileComponents();
|
||||
|
||||
fixture = TestBed.createComponent(CashboxExportSubscriptionDialog);
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(fixture.componentInstance['loadError']()).toBe('Einstellungen konnten nicht geladen werden.');
|
||||
});
|
||||
|
||||
it('handles updateSubscription error by setting saveError and not closing dialog', () => {
|
||||
updateSubscription.mockReturnValueOnce(throwError(() => new Error('API error')));
|
||||
dialogRef.close.mockClear();
|
||||
|
||||
fixture.componentInstance['addRecipient']('b@example.com');
|
||||
fixture.componentInstance['save']();
|
||||
|
||||
expect(fixture.componentInstance['saveError']()).toBe('Speichern fehlgeschlagen.');
|
||||
expect(dialogRef.close).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -35,15 +35,22 @@ export class CashboxExportSubscriptionDialog {
|
||||
private readonly api = inject(CashboxExportApi);
|
||||
|
||||
protected readonly recipients = signal<string[]>([]);
|
||||
protected readonly loadError = signal<string | null>(null);
|
||||
protected readonly saveError = signal<string | null>(null);
|
||||
protected readonly form = this.formBuilder.nonNullable.group({
|
||||
interval: ['monthly' as RecurringTransactionInterval, Validators.required],
|
||||
active: [false],
|
||||
});
|
||||
|
||||
constructor() {
|
||||
this.api.getSubscription(this.data.teamId).subscribe((subscription) => {
|
||||
this.recipients.set(subscription.recipients);
|
||||
this.form.setValue({ interval: subscription.interval, active: subscription.active });
|
||||
this.api.getSubscription(this.data.teamId).subscribe({
|
||||
next: (subscription) => {
|
||||
this.recipients.set(subscription.recipients);
|
||||
this.form.setValue({ interval: subscription.interval, active: subscription.active });
|
||||
},
|
||||
error: () => {
|
||||
this.loadError.set('Einstellungen konnten nicht geladen werden.');
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -59,9 +66,15 @@ export class CashboxExportSubscriptionDialog {
|
||||
|
||||
protected save(): void {
|
||||
if (this.form.invalid) return;
|
||||
this.saveError.set(null);
|
||||
const { interval, active } = this.form.getRawValue();
|
||||
this.api
|
||||
.updateSubscription(this.data.teamId, { recipients: this.recipients(), interval, active })
|
||||
.subscribe(() => this.dialogRef.close());
|
||||
.subscribe({
|
||||
next: () => this.dialogRef.close(),
|
||||
error: () => {
|
||||
this.saveError.set('Speichern fehlgeschlagen.');
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user