From 03eee45cbd172320835f7d4adf7619b4577e1f37 Mon Sep 17 00:00:00 2001 From: Jennie Robinson Faber Date: Sun, 19 Apr 2026 18:10:57 +0100 Subject: [PATCH] refactor(contributions): tighten requiresPayment contract; use findLast --- app/config/contributions.js | 4 ++-- tests/client/contributions.test.js | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/app/config/contributions.js b/app/config/contributions.js index 7d8cab2..73005f1 100644 --- a/app/config/contributions.js +++ b/app/config/contributions.js @@ -8,7 +8,7 @@ export const CONTRIBUTION_PRESETS = [ { amount: 50, label: "I want to sponsor multiple members" }, ] -export const requiresPayment = (amount) => Number(amount) > 0 +export const requiresPayment = (amount) => amount > 0 export const isValidContributionAmount = (amount) => Number.isInteger(amount) && amount >= 0 @@ -17,6 +17,6 @@ export const getGuidanceLabel = (amount) => { if (amount === null || amount === undefined) return null const n = Number(amount) if (!Number.isFinite(n) || n < 0) return null - const match = [...CONTRIBUTION_PRESETS].reverse().find(p => p.amount <= n) + const match = CONTRIBUTION_PRESETS.findLast(p => p.amount <= n) return match?.label ?? null } diff --git a/tests/client/contributions.test.js b/tests/client/contributions.test.js index fff5db4..7bda7a3 100644 --- a/tests/client/contributions.test.js +++ b/tests/client/contributions.test.js @@ -26,9 +26,10 @@ describe('requiresPayment', () => { expect(requiresPayment(1)).toBe(true) expect(requiresPayment(15)).toBe(true) }) - it('coerces string numbers', () => { - expect(requiresPayment('15')).toBe(true) - expect(requiresPayment('0')).toBe(false) + it('returns false for null, undefined, NaN', () => { + expect(requiresPayment(null)).toBe(false) + expect(requiresPayment(undefined)).toBe(false) + expect(requiresPayment(NaN)).toBe(false) }) })