|
| 1 | +/* |
| 2 | + * Copyright 2025 New Vector Ltd. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial |
| 5 | + * Please see LICENSE files in the repository root for full details. |
| 6 | + */ |
| 7 | + |
| 8 | +import { humanizeTime } from "./humanize"; |
| 9 | + |
| 10 | +describe("humanizeTime", () => { |
| 11 | + const now = new Date("2025-08-01T12:00:00Z").getTime(); |
| 12 | + |
| 13 | + beforeAll(() => { |
| 14 | + jest.useFakeTimers().setSystemTime(now); |
| 15 | + }); |
| 16 | + |
| 17 | + it.each([ |
| 18 | + // Past |
| 19 | + ["returns 'a few seconds ago' for <15s ago", now - 5000, "a few seconds ago"], |
| 20 | + ["returns 'about a minute ago' for <75s ago", now - 60000, "about a minute ago"], |
| 21 | + ["returns '20 minutes ago' for <45min ago", now - 20 * 60000, "20 minutes ago"], |
| 22 | + ["returns 'about an hour ago' for <75min ago", now - 70 * 60000, "about an hour ago"], |
| 23 | + ["returns '5 hours ago' for <23h ago", now - 5 * 3600000, "5 hours ago"], |
| 24 | + ["returns 'about a day ago' for <26h ago", now - 25 * 3600000, "about a day ago"], |
| 25 | + ["returns '3 days ago' for >26h ago", now - 3 * 24 * 3600000, "3 days ago"], |
| 26 | + // Future |
| 27 | + ["returns 'a few seconds from now' for <15s ahead", now + 5000, "a few seconds from now"], |
| 28 | + ["returns 'about a minute from now' for <75s ahead", now + 60000, "about a minute from now"], |
| 29 | + ["returns '20 minutes from now' for <45min ahead", now + 20 * 60000, "20 minutes from now"], |
| 30 | + ["returns 'about an hour from now' for <75min ahead", now + 70 * 60000, "about an hour from now"], |
| 31 | + ["returns '5 hours from now' for <23h ahead", now + 5 * 3600000, "5 hours from now"], |
| 32 | + ["returns 'about a day from now' for <26h ahead", now + 25 * 3600000, "about a day from now"], |
| 33 | + ["returns '3 days from now' for >26h ahead", now + 3 * 24 * 3600000, "3 days from now"], |
| 34 | + ])("%s", (_, date, expected) => { |
| 35 | + expect(humanizeTime(date)).toBe(expected); |
| 36 | + }); |
| 37 | +}); |
0 commit comments