| 1 | import {
|
| 2 | formatDate,
|
| 3 | formatMoment,
|
| 4 | getTimeBadge,
|
| 5 | parseDateOnly,
|
| 6 | } from "@/lib/utils/time";
|
| 7 |
|
| 8 | const at = (hour: number, minute = 0) => new Date(2025, 0, 15, hour, minute);
|
| 9 |
|
| 10 | beforeEach(() => {
|
| 11 | jest.useFakeTimers();
|
| 12 | jest.setSystemTime(new Date(2025, 0, 15, 10, 0, 0));
|
| 13 | });
|
| 14 |
|
| 15 | afterEach(() => {
|
| 16 | jest.useRealTimers();
|
| 17 | });
|
| 18 |
|
| 19 | describe("getTimeBadge", () => {
|
| 20 | it.each([
|
| 21 | { clock: "00:00", date: at(0), expected: { label: "Madrugada", variant: "purple" } },
|
| 22 | { clock: "03:30", date: at(3, 30), expected: { label: "Madrugada", variant: "purple" } },
|
| 23 | { clock: "05:59", date: at(5, 59), expected: { label: "Madrugada", variant: "purple" } },
|
| 24 | { clock: "06:00", date: at(6), expected: { label: "Manhã", variant: "green" } },
|
| 25 | { clock: "11:59", date: at(11, 59), expected: { label: "Manhã", variant: "green" } },
|
| 26 | { clock: "12:00", date: at(12), expected: { label: "Tarde", variant: "orange" } },
|
| 27 | { clock: "17:59", date: at(17, 59), expected: { label: "Tarde", variant: "orange" } },
|
| 28 | { clock: "18:00", date: at(18), expected: { label: "Noite", variant: "blue" } },
|
| 29 | { clock: "23:59", date: at(23, 59), expected: { label: "Noite", variant: "blue" } },
|
| 30 | ])("badges $clock as $expected.label", ({ date, expected }) => {
|
| 31 | expect(getTimeBadge(date)).toStrictEqual(expected);
|
| 32 | });
|
| 33 | });
|
| 34 |
|
| 35 | describe("formatMoment", () => {
|
| 36 | it("labels a moment from earlier today", () => {
|
| 37 | expect(formatMoment(new Date(2025, 0, 15, 8, 30))).toBe("Hoje às 08h30");
|
| 38 | });
|
| 39 |
|
| 40 | it("labels today's first minute as today", () => {
|
| 41 | expect(formatMoment(new Date(2025, 0, 15, 0, 0))).toBe("Hoje às 00h00");
|
| 42 | });
|
| 43 |
|
| 44 | it("labels last night's final minute as yesterday", () => {
|
| 45 | expect(formatMoment(new Date(2025, 0, 14, 23, 59))).toBe("Ontem às 23h59");
|
| 46 | });
|
| 47 |
|
| 48 | it("labels an older moment with the day and month", () => {
|
| 49 | expect(formatMoment(new Date(2025, 0, 10, 8, 30))).toBe(
|
| 50 | "10 de jan às 08h30",
|
| 51 | );
|
| 52 | });
|
| 53 | });
|
| 54 |
|
| 55 | describe("formatDate", () => {
|
| 56 | it("names today with a short date", () => {
|
| 57 | expect(formatDate(new Date(2025, 0, 15))).toBe("Hoje, 15/01");
|
| 58 | });
|
| 59 |
|
| 60 | it("names yesterday with a short date", () => {
|
| 61 | expect(formatDate(new Date(2025, 0, 14, 23, 59))).toBe("Ontem, 14/01");
|
| 62 | });
|
| 63 |
|
| 64 | it("names an older date with the year", () => {
|
| 65 | expect(formatDate(new Date(2025, 0, 10))).toBe("10/01/2025");
|
| 66 | });
|
| 67 | });
|
| 68 |
|
| 69 | describe("parseDateOnly", () => {
|
| 70 | it("anchors a date-only string inside the day it names", () => {
|
| 71 | const parsed = parseDateOnly("2025-01-10T00:00:00.000Z");
|
| 72 |
|
| 73 | expect(parsed.toDateString()).toBe(new Date(2025, 0, 10).toDateString());
|
| 74 | });
|
| 75 |
|
| 76 | it("anchors at local noon so an offset cannot roll the day over", () => {
|
| 77 | expect(parseDateOnly("2025-01-10T00:00:00.000Z").getHours()).toBe(12);
|
| 78 | });
|
| 79 |
|
| 80 | it("accepts a Date", () => {
|
| 81 | const parsed = parseDateOnly(new Date(Date.UTC(2025, 0, 10)));
|
| 82 |
|
| 83 | expect(parsed.toDateString()).toBe(new Date(2025, 0, 10).toDateString());
|
| 84 | });
|
| 85 | });
|