fix: Correct hours-to-days conversion in convertToNewDurationType (#27964)

Signed-off-by: Aritra Dey <[email protected]>
This commit is contained in:
Aritra Dey authored and GitHub committed 2026-02-15 21:01:00 +05:30
1 parent 773fab858f
commit 9d4cb08c55
2 files changed
+86 -1

No files matched your search

@@ -0,0 +1,85 @@
import { describe, it, expect } from "vitest";
import convertToNewDurationType, { MINUTES_IN_HOUR, MINUTES_IN_DAY, HOURS_IN_DAY } from "./convertToNewDurationType";
describe("convertToNewDurationType", () => {
describe("identity conversions", () => {
it("should return the same value when converting minutes to minutes", () => {
expect(convertToNewDurationType("minutes", "minutes", 120)).toBe(120);
});
it("should return the same value when converting hours to hours", () => {
expect(convertToNewDurationType("hours", "hours", 5)).toBe(5);
});
it("should return the same value when converting days to days", () => {
expect(convertToNewDurationType("days", "days", 3)).toBe(3);
});
});
describe("minutes to other types", () => {
it("should convert minutes to hours", () => {
expect(convertToNewDurationType("minutes", "hours", 120)).toBe(2);
expect(convertToNewDurationType("minutes", "hours", 60)).toBe(1);
});
it("should convert minutes to days", () => {
expect(convertToNewDurationType("minutes", "days", 2880)).toBe(2);
expect(convertToNewDurationType("minutes", "days", 1440)).toBe(1);
});
});
describe("hours to other types", () => {
it("should convert hours to minutes", () => {
expect(convertToNewDurationType("hours", "minutes", 2)).toBe(120);
expect(convertToNewDurationType("hours", "minutes", 1)).toBe(60);
});
it("should convert hours to days", () => {
expect(convertToNewDurationType("hours", "days", 24)).toBe(1);
expect(convertToNewDurationType("hours", "days", 48)).toBe(2);
});
});
describe("days to other types", () => {
it("should convert days to minutes", () => {
expect(convertToNewDurationType("days", "minutes", 1)).toBe(1440);
expect(convertToNewDurationType("days", "minutes", 2)).toBe(2880);
});
it("should convert days to hours", () => {
expect(convertToNewDurationType("days", "hours", 1)).toBe(24);
expect(convertToNewDurationType("days", "hours", 2)).toBe(48);
});
});
describe("rounding", () => {
it("should round up fractional results with Math.ceil", () => {
// 90 minutes = 1.5 hours, should ceil to 2
expect(convertToNewDurationType("minutes", "hours", 90)).toBe(2);
// 100 minutes = 0.069 days, should ceil to 1
expect(convertToNewDurationType("minutes", "days", 100)).toBe(1);
// 5 hours = 0.208 days, should ceil to 1
expect(convertToNewDurationType("hours", "days", 5)).toBe(1);
});
});
describe("zero values", () => {
it("should handle zero correctly for all conversions", () => {
expect(convertToNewDurationType("minutes", "hours", 0)).toBe(0);
expect(convertToNewDurationType("minutes", "days", 0)).toBe(0);
expect(convertToNewDurationType("hours", "minutes", 0)).toBe(0);
expect(convertToNewDurationType("hours", "days", 0)).toBe(0);
expect(convertToNewDurationType("days", "minutes", 0)).toBe(0);
expect(convertToNewDurationType("days", "hours", 0)).toBe(0);
});
});
describe("exported constants", () => {
it("should export correct constant values", () => {
expect(MINUTES_IN_HOUR).toBe(60);
expect(MINUTES_IN_DAY).toBe(1440);
expect(HOURS_IN_DAY).toBe(24);
});
});
});
+1 -1
View File
@@ -16,7 +16,7 @@ export default function convertToNewDurationType(
minutes_days: () => prevValue / MINUTES_IN_DAY,
hours_minutes: () => prevValue * MINUTES_IN_HOUR,
hours_hours: () => prevValue,
hours_days: () => prevValue * HOURS_IN_DAY,
hours_days: () => prevValue / HOURS_IN_DAY,
days_minutes: () => prevValue * MINUTES_IN_DAY,
days_hours: () => prevValue * HOURS_IN_DAY,
days_days: () => prevValue,