const { spotPrice, sponsorshipCharge, poolSummary } = require('../src/utils/sponsorship'); describe('spotPrice', () => { test('is the cheapest paid main ticket', () => { expect(spotPrice({ price: 0, eventOptions: [ { isMainTicket: true, price: 450 }, { isMainTicket: true, price: 300 }, { isMainTicket: false, price: 50 }, { isMainTicket: true, price: 0 }, ] })).toBe(300); }); test('falls back to the event price, or null for free events', () => { expect(spotPrice({ price: 200, eventOptions: [] })).toBe(200); expect(spotPrice({ price: 0, eventOptions: [{ isMainTicket: true, price: 0 }] })).toBeNull(); }); }); describe('sponsorshipCharge', () => { test('whole spots cost the spot price each', () => { expect(sponsorshipCharge({ spots: 3 }, 450)).toEqual({ amount: 1350, spots: 3 }); }); test('a custom amount is taken as given (2 decimals)', () => { expect(sponsorshipCharge({ amount: '120.555' }, 450)).toEqual({ amount: 120.56, spots: null }); }); test('rejects bad input', () => { expect(() => sponsorshipCharge({ spots: 0 }, 450)).toThrow(/between 1/); expect(() => sponsorshipCharge({ spots: 1.5 }, 450)).toThrow(); expect(() => sponsorshipCharge({ spots: 1 }, null)).toThrow(/no ticket price/); expect(() => sponsorshipCharge({ amount: 10 }, 450)).toThrow(/minimum/); expect(() => sponsorshipCharge({}, 450)).toThrow(); }); }); describe('poolSummary', () => { test('received, assigned and remaining, with spots funded', () => { const sponsorships = [{ id: 'a', amount: 900 }, { id: 'b', amount: 300 }]; const legs = [{ originalPaymentId: 'a', amount: 450 }, { originalPaymentId: 'b', amount: -100 }]; expect(poolSummary(sponsorships, legs, 450)).toEqual({ received: 1200, assigned: 550, remaining: 650, sponsors: 2, spotsFunded: 2 }); }); test('no spot count for free events', () => { expect(poolSummary([{ id: 'a', amount: 50 }], [], null).spotsFunded).toBeNull(); }); });