fix: add error handling to CashboxExportSubscriptionDialog
This commit is contained in:
@@ -1,5 +1,11 @@
|
|||||||
<h2 mat-dialog-title>Automatischen Versand einrichten</h2>
|
<h2 mat-dialog-title>Automatischen Versand einrichten</h2>
|
||||||
<mat-dialog-content>
|
<mat-dialog-content>
|
||||||
|
@if (loadError()) {
|
||||||
|
<p class="error">{{ loadError() }}</p>
|
||||||
|
}
|
||||||
|
@if (saveError()) {
|
||||||
|
<p class="error">{{ saveError() }}</p>
|
||||||
|
}
|
||||||
<mat-form-field appearance="outline">
|
<mat-form-field appearance="outline">
|
||||||
<mat-label>E-Mail-Adresse hinzufügen</mat-label>
|
<mat-label>E-Mail-Adresse hinzufügen</mat-label>
|
||||||
<input matInput #recipientInput (keydown.enter)="addRecipient(recipientInput.value); recipientInput.value = ''" />
|
<input matInput #recipientInput (keydown.enter)="addRecipient(recipientInput.value); recipientInput.value = ''" />
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||||
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
|
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 { CashboxExportApi } from '../../../../core/team/cashbox-export-api';
|
||||||
import { CashboxExportSubscriptionDialog } from './cashbox-export-subscription-dialog';
|
import { CashboxExportSubscriptionDialog } from './cashbox-export-subscription-dialog';
|
||||||
|
|
||||||
@@ -59,4 +59,34 @@ describe('CashboxExportSubscriptionDialog', () => {
|
|||||||
fixture.componentInstance['removeRecipient']('a@example.com');
|
fixture.componentInstance['removeRecipient']('a@example.com');
|
||||||
expect(fixture.componentInstance['recipients']()).toEqual([]);
|
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);
|
private readonly api = inject(CashboxExportApi);
|
||||||
|
|
||||||
protected readonly recipients = signal<string[]>([]);
|
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({
|
protected readonly form = this.formBuilder.nonNullable.group({
|
||||||
interval: ['monthly' as RecurringTransactionInterval, Validators.required],
|
interval: ['monthly' as RecurringTransactionInterval, Validators.required],
|
||||||
active: [false],
|
active: [false],
|
||||||
});
|
});
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.api.getSubscription(this.data.teamId).subscribe((subscription) => {
|
this.api.getSubscription(this.data.teamId).subscribe({
|
||||||
|
next: (subscription) => {
|
||||||
this.recipients.set(subscription.recipients);
|
this.recipients.set(subscription.recipients);
|
||||||
this.form.setValue({ interval: subscription.interval, active: subscription.active });
|
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 {
|
protected save(): void {
|
||||||
if (this.form.invalid) return;
|
if (this.form.invalid) return;
|
||||||
|
this.saveError.set(null);
|
||||||
const { interval, active } = this.form.getRawValue();
|
const { interval, active } = this.form.getRawValue();
|
||||||
this.api
|
this.api
|
||||||
.updateSubscription(this.data.teamId, { recipients: this.recipients(), interval, active })
|
.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