|
| 1 | +import { describe, test, expect } from "bun:test"; |
| 2 | +import { formatRelativeTime } from "./dateTime"; |
| 3 | + |
| 4 | +describe("formatRelativeTime", () => { |
| 5 | + test("should return 'just now' for very recent timestamps", () => { |
| 6 | + const now = Date.now(); |
| 7 | + expect(formatRelativeTime(now)).toBe("just now"); |
| 8 | + expect(formatRelativeTime(now - 30 * 1000)).toBe("just now"); // 30 seconds ago |
| 9 | + }); |
| 10 | + |
| 11 | + test("should return 'just now' for future timestamps", () => { |
| 12 | + const future = Date.now() + 5000; |
| 13 | + expect(formatRelativeTime(future)).toBe("just now"); |
| 14 | + }); |
| 15 | + |
| 16 | + test("should format minutes correctly", () => { |
| 17 | + const now = Date.now(); |
| 18 | + expect(formatRelativeTime(now - 1 * 60 * 1000)).toBe("1 minute ago"); |
| 19 | + expect(formatRelativeTime(now - 5 * 60 * 1000)).toBe("5 minutes ago"); |
| 20 | + expect(formatRelativeTime(now - 59 * 60 * 1000)).toBe("59 minutes ago"); |
| 21 | + }); |
| 22 | + |
| 23 | + test("should format hours correctly", () => { |
| 24 | + const now = Date.now(); |
| 25 | + expect(formatRelativeTime(now - 1 * 60 * 60 * 1000)).toBe("1 hour ago"); |
| 26 | + expect(formatRelativeTime(now - 3 * 60 * 60 * 1000)).toBe("3 hours ago"); |
| 27 | + expect(formatRelativeTime(now - 23 * 60 * 60 * 1000)).toBe("23 hours ago"); |
| 28 | + }); |
| 29 | + |
| 30 | + test("should format days correctly", () => { |
| 31 | + const now = Date.now(); |
| 32 | + expect(formatRelativeTime(now - 1 * 24 * 60 * 60 * 1000)).toBe("1 day ago"); |
| 33 | + expect(formatRelativeTime(now - 3 * 24 * 60 * 60 * 1000)).toBe("3 days ago"); |
| 34 | + expect(formatRelativeTime(now - 6 * 24 * 60 * 60 * 1000)).toBe("6 days ago"); |
| 35 | + }); |
| 36 | + |
| 37 | + test("should format weeks correctly", () => { |
| 38 | + const now = Date.now(); |
| 39 | + expect(formatRelativeTime(now - 7 * 24 * 60 * 60 * 1000)).toBe("1 week ago"); |
| 40 | + expect(formatRelativeTime(now - 14 * 24 * 60 * 60 * 1000)).toBe("2 weeks ago"); |
| 41 | + expect(formatRelativeTime(now - 27 * 24 * 60 * 60 * 1000)).toBe("3 weeks ago"); |
| 42 | + }); |
| 43 | + |
| 44 | + test("should format months correctly", () => { |
| 45 | + const now = Date.now(); |
| 46 | + expect(formatRelativeTime(now - 30 * 24 * 60 * 60 * 1000)).toBe("1 month ago"); |
| 47 | + expect(formatRelativeTime(now - 60 * 24 * 60 * 60 * 1000)).toBe("2 months ago"); |
| 48 | + expect(formatRelativeTime(now - 180 * 24 * 60 * 60 * 1000)).toBe("6 months ago"); |
| 49 | + }); |
| 50 | + |
| 51 | + test("should format years correctly", () => { |
| 52 | + const now = Date.now(); |
| 53 | + expect(formatRelativeTime(now - 365 * 24 * 60 * 60 * 1000)).toBe("1 year ago"); |
| 54 | + expect(formatRelativeTime(now - 730 * 24 * 60 * 60 * 1000)).toBe("2 years ago"); |
| 55 | + }); |
| 56 | +}); |
0 commit comments