|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import { getAveragePurchaseInterval } from '../src/utils/dates'; |
| 3 | + |
| 4 | +describe('getAveragePurchaseInterval function', () => { |
| 5 | + it('correctly calculates average purchase intervals', () => { |
| 6 | + const purchaseIntervals = { |
| 7 | + lastEstimatedInterval: 4, |
| 8 | + daysSinceLastPurchase: 6, |
| 9 | + }; |
| 10 | + const estimatedDaysUntilPurchase = 5; |
| 11 | + const result = getAveragePurchaseInterval( |
| 12 | + purchaseIntervals, |
| 13 | + estimatedDaysUntilPurchase, |
| 14 | + ); |
| 15 | + expect(result).toBe(5); |
| 16 | + }); |
| 17 | + |
| 18 | + it('handles zero values in the intervals', () => { |
| 19 | + const purchaseIntervals = { |
| 20 | + lastEstimatedInterval: 0, |
| 21 | + daysSinceLastPurchase: 6, |
| 22 | + }; |
| 23 | + const estimatedDaysUntilPurchase = 5; |
| 24 | + |
| 25 | + const result = getAveragePurchaseInterval( |
| 26 | + purchaseIntervals, |
| 27 | + estimatedDaysUntilPurchase, |
| 28 | + ); |
| 29 | + |
| 30 | + expect(result).toBeCloseTo(3.67, 2); |
| 31 | + }); |
| 32 | + |
| 33 | + it('returns 0 when all intervals are zero', () => { |
| 34 | + const purchaseIntervals = { |
| 35 | + lastEstimatedInterval: 0, |
| 36 | + daysSinceLastPurchase: 0, |
| 37 | + }; |
| 38 | + const estimatedDaysUntilPurchase = 0; |
| 39 | + |
| 40 | + const result = getAveragePurchaseInterval( |
| 41 | + purchaseIntervals, |
| 42 | + estimatedDaysUntilPurchase, |
| 43 | + ); |
| 44 | + |
| 45 | + expect(result).toBe(0); |
| 46 | + }); |
| 47 | +}); |
0 commit comments