2026-05-25 05:25:57 +08:00
|
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
|
import { renderHook } from "@testing-library/react";
|
|
|
|
|
import { useGroupedByDay } from "./useGroupedByDay";
|
|
|
|
|
import type { Post } from "../../../types/post";
|
|
|
|
|
|
|
|
|
|
function makePost(id: string, isoDate: string): Post {
|
|
|
|
|
return {
|
|
|
|
|
id,
|
|
|
|
|
categoryId: 1,
|
|
|
|
|
categorySlug: "x",
|
2026-05-26 07:36:53 +08:00
|
|
|
language: "zh",
|
2026-05-25 05:25:57 +08:00
|
|
|
attachments: [],
|
|
|
|
|
isRecommended: false,
|
|
|
|
|
publishedAt: isoDate,
|
|
|
|
|
updatedAt: isoDate,
|
|
|
|
|
text: id,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe("useGroupedByDay", () => {
|
|
|
|
|
it("groups posts by local date", () => {
|
|
|
|
|
const posts: Post[] = [
|
|
|
|
|
makePost("a", "2026-02-27T10:00:00.000Z"),
|
|
|
|
|
makePost("b", "2026-02-27T23:00:00.000Z"),
|
|
|
|
|
makePost("c", "2026-02-28T01:00:00.000Z"),
|
|
|
|
|
makePost("d", "2026-05-16T12:00:00.000Z"),
|
|
|
|
|
];
|
2026-05-26 07:36:53 +08:00
|
|
|
const { result } = renderHook(() => useGroupedByDay(posts, "zh"));
|
2026-05-25 05:25:57 +08:00
|
|
|
expect(result.current.length).toBeGreaterThanOrEqual(2);
|
|
|
|
|
const allIds = result.current.flatMap((g) => g.items.map((p) => p.id));
|
|
|
|
|
expect(allIds).toEqual(["a", "b", "c", "d"]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("preserves input order within groups", () => {
|
|
|
|
|
const posts: Post[] = [
|
|
|
|
|
makePost("first", "2026-03-01T10:00:00.000Z"),
|
|
|
|
|
makePost("second", "2026-03-01T11:00:00.000Z"),
|
|
|
|
|
makePost("third", "2026-03-01T12:00:00.000Z"),
|
|
|
|
|
];
|
|
|
|
|
const { result } = renderHook(() => useGroupedByDay(posts, "en"));
|
|
|
|
|
expect(result.current).toHaveLength(1);
|
|
|
|
|
expect(result.current[0].items.map((p) => p.id)).toEqual([
|
|
|
|
|
"first",
|
|
|
|
|
"second",
|
|
|
|
|
"third",
|
|
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("returns empty array for empty input", () => {
|
2026-05-26 07:36:53 +08:00
|
|
|
const { result } = renderHook(() => useGroupedByDay([], "zh"));
|
2026-05-25 05:25:57 +08:00
|
|
|
expect(result.current).toEqual([]);
|
|
|
|
|
});
|
|
|
|
|
});
|