format-time-delta.test.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { describe, expect, it, vi, beforeEach } from "vitest";
  2. import { formatTimeDelta } from "#/utils/format-time-delta";
  3. describe("formatTimeDelta", () => {
  4. beforeEach(() => {
  5. const now = new Date("2024-01-01T00:00:00Z");
  6. vi.useFakeTimers({ now });
  7. });
  8. it("formats the yearly time correctly", () => {
  9. const oneYearAgo = new Date("2023-01-01T00:00:00Z");
  10. expect(formatTimeDelta(oneYearAgo)).toBe("1 year");
  11. const twoYearsAgo = new Date("2022-01-01T00:00:00Z");
  12. expect(formatTimeDelta(twoYearsAgo)).toBe("2 years");
  13. const threeYearsAgo = new Date("2021-01-01T00:00:00Z");
  14. expect(formatTimeDelta(threeYearsAgo)).toBe("3 years");
  15. });
  16. it("formats the monthly time correctly", () => {
  17. const oneMonthAgo = new Date("2023-12-01T00:00:00Z");
  18. expect(formatTimeDelta(oneMonthAgo)).toBe("1 month");
  19. const twoMonthsAgo = new Date("2023-11-01T00:00:00Z");
  20. expect(formatTimeDelta(twoMonthsAgo)).toBe("2 months");
  21. const threeMonthsAgo = new Date("2023-10-01T00:00:00Z");
  22. expect(formatTimeDelta(threeMonthsAgo)).toBe("3 months");
  23. });
  24. it("formats the daily time correctly", () => {
  25. const oneDayAgo = new Date("2023-12-31T00:00:00Z");
  26. expect(formatTimeDelta(oneDayAgo)).toBe("1 day");
  27. const twoDaysAgo = new Date("2023-12-30T00:00:00Z");
  28. expect(formatTimeDelta(twoDaysAgo)).toBe("2 days");
  29. const threeDaysAgo = new Date("2023-12-29T00:00:00Z");
  30. expect(formatTimeDelta(threeDaysAgo)).toBe("3 days");
  31. });
  32. it("formats the hourly time correctly", () => {
  33. const oneHourAgo = new Date("2023-12-31T23:00:00Z");
  34. expect(formatTimeDelta(oneHourAgo)).toBe("1 hour");
  35. const twoHoursAgo = new Date("2023-12-31T22:00:00Z");
  36. expect(formatTimeDelta(twoHoursAgo)).toBe("2 hours");
  37. const threeHoursAgo = new Date("2023-12-31T21:00:00Z");
  38. expect(formatTimeDelta(threeHoursAgo)).toBe("3 hours");
  39. });
  40. it("formats the minute time correctly", () => {
  41. const oneMinuteAgo = new Date("2023-12-31T23:59:00Z");
  42. expect(formatTimeDelta(oneMinuteAgo)).toBe("1 minute");
  43. const twoMinutesAgo = new Date("2023-12-31T23:58:00Z");
  44. expect(formatTimeDelta(twoMinutesAgo)).toBe("2 minutes");
  45. const threeMinutesAgo = new Date("2023-12-31T23:57:00Z");
  46. expect(formatTimeDelta(threeMinutesAgo)).toBe("3 minutes");
  47. });
  48. it("formats the second time correctly", () => {
  49. const oneSecondAgo = new Date("2023-12-31T23:59:59Z");
  50. expect(formatTimeDelta(oneSecondAgo)).toBe("1 second");
  51. const twoSecondsAgo = new Date("2023-12-31T23:59:58Z");
  52. expect(formatTimeDelta(twoSecondsAgo)).toBe("2 seconds");
  53. const threeSecondsAgo = new Date("2023-12-31T23:59:57Z");
  54. expect(formatTimeDelta(threeSecondsAgo)).toBe("3 seconds");
  55. });
  56. });