Skip to content

Commit 9d4cb08

Browse files
fix: Correct hours-to-days conversion in convertToNewDurationType (#27964)
Signed-off-by: Aritra Dey <adey01027@gmail.com>
1 parent 773fab8 commit 9d4cb08

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { describe, it, expect } from "vitest";
2+
3+
import convertToNewDurationType, { MINUTES_IN_HOUR, MINUTES_IN_DAY, HOURS_IN_DAY } from "./convertToNewDurationType";
4+
5+
describe("convertToNewDurationType", () => {
6+
describe("identity conversions", () => {
7+
it("should return the same value when converting minutes to minutes", () => {
8+
expect(convertToNewDurationType("minutes", "minutes", 120)).toBe(120);
9+
});
10+
11+
it("should return the same value when converting hours to hours", () => {
12+
expect(convertToNewDurationType("hours", "hours", 5)).toBe(5);
13+
});
14+
15+
it("should return the same value when converting days to days", () => {
16+
expect(convertToNewDurationType("days", "days", 3)).toBe(3);
17+
});
18+
});
19+
20+
describe("minutes to other types", () => {
21+
it("should convert minutes to hours", () => {
22+
expect(convertToNewDurationType("minutes", "hours", 120)).toBe(2);
23+
expect(convertToNewDurationType("minutes", "hours", 60)).toBe(1);
24+
});
25+
26+
it("should convert minutes to days", () => {
27+
expect(convertToNewDurationType("minutes", "days", 2880)).toBe(2);
28+
expect(convertToNewDurationType("minutes", "days", 1440)).toBe(1);
29+
});
30+
});
31+
32+
describe("hours to other types", () => {
33+
it("should convert hours to minutes", () => {
34+
expect(convertToNewDurationType("hours", "minutes", 2)).toBe(120);
35+
expect(convertToNewDurationType("hours", "minutes", 1)).toBe(60);
36+
});
37+
38+
it("should convert hours to days", () => {
39+
expect(convertToNewDurationType("hours", "days", 24)).toBe(1);
40+
expect(convertToNewDurationType("hours", "days", 48)).toBe(2);
41+
});
42+
});
43+
44+
describe("days to other types", () => {
45+
it("should convert days to minutes", () => {
46+
expect(convertToNewDurationType("days", "minutes", 1)).toBe(1440);
47+
expect(convertToNewDurationType("days", "minutes", 2)).toBe(2880);
48+
});
49+
50+
it("should convert days to hours", () => {
51+
expect(convertToNewDurationType("days", "hours", 1)).toBe(24);
52+
expect(convertToNewDurationType("days", "hours", 2)).toBe(48);
53+
});
54+
});
55+
56+
describe("rounding", () => {
57+
it("should round up fractional results with Math.ceil", () => {
58+
// 90 minutes = 1.5 hours, should ceil to 2
59+
expect(convertToNewDurationType("minutes", "hours", 90)).toBe(2);
60+
// 100 minutes = 0.069 days, should ceil to 1
61+
expect(convertToNewDurationType("minutes", "days", 100)).toBe(1);
62+
// 5 hours = 0.208 days, should ceil to 1
63+
expect(convertToNewDurationType("hours", "days", 5)).toBe(1);
64+
});
65+
});
66+
67+
describe("zero values", () => {
68+
it("should handle zero correctly for all conversions", () => {
69+
expect(convertToNewDurationType("minutes", "hours", 0)).toBe(0);
70+
expect(convertToNewDurationType("minutes", "days", 0)).toBe(0);
71+
expect(convertToNewDurationType("hours", "minutes", 0)).toBe(0);
72+
expect(convertToNewDurationType("hours", "days", 0)).toBe(0);
73+
expect(convertToNewDurationType("days", "minutes", 0)).toBe(0);
74+
expect(convertToNewDurationType("days", "hours", 0)).toBe(0);
75+
});
76+
});
77+
78+
describe("exported constants", () => {
79+
it("should export correct constant values", () => {
80+
expect(MINUTES_IN_HOUR).toBe(60);
81+
expect(MINUTES_IN_DAY).toBe(1440);
82+
expect(HOURS_IN_DAY).toBe(24);
83+
});
84+
});
85+
});

packages/lib/convertToNewDurationType.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default function convertToNewDurationType(
1616
minutes_days: () => prevValue / MINUTES_IN_DAY,
1717
hours_minutes: () => prevValue * MINUTES_IN_HOUR,
1818
hours_hours: () => prevValue,
19-
hours_days: () => prevValue * HOURS_IN_DAY,
19+
hours_days: () => prevValue / HOURS_IN_DAY,
2020
days_minutes: () => prevValue * MINUTES_IN_DAY,
2121
days_hours: () => prevValue * HOURS_IN_DAY,
2222
days_days: () => prevValue,

0 commit comments

Comments
 (0)