Files
homepage/src/utils/relative-date-formatter.test.js
T
shamoon a7a4378a40
Docker CI / Docker Build & Push (push) Has been cancelled
Lint / Linting Checks (push) Has been cancelled
Release Drafter / Update Release Draft (push) Has been cancelled
Release Drafter / Auto Label PR (push) Has been cancelled
Tests / vitest (1) (push) Has been cancelled
Tests / vitest (2) (push) Has been cancelled
Tests / vitest (3) (push) Has been cancelled
Tests / vitest (4) (push) Has been cancelled
Fix: ensure correct relative dates formatter uses calendar days (#7174)
2026-09-23 11:41:55 -07:00

47 lines
1.4 KiB
JavaScript

import { createRequire } from "node:module";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
const require = createRequire(import.meta.url);
const i18nextConfig = require("../../next-i18next.config.js");
function getRelativeDateFormatter() {
let relativeDateFormatter;
i18nextConfig.use[0].init({
services: {
formatter: {
add(name, formatter) {
if (name === "relativeDate") relativeDateFormatter = formatter;
},
},
},
});
return relativeDateFormatter;
}
describe("relativeDate formatter", () => {
const formatRelativeDate = getRelativeDateFormatter();
const options = { numeric: "auto" };
beforeEach(() => {
vi.useFakeTimers();
vi.setSystemTime(new Date(2026, 8, 23, 16, 0));
});
afterEach(() => {
vi.useRealTimers();
});
it.each([
["hours away on the same day", new Date(2026, 8, 23, 20, 0), "in 4 hours"],
["next calendar day", new Date(2026, 8, 24, 20, 0), "tomorrow"],
["two calendar days away but under 48h", new Date(2026, 8, 25, 10, 40), "in 2 days"],
["previous calendar day", new Date(2026, 8, 22, 10, 0), "yesterday"],
["two calendar days ago but under 48h", new Date(2026, 8, 21, 20, 0), "2 days ago"],
])("formats %s", (_description, value, expected) => {
expect(formatRelativeDate(value.toISOString(), "en", options)).toBe(expected);
});
});