Files
marco/tests/unit/utils/format-text-test.js
T
raucao 23444a49a8
CI / Lint (pull_request) Successful in 1m2s
CI / Test (pull_request) Successful in 1m17s
Release Drafter / Update release notes draft (pull_request) Successful in 4s
Omit year for current year, tweak layout
2026-08-29 15:50:49 -06:00

89 lines
3.7 KiB
JavaScript

import { module, test } from 'qunit';
import {
formatRelativeDate,
humanizeOsmTag,
capitalize,
} from 'marco/utils/format-text';
module('Unit | Utility | format-text', function () {
test('humanizeOsmTag replaces underscores and dashes with spaces and title-cases', function (assert) {
assert.strictEqual(humanizeOsmTag('atm'), 'Atm');
assert.strictEqual(humanizeOsmTag('fast_food'), 'Fast Food');
assert.strictEqual(humanizeOsmTag('phone-repair'), 'Phone Repair');
assert.strictEqual(humanizeOsmTag(''), '');
assert.strictEqual(humanizeOsmTag(null), '');
});
test('capitalize uppercases the first letter', function (assert) {
assert.strictEqual(capitalize('hello'), 'Hello');
assert.strictEqual(capitalize(''), '');
assert.strictEqual(capitalize(null), '');
});
test('formatRelativeDate returns empty string for falsy timestamp', function (assert) {
assert.strictEqual(formatRelativeDate(null), '');
assert.strictEqual(formatRelativeDate(0), '');
assert.strictEqual(formatRelativeDate(undefined), '');
});
test('formatRelativeDate returns "just now" for timestamps within the last minute', function (assert) {
const now = Math.floor(Date.now() / 1000);
assert.strictEqual(formatRelativeDate(now), 'just now');
assert.strictEqual(formatRelativeDate(now - 10), 'just now');
});
test('formatRelativeDate returns "min ago" for timestamps within the last hour', function (assert) {
const now = Math.floor(Date.now() / 1000);
assert.strictEqual(formatRelativeDate(now - 60), '1 min ago');
assert.strictEqual(formatRelativeDate(now - 300), '5 min ago');
assert.strictEqual(formatRelativeDate(now - 3599), '59 min ago');
});
test('formatRelativeDate returns "hr ago" for timestamps within the last day', function (assert) {
const now = Math.floor(Date.now() / 1000);
assert.strictEqual(formatRelativeDate(now - 3600), '1 hr ago');
assert.strictEqual(formatRelativeDate(now - 7200), '2 hr ago');
assert.strictEqual(formatRelativeDate(now - 86399), '23 hr ago');
});
test('formatRelativeDate returns "day(s) ago" for timestamps within the last week', function (assert) {
const now = Math.floor(Date.now() / 1000);
assert.strictEqual(formatRelativeDate(now - 86400), '1 day ago');
assert.strictEqual(formatRelativeDate(now - 86400 * 2), '2 days ago');
assert.strictEqual(formatRelativeDate(now - 86400 * 6), '6 days ago');
});
test('formatRelativeDate returns an absolute date for timestamps older than a week', function (assert) {
const now = Math.floor(Date.now() / 1000);
const oldTimestamp = now - 86400 * 400; // 400 days ago (previous year)
const result = formatRelativeDate(oldTimestamp);
// The exact format depends on the locale, but it should contain a year
assert.ok(
result.includes(String(new Date().getFullYear() - 1)),
'Contains the previous year'
);
});
test('formatRelativeDate omits year for same-year dates older than a week', function (assert) {
const now = Math.floor(Date.now() / 1000);
const sameYearTimestamp = now - 86400 * 30; // 30 days ago (same year)
const result = formatRelativeDate(sameYearTimestamp);
// Should NOT contain the current year
assert.notOk(
result.includes(String(new Date().getFullYear())),
'Does not contain the current year'
);
// Should contain month and day
const expectedMonth = new Date(sameYearTimestamp * 1000).toLocaleDateString(
undefined,
{ month: 'short' }
);
const expectedDay = new Date(sameYearTimestamp * 1000).toLocaleDateString(
undefined,
{ day: 'numeric' }
);
assert.ok(result.includes(expectedMonth), 'Contains month');
assert.ok(result.includes(expectedDay), 'Contains day');
});
});