|
| 1 | +import { getUpcomingSubscriptions, _clearUpcomingCache } from '../dummyData'; |
| 2 | +import { Subscription, SubscriptionCategory, BillingCycle } from '../../types/subscription'; |
| 3 | + |
| 4 | +/** Helper to build a minimal Subscription for testing. */ |
| 5 | +const makeSub = ( |
| 6 | + overrides: Partial<Subscription> & { id: string; nextBillingDate: Date } |
| 7 | +): Subscription => ({ |
| 8 | + name: `Sub ${overrides.id}`, |
| 9 | + description: '', |
| 10 | + category: SubscriptionCategory.OTHER, |
| 11 | + price: 9.99, |
| 12 | + currency: 'USD', |
| 13 | + billingCycle: BillingCycle.MONTHLY, |
| 14 | + isActive: true, |
| 15 | + isCryptoEnabled: false, |
| 16 | + createdAt: new Date('2024-01-01'), |
| 17 | + updatedAt: new Date('2024-01-01'), |
| 18 | + ...overrides, |
| 19 | +}); |
| 20 | + |
| 21 | +const DAY_MS = 24 * 60 * 60 * 1000; |
| 22 | + |
| 23 | +describe('getUpcomingSubscriptions', () => { |
| 24 | + beforeAll(() => { |
| 25 | + jest.useFakeTimers(); |
| 26 | + jest.setSystemTime(new Date('2024-06-15T12:00:00Z')); |
| 27 | + }); |
| 28 | + |
| 29 | + afterAll(() => { |
| 30 | + jest.useRealTimers(); |
| 31 | + }); |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + _clearUpcomingCache(); |
| 35 | + }); |
| 36 | + |
| 37 | + const NOW = new Date('2024-06-15T12:00:00Z').getTime(); |
| 38 | + |
| 39 | + it('returns active subscriptions within the next 7 days', () => { |
| 40 | + const subs: Subscription[] = [ |
| 41 | + makeSub({ id: '1', nextBillingDate: new Date(NOW + 1 * DAY_MS) }), |
| 42 | + makeSub({ id: '2', nextBillingDate: new Date(NOW + 6 * DAY_MS) }), |
| 43 | + ]; |
| 44 | + |
| 45 | + const result = getUpcomingSubscriptions(subs); |
| 46 | + expect(result).toHaveLength(2); |
| 47 | + expect(result[0].id).toBe('1'); |
| 48 | + expect(result[1].id).toBe('2'); |
| 49 | + }); |
| 50 | + |
| 51 | + it('excludes inactive subscriptions', () => { |
| 52 | + const subs: Subscription[] = [ |
| 53 | + makeSub({ id: '1', nextBillingDate: new Date(NOW + 1 * DAY_MS), isActive: false }), |
| 54 | + makeSub({ id: '2', nextBillingDate: new Date(NOW + 2 * DAY_MS) }), |
| 55 | + ]; |
| 56 | + |
| 57 | + const result = getUpcomingSubscriptions(subs); |
| 58 | + expect(result).toHaveLength(1); |
| 59 | + expect(result[0].id).toBe('2'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('excludes subscriptions beyond 7 days', () => { |
| 63 | + const subs: Subscription[] = [ |
| 64 | + makeSub({ id: '1', nextBillingDate: new Date(NOW + 8 * DAY_MS) }), |
| 65 | + makeSub({ id: '2', nextBillingDate: new Date(NOW + 15 * DAY_MS) }), |
| 66 | + ]; |
| 67 | + |
| 68 | + const result = getUpcomingSubscriptions(subs); |
| 69 | + expect(result).toHaveLength(0); |
| 70 | + }); |
| 71 | + |
| 72 | + it('excludes subscriptions in the past', () => { |
| 73 | + const subs: Subscription[] = [ |
| 74 | + makeSub({ id: '1', nextBillingDate: new Date(NOW - 1 * DAY_MS) }), |
| 75 | + ]; |
| 76 | + |
| 77 | + const result = getUpcomingSubscriptions(subs); |
| 78 | + expect(result).toHaveLength(0); |
| 79 | + }); |
| 80 | + |
| 81 | + it('returns results sorted by billing date ascending', () => { |
| 82 | + const subs: Subscription[] = [ |
| 83 | + makeSub({ id: 'c', nextBillingDate: new Date(NOW + 5 * DAY_MS) }), |
| 84 | + makeSub({ id: 'a', nextBillingDate: new Date(NOW + 1 * DAY_MS) }), |
| 85 | + makeSub({ id: 'b', nextBillingDate: new Date(NOW + 3 * DAY_MS) }), |
| 86 | + ]; |
| 87 | + |
| 88 | + const result = getUpcomingSubscriptions(subs); |
| 89 | + expect(result.map((s) => s.id)).toEqual(['a', 'b', 'c']); |
| 90 | + }); |
| 91 | + |
| 92 | + it('includes subscriptions due exactly today (now)', () => { |
| 93 | + const subs: Subscription[] = [makeSub({ id: '1', nextBillingDate: new Date(NOW) })]; |
| 94 | + |
| 95 | + const result = getUpcomingSubscriptions(subs); |
| 96 | + expect(result).toHaveLength(1); |
| 97 | + }); |
| 98 | + |
| 99 | + it('includes subscriptions due exactly at the 7-day boundary', () => { |
| 100 | + const subs: Subscription[] = [ |
| 101 | + makeSub({ id: '1', nextBillingDate: new Date(NOW + 7 * DAY_MS) }), |
| 102 | + ]; |
| 103 | + |
| 104 | + const result = getUpcomingSubscriptions(subs); |
| 105 | + expect(result).toHaveLength(1); |
| 106 | + }); |
| 107 | + |
| 108 | + it('handles empty array', () => { |
| 109 | + expect(getUpcomingSubscriptions([])).toEqual([]); |
| 110 | + }); |
| 111 | + |
| 112 | + it('handles null / undefined gracefully', () => { |
| 113 | + expect(getUpcomingSubscriptions(null as unknown as Subscription[])).toEqual([]); |
| 114 | + expect(getUpcomingSubscriptions(undefined as unknown as Subscription[])).toEqual([]); |
| 115 | + }); |
| 116 | + |
| 117 | + it('returns cached result for the same input reference', () => { |
| 118 | + const subs: Subscription[] = [ |
| 119 | + makeSub({ id: '1', nextBillingDate: new Date(NOW + 1 * DAY_MS) }), |
| 120 | + ]; |
| 121 | + |
| 122 | + const first = getUpcomingSubscriptions(subs); |
| 123 | + const second = getUpcomingSubscriptions(subs); |
| 124 | + expect(first).toBe(second); // Same reference (===) means cache hit |
| 125 | + }); |
| 126 | + |
| 127 | + it('invalidates cache when input reference changes', () => { |
| 128 | + const subs1: Subscription[] = [ |
| 129 | + makeSub({ id: '1', nextBillingDate: new Date(NOW + 1 * DAY_MS) }), |
| 130 | + ]; |
| 131 | + const subs2: Subscription[] = [ |
| 132 | + makeSub({ id: '2', nextBillingDate: new Date(NOW + 2 * DAY_MS) }), |
| 133 | + ]; |
| 134 | + |
| 135 | + const first = getUpcomingSubscriptions(subs1); |
| 136 | + const second = getUpcomingSubscriptions(subs2); |
| 137 | + expect(first).not.toBe(second); |
| 138 | + expect(second[0].id).toBe('2'); |
| 139 | + }); |
| 140 | +}); |
0 commit comments