Skip to content

Commit

Permalink
add: messages on formatTime tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mitsunee committed Nov 23, 2024
1 parent 3b78ecc commit c5eb2f8
Showing 1 changed file with 68 additions and 18 deletions.
86 changes: 68 additions & 18 deletions tests/formatTime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,97 @@ import * as assert from "uvu/assert";
import { formatTime } from "../src/formatTime";
import { getLocalTimezone } from "./utils/getLocalTimezone";

// need to actually get local timezone for these tests
// TODO: actually set a non-UTC tz in tests (see https://github.com/szenius/set-timezone/blob/master/index.mjs#L12)
const localTz = getLocalTimezone();
const sampleDate = new Date(`2024-11-23T00:17:32.123${localTz}`);

console.log(
`Created sample time with offset ${localTz}: ${sampleDate.toLocaleString()}`
`Created sample time with offset ${localTz}:
Local: ${sampleDate.toLocaleString()}
UTC: ${sampleDate.toLocaleString(undefined, { timeZone: "UTC" })}`
);

// TODO: messages on all of these tests below

test("basic date variables", () => {
assert.is(formatTime("%iso_short%", sampleDate, false, 24), "2024-11-23");
assert.is(formatTime("%time%", sampleDate, false, 24), "00:17:32");
assert.is(
formatTime("%iso_short%", sampleDate, false, 24),
"2024-11-23",
"test current date (%iso_short%)"
);
assert.is(
formatTime("%time%", sampleDate, false, 24),
"00:17:32",
"test current time (%time%)"
);
});

test("12hour notation", () => {
assert.is(formatTime("%time%", sampleDate, false, 12), "12:17:32");
assert.is(
formatTime("%time%", sampleDate, false, 12),
"12:17:32",
"test current time with 12hr notation"
);
const date = new Date(`2024-11-23T14:25:36${localTz}`);
assert.is(formatTime("%time%", date, false, 24), "14:25:36");
assert.is(formatTime("%time%", date, false, 12), "02:25:36");
assert.is(
formatTime("%time%", date, false, 24),
"14:25:36",
"test a date at 2pm with 24hr notation"
);
assert.is(
formatTime("%time%", date, false, 12),
"02:25:36",
"test a date at 2pm with 12hr notation"
);
});

test("UTC vs local time", () => {
// NOTE: this test will break in the year 10,000. You'll have to adjust the 19 below :)
const utc = sampleDate.toISOString().slice(0, 19); // pls work
const [date, time] = utc.split("T");
assert.is(formatTime("%iso_short%", sampleDate, true, 24), date);
assert.is(formatTime("%time%", sampleDate, true, 24), time);
assert.is(
formatTime("%iso_short%", sampleDate, true, 24),
date,
"test %iso_short% for UTC"
);
assert.is(
formatTime("%time%", sampleDate, true, 24),
time,
"test %time% for UTC"
);
});

test("other variables", () => {
const otherDate = new Date(`2024-12-25T00:17:32.123${localTz}`);
assert.is(formatTime("%month_short%", sampleDate, false, 24), "Nov");
assert.is(formatTime("%month_short%", otherDate, false, 24), "Dec");
assert.is(
formatTime("%month_short%", sampleDate, false, 24),
"Nov",
"test %month_short% on sample time"
);
assert.is(
formatTime("%month_short%", otherDate, false, 24),
"Dec",
"test %month_short% on other time"
);

assert.is(formatTime("%date_ord%", sampleDate, false, 24), "23rd");
assert.is(formatTime("%date_ord%", otherDate, false, 24), "25th");
assert.is(
formatTime("%date_ord%", sampleDate, false, 24),
"23rd",
"test %date_ord% on sample time (with '3rd')"
);
assert.is(
formatTime("%date_ord%", otherDate, false, 24),
"25th",
"test %date_ord% on day with 'th'"
);

assert.is(formatTime("%day%", sampleDate, false, 24), "Sat");
assert.is(formatTime("%day%", otherDate, false, 24), "Wed");
assert.is(
formatTime("%day%", sampleDate, false, 24),
"Sat",
"test %day% on sample time"
);
assert.is(
formatTime("%day%", otherDate, false, 24),
"Wed",
"test %day% on other time"
);
});

test.run();

0 comments on commit c5eb2f8

Please sign in to comment.