Each <RelativeTime> ran its own one-second interval and redrew each tick, though "5m ago" holds for a minute and "3h ago" for an hour. Every label now sets a timeout for the moment its text next changes (next-change.ts), so a list of timestamps in minutes redraws once a minute.
37 lines
1.3 KiB
JavaScript
37 lines
1.3 KiB
JavaScript
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { msUntilTextChanges } from "../src/ui/relative-time/next-change.ts";
|
|
|
|
test("an age under a minute changes every second", () => {
|
|
assert.equal(msUntilTextChanges(0, false), 1000);
|
|
assert.equal(msUntilTextChanges(2500, false), 500);
|
|
});
|
|
|
|
test("an age in minutes changes at the next minute", () => {
|
|
assert.equal(msUntilTextChanges(60_000, false), 60_000);
|
|
assert.equal(msUntilTextChanges(90_000, false), 30_000);
|
|
});
|
|
|
|
test("an age in hours changes at the next hour", () => {
|
|
assert.equal(msUntilTextChanges(3_600_000 + 1_000, false), 3_599_000);
|
|
});
|
|
|
|
test("an age in days changes at the next day", () => {
|
|
assert.equal(msUntilTextChanges(86_400_000, false), 86_400_000);
|
|
});
|
|
|
|
test("a countdown drops a step when the time left falls below it", () => {
|
|
// 5m 30s left reads "in 5m" until 5m, then "in 4m".
|
|
assert.equal(msUntilTextChanges(-330_000, true), 30_001);
|
|
// 20.5s left reads "in 20s" until 20s.
|
|
assert.equal(msUntilTextChanges(-20_500, true), 501);
|
|
});
|
|
|
|
test("a clock ahead of an age waits for it to catch up", () => {
|
|
assert.equal(msUntilTextChanges(-4000, false), 5000);
|
|
});
|
|
|
|
test("the wait never goes under the floor", () => {
|
|
assert.equal(msUntilTextChanges(999_990, false) >= 50, true);
|
|
assert.equal(msUntilTextChanges(-10, true) >= 50, true);
|
|
});
|