feat(contributions): rewrite client config as preset-based helpers
This commit is contained in:
parent
4da0265935
commit
62c606b30a
2 changed files with 96 additions and 19 deletions
77
tests/client/contributions.test.js
Normal file
77
tests/client/contributions.test.js
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
import { describe, it, expect } from 'vitest'
|
||||
import {
|
||||
CONTRIBUTION_PRESETS,
|
||||
requiresPayment,
|
||||
isValidContributionAmount,
|
||||
getGuidanceLabel,
|
||||
} from '../../app/config/contributions.js'
|
||||
|
||||
describe('CONTRIBUTION_PRESETS', () => {
|
||||
it('exposes the five suggested amounts with guidance labels', () => {
|
||||
expect(CONTRIBUTION_PRESETS).toEqual([
|
||||
{ amount: 0, label: 'I need support right now' },
|
||||
{ amount: 5, label: 'I can contribute' },
|
||||
{ amount: 15, label: 'I can sustain the community' },
|
||||
{ amount: 30, label: 'I can support others too' },
|
||||
{ amount: 50, label: 'I want to sponsor multiple members' },
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe('requiresPayment', () => {
|
||||
it('returns false for 0', () => {
|
||||
expect(requiresPayment(0)).toBe(false)
|
||||
})
|
||||
it('returns true for any positive number', () => {
|
||||
expect(requiresPayment(1)).toBe(true)
|
||||
expect(requiresPayment(15)).toBe(true)
|
||||
})
|
||||
it('coerces string numbers', () => {
|
||||
expect(requiresPayment('15')).toBe(true)
|
||||
expect(requiresPayment('0')).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('isValidContributionAmount', () => {
|
||||
it('accepts non-negative integers', () => {
|
||||
expect(isValidContributionAmount(0)).toBe(true)
|
||||
expect(isValidContributionAmount(15)).toBe(true)
|
||||
expect(isValidContributionAmount(9999)).toBe(true)
|
||||
})
|
||||
it('rejects negatives', () => {
|
||||
expect(isValidContributionAmount(-1)).toBe(false)
|
||||
})
|
||||
it('rejects non-integers and non-numbers', () => {
|
||||
expect(isValidContributionAmount(1.5)).toBe(false)
|
||||
expect(isValidContributionAmount('15')).toBe(false)
|
||||
expect(isValidContributionAmount(NaN)).toBe(false)
|
||||
expect(isValidContributionAmount(null)).toBe(false)
|
||||
expect(isValidContributionAmount(undefined)).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('getGuidanceLabel', () => {
|
||||
it('returns the exact preset label when amount matches a preset', () => {
|
||||
expect(getGuidanceLabel(0)).toBe('I need support right now')
|
||||
expect(getGuidanceLabel(5)).toBe('I can contribute')
|
||||
expect(getGuidanceLabel(15)).toBe('I can sustain the community')
|
||||
expect(getGuidanceLabel(30)).toBe('I can support others too')
|
||||
expect(getGuidanceLabel(50)).toBe('I want to sponsor multiple members')
|
||||
})
|
||||
it('returns the nearest-lower preset label for between-preset values', () => {
|
||||
expect(getGuidanceLabel(7)).toBe('I can contribute')
|
||||
expect(getGuidanceLabel(14)).toBe('I can contribute')
|
||||
expect(getGuidanceLabel(40)).toBe('I can support others too')
|
||||
expect(getGuidanceLabel(100)).toBe('I want to sponsor multiple members')
|
||||
})
|
||||
it('coerces string input', () => {
|
||||
expect(getGuidanceLabel('15')).toBe('I can sustain the community')
|
||||
})
|
||||
it('returns null for invalid input', () => {
|
||||
expect(getGuidanceLabel(-1)).toBeNull()
|
||||
expect(getGuidanceLabel(NaN)).toBeNull()
|
||||
expect(getGuidanceLabel('abc')).toBeNull()
|
||||
expect(getGuidanceLabel(null)).toBeNull()
|
||||
expect(getGuidanceLabel(undefined)).toBeNull()
|
||||
})
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue