fix: add error handling to CashboxExportSubscriptionDialog

This commit is contained in:
Bastian Wagner
2026-08-04 08:39:10 +02:00
parent db061a7ef7
commit 148269da11
3 changed files with 54 additions and 5 deletions

View File

@@ -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 = ''" />

View File

@@ -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();
});
});

View File

@@ -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.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.');
},
});
}
}