-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7626d51
commit cf5d731
Showing
3 changed files
with
79 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; | ||
import { getFormattedFeesDuration } from "./getFormattedFeesDuration"; | ||
|
||
describe("getFormattedFeesDuration", () => { | ||
beforeEach(() => { | ||
vi.useFakeTimers(); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.useRealTimers(); | ||
}); | ||
|
||
it("should return only hours when less than 24 hours passed", () => { | ||
// Set current time to Wednesday 15:00 UTC | ||
const now = new Date("2024-03-20T15:00:00Z"); | ||
vi.setSystemTime(now); | ||
|
||
const result = getFormattedFeesDuration(); | ||
expect(result).toBe("15h"); | ||
}); | ||
|
||
it("should return days and hours when more than 24 hours passed", () => { | ||
// Set current time to Friday 15:00 UTC | ||
const now = new Date("2024-03-22T15:00:00Z"); | ||
vi.setSystemTime(now); | ||
|
||
const result = getFormattedFeesDuration(); | ||
expect(result).toBe("2d 15h"); | ||
}); | ||
|
||
it("should return only days when hours are 0", () => { | ||
// Set current time to Friday 00:00 UTC | ||
const now = new Date("2024-03-22T00:00:00Z"); | ||
vi.setSystemTime(now); | ||
|
||
const result = getFormattedFeesDuration(); | ||
expect(result).toBe("2d"); | ||
}); | ||
|
||
it("should return minimum 1h when less than an hour passed", () => { | ||
// Set current time to Wednesday 00:30 UTC | ||
const now = new Date("2024-03-20T00:30:00Z"); | ||
vi.setSystemTime(now); | ||
|
||
const result = getFormattedFeesDuration(); | ||
expect(result).toBe("1h"); | ||
}); | ||
|
||
it("should start counting from Wednesday 00:00 UTC", () => { | ||
// Set current time to exact Wednesday 00:00 UTC | ||
const now = new Date("2024-03-20T00:00:00Z"); | ||
vi.setSystemTime(now); | ||
|
||
const result = getFormattedFeesDuration(); | ||
expect(result).toBe("1h"); // Minimum 1h even at start | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { differenceInDays, differenceInHours } from "date-fns"; | ||
import { getCurrentEpochStartedTimestamp } from "domain/stats"; | ||
|
||
export function getFormattedFeesDuration() { | ||
const epochStartedTimestamp = getCurrentEpochStartedTimestamp(); | ||
|
||
const now = new Date(); | ||
const epochStartedDate = new Date(epochStartedTimestamp * 1000); | ||
const days = differenceInDays(now, epochStartedDate); | ||
let restHours = differenceInHours(now, epochStartedDate, { roundingMethod: "round" }) - days * 24; | ||
if (days === 0) { | ||
restHours = Math.max(restHours, 1); | ||
} | ||
|
||
const daysStr = days > 0 ? `${days}d` : ""; | ||
const hoursStr = restHours > 0 ? `${restHours}h` : ""; | ||
return [daysStr, hoursStr].filter(Boolean).join(" "); | ||
} |