From a7ddba0bef3d9d5cf8ac7bd91096966bda8d4dde Mon Sep 17 00:00:00 2001 From: Nika Kolesnikova Date: Thu, 17 Oct 2024 18:23:49 -0400 Subject: [PATCH] feat: export getAveragePurchaseInterval function and create a unit test for it --- src/utils/dates.js | 2 +- tests/getAveragePurchaseInterval.test.js | 47 ++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 tests/getAveragePurchaseInterval.test.js diff --git a/src/utils/dates.js b/src/utils/dates.js index ec33940..daaaa56 100644 --- a/src/utils/dates.js +++ b/src/utils/dates.js @@ -142,7 +142,7 @@ function getNextPurchaseEstimate(purchaseIntervals, totalPurchases) { * @param {number} estimatedDaysUntilPurchase - The estimated number of days until the next purchase. * @returns {number} The average purchase interval calculated from the provided intervals. */ -function getAveragePurchaseInterval( +export function getAveragePurchaseInterval( purchaseIntervals, estimatedDaysUntilPurchase, ) { diff --git a/tests/getAveragePurchaseInterval.test.js b/tests/getAveragePurchaseInterval.test.js new file mode 100644 index 0000000..5c91b03 --- /dev/null +++ b/tests/getAveragePurchaseInterval.test.js @@ -0,0 +1,47 @@ +import { describe, it, expect } from 'vitest'; +import { getAveragePurchaseInterval } from '../src/utils/dates'; + +describe('getAveragePurchaseInterval function', () => { + it('correctly calculates average purchase intervals', () => { + const purchaseIntervals = { + lastEstimatedInterval: 4, + daysSinceLastPurchase: 6, + }; + const estimatedDaysUntilPurchase = 5; + const result = getAveragePurchaseInterval( + purchaseIntervals, + estimatedDaysUntilPurchase, + ); + expect(result).toBe(5); + }); + + it('handles zero values in the intervals', () => { + const purchaseIntervals = { + lastEstimatedInterval: 0, + daysSinceLastPurchase: 6, + }; + const estimatedDaysUntilPurchase = 5; + + const result = getAveragePurchaseInterval( + purchaseIntervals, + estimatedDaysUntilPurchase, + ); + + expect(result).toBeCloseTo(3.67, 2); + }); + + it('returns 0 when all intervals are zero', () => { + const purchaseIntervals = { + lastEstimatedInterval: 0, + daysSinceLastPurchase: 0, + }; + const estimatedDaysUntilPurchase = 0; + + const result = getAveragePurchaseInterval( + purchaseIntervals, + estimatedDaysUntilPurchase, + ); + + expect(result).toBe(0); + }); +});