Skip to content

Commit a7ddba0

Browse files
author
Nika Kolesnikova
committed
feat: export getAveragePurchaseInterval function and create a unit test for it
1 parent 77c1b19 commit a7ddba0

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/utils/dates.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ function getNextPurchaseEstimate(purchaseIntervals, totalPurchases) {
142142
* @param {number} estimatedDaysUntilPurchase - The estimated number of days until the next purchase.
143143
* @returns {number} The average purchase interval calculated from the provided intervals.
144144
*/
145-
function getAveragePurchaseInterval(
145+
export function getAveragePurchaseInterval(
146146
purchaseIntervals,
147147
estimatedDaysUntilPurchase,
148148
) {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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

Comments
 (0)