refactor(contributions): tighten requiresPayment contract; use findLast

This commit is contained in:
Jennie Robinson Faber 2026-04-19 18:10:57 +01:00
parent 62c606b30a
commit 03eee45cbd
2 changed files with 6 additions and 5 deletions

View file

@ -8,7 +8,7 @@ export const CONTRIBUTION_PRESETS = [
{ amount: 50, label: "I want to sponsor multiple members" }, { 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) => export const isValidContributionAmount = (amount) =>
Number.isInteger(amount) && amount >= 0 Number.isInteger(amount) && amount >= 0
@ -17,6 +17,6 @@ export const getGuidanceLabel = (amount) => {
if (amount === null || amount === undefined) return null if (amount === null || amount === undefined) return null
const n = Number(amount) const n = Number(amount)
if (!Number.isFinite(n) || n < 0) return null 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 return match?.label ?? null
} }

View file

@ -26,9 +26,10 @@ describe('requiresPayment', () => {
expect(requiresPayment(1)).toBe(true) expect(requiresPayment(1)).toBe(true)
expect(requiresPayment(15)).toBe(true) expect(requiresPayment(15)).toBe(true)
}) })
it('coerces string numbers', () => { it('returns false for null, undefined, NaN', () => {
expect(requiresPayment('15')).toBe(true) expect(requiresPayment(null)).toBe(false)
expect(requiresPayment('0')).toBe(false) expect(requiresPayment(undefined)).toBe(false)
expect(requiresPayment(NaN)).toBe(false)
}) })
}) })