diff --git a/resources/js/admin/__tests__/api.test.ts b/resources/js/admin/__tests__/api.test.ts new file mode 100644 index 0000000..c120e22 --- /dev/null +++ b/resources/js/admin/__tests__/api.test.ts @@ -0,0 +1,19 @@ +import { describe, expect, it } from 'vitest'; +import { normalizeTenantPackage } from '../api'; + +describe('normalizeTenantPackage', () => { + it('keeps remaining_events null when payload is null', () => { + const normalized = normalizeTenantPackage({ id: 1, remaining_events: null, used_events: 0 } as any); + expect(normalized.remaining_events).toBeNull(); + }); + + it('keeps remaining_events null when payload is missing', () => { + const normalized = normalizeTenantPackage({ id: 1, used_events: 0 } as any); + expect(normalized.remaining_events).toBeNull(); + }); + + it('coerces remaining_events to number when provided', () => { + const normalized = normalizeTenantPackage({ id: 1, remaining_events: '2', used_events: 0 } as any); + expect(normalized.remaining_events).toBe(2); + }); +}); diff --git a/resources/js/admin/api.ts b/resources/js/admin/api.ts index c578c91..a21b097 100644 --- a/resources/js/admin/api.ts +++ b/resources/js/admin/api.ts @@ -1011,7 +1011,7 @@ function normalizeDashboard(payload: JsonValue | null): DashboardSummary | null }; } -function normalizeTenantPackage(pkg: JsonValue): TenantPackageSummary { +export function normalizeTenantPackage(pkg: JsonValue): TenantPackageSummary { const packageData = pkg.package ?? {}; return { id: Number(pkg.id ?? 0), @@ -1031,7 +1031,8 @@ function normalizeTenantPackage(pkg: JsonValue): TenantPackageSummary { : null, active: Boolean(pkg.active ?? false), used_events: Number(pkg.used_events ?? 0), - remaining_events: pkg.remaining_events !== undefined ? Number(pkg.remaining_events) : null, + remaining_events: + pkg.remaining_events === undefined || pkg.remaining_events === null ? null : Number(pkg.remaining_events), price: packageData.price !== undefined ? Number(packageData.price) : pkg.price ?? null, currency: packageData.currency ?? pkg.currency ?? 'EUR', purchased_at: pkg.purchased_at ?? pkg.created_at ?? null,