Skip to content

Commit

Permalink
feat: export getAveragePurchaseInterval function and create a unit te…
Browse files Browse the repository at this point in the history
…st for it
  • Loading branch information
kweeuhree committed Oct 17, 2024
1 parent 77c1b19 commit a7ddba0
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/utils/dates.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {
Expand Down
47 changes: 47 additions & 0 deletions tests/getAveragePurchaseInterval.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit a7ddba0

Please sign in to comment.