feat(comments) : add newest and oldest sorting to admin inbox

This commit is contained in:
2026-10-13 00:48:43 +07:00 Unverified
parent d1dff1a763
commit 7933c08068
8 changed files with 98 additions and 7 deletions
@@ -72,6 +72,26 @@ async function click(label: string) {
function singleRequests() { return fetchMock.mock.calls.filter(([url]) => String(url) === `/api/comments/${selectedId}`); }
describe("notification conversations in the admin inbox", () => {
it("defaults to newest and keeps oldest across pagination and refresh", async () => {
await mount(page(inbox.items, "newest-page"), "");
await click("รีเฟรช");
expect(new URL(String(fetchMock.mock.calls.at(-1)![0]), "https://guide.sudloh.com").searchParams.get("sort")).toBe("newest");
inbox = page([comment("oldest")], "oldest-page");
await click("เก่าสุด");
expect(rows()).toEqual(["comment-oldest"]);
nextPage = page([comment("next-oldest")]);
await click("ดูความคิดเห็นเพิ่มเติม");
const pagination = new URL(String(fetchMock.mock.calls.at(-1)![0]), "https://guide.sudloh.com").searchParams;
expect(pagination.get("sort")).toBe("oldest");
expect(pagination.get("cursor")).toBe("oldest-page");
await click("รีเฟรช");
expect(new URL(String(fetchMock.mock.calls.at(-1)![0]), "https://guide.sudloh.com").searchParams.get("sort")).toBe("oldest");
await click("ล่าสุด");
const newest = new URL(String(fetchMock.mock.calls.at(-1)![0]), "https://guide.sudloh.com").searchParams;
expect(newest.get("sort")).toBe("newest");
expect(newest.has("cursor")).toBe(false);
});
it("moves a loaded conversation to the first normal row, expands it, and avoids a second lookup", async () => {
await mount(page([...inbox.items, selected]));
expect(rows()).toEqual([`comment-${selectedId}`, "comment-newer"]);
@@ -107,18 +127,19 @@ describe("notification conversations in the admin inbox", () => {
expect(rows()).toEqual([`comment-${selectedId}`, "comment-newer"]);
});
it.each(["visibility", "answered", "target"])("clears the notification selection when the %s filter changes", async (filter) => {
it.each(["visibility", "answered", "target", "sort"])("clears the notification selection when the %s filter changes", async (filter) => {
await mount();
inbox = page([comment("filtered")]);
if (filter === "target") {
const clear = container.querySelector<HTMLButtonElement>('[data-slot="combobox-clear"]');
expect(clear).not.toBeNull();
await act(async () => clear!.click());
} else await click(filter === "visibility" ? "ซ่อนอยู่" : "ยังไม่ได้ตอบ");
} else await click(filter === "visibility" ? "ซ่อนอยู่" : filter === "sort" ? "เก่าสุด" : "ยังไม่ได้ตอบ");
expect(rows()).toEqual(["comment-filtered"]);
expect(singleRequests()).toHaveLength(1);
const query = new URL(String(fetchMock.mock.calls.at(-1)![0]), "https://guide.sudloh.com").searchParams;
if (filter === "target") expect(query.has("target")).toBe(false);
else if (filter === "sort") expect(query.get("sort")).toBe("oldest");
else expect(query.get(filter === "visibility" ? "status" : "unanswered")).toBe(filter === "visibility" ? "hidden" : "true");
});