forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

shell-unread-badge.test.tsx93 lines, 3.7 KiBHistoryRawDownload
1import { createSignal } from "solid-js";
2import { fireEvent, render, screen } from "@solidjs/testing-library";
3import { NotificationCenter } from "@/components/layout/notification-center";
4import { ShellMessagesButton } from "@/components/layout/shell-messages-button";
5import { PreferencesProvider } from "@/stores/preferences-context";
6
7// The shell feed is the single source of truth for both badges; the test drives
8// it through signals so a refetch (new message arriving) is reproducible.
9const h = vi.hoisted(() => ({ feed: null as never }));
10
11vi.mock("@/stores/shell-feed-context", () => ({ useShellFeed: () => h.feed }));
12vi.mock("@/stores/auth-context", () => ({ useAuth: () => ({ user: () => ({ id: "u-1" }) }) }));
13vi.mock("@tanstack/solid-router", () => ({
14 Link: (props: never) => <a href={(props as { to: string }).to}>{(props as { children: unknown }).children}</a>,
15 useNavigate: () => () => undefined,
16}));
17// Kobalte's popover keeps its content unmounted in jsdom; the badge and the
18// "dismiss all" button are the surfaces under test, so render both inline.
19vi.mock("@/components/ui/popover", () => ({
20 Popover: (props: never) => (props as { children: unknown }).children,
21 PopoverTrigger: (props: never) => <div>{(props as { children: unknown }).children}</div>,
22 PopoverContent: (props: never) => <div>{(props as { children: unknown }).children}</div>,
23}));
24
25const emptyPage = { items: [], total: 0, limit: null, offset: 0 };
26const msg = (id: string) => ({
27 id,
28 sender: { id: "s", name: "Ada", surname: "L" },
29 subject: `subject ${id}`,
30 body: "",
31 read: false,
32 sent_at: 1,
33});
34
35function mountFeed(inbox: unknown[], unread: { items: unknown[]; total: number }) {
36 const [messages, setMessages] = createSignal({ ...emptyPage, items: inbox, total: inbox.length });
37 const [unreadMessages, setUnreadMessages] = createSignal({ ...emptyPage, ...unread });
38 h.feed = {
39 messages,
40 unreadMessages,
41 events: () => emptyPage,
42 exams: () => emptyPage,
43 appointments: () => emptyPage,
44 homework: () => emptyPage,
45 nowMs: () => Date.now(),
46 refetchMessages: () => {},
47 refreshAll: () => {},
48 } as never;
49 return { setMessages, setUnreadMessages };
50}
51
52const badges = () => Array.from(document.querySelectorAll(".bg-destructive")).map((n) => n.textContent);
53
54beforeEach(() => localStorage.clear());
55afterEach(() => vi.restoreAllMocks());
56
57// DEFECT 1: shell message badge used to filter the capped `limit:100` inbox page, so an
58// unread message that fell off that page was counted by one badge only.
59test("both shell badges report the same unread count", () => {
60 // 7 unread server-side, but only 2 of them are on the capped inbox page.
61 mountFeed([msg("m6"), msg("m7")], { items: [msg("m3"), msg("m6"), msg("m7")], total: 7 });
62
63 render(() => (
64 <PreferencesProvider>
65 <ShellMessagesButton />
66 <NotificationCenter />
67 </PreferencesProvider>
68 ));
69
70 expect(badges()).toEqual(["7", "7"]);
71});
72
73// DEFECT 2: unlisted unread ignored dismissal, so dismiss-all left the badge
74// stuck at (total - listed) over an empty popover.
75test("dismiss all clears the badge, a later message raises it again", () => {
76 const listed = Array.from({ length: 10 }, (_, i) => msg(`m${i}`));
77 const { setUnreadMessages } = mountFeed([], { items: listed, total: 11 });
78
79 render(() => (
80 <PreferencesProvider>
81 <NotificationCenter />
82 </PreferencesProvider>
83 ));
84
85 expect(badges()).toEqual(["9+"]);
86
87 fireEvent.click(screen.getByTitle("Clear all"));
88 expect(badges()).toEqual([]);
89
90 // A genuinely new message pushes the oldest listed one off the page.
91 setUnreadMessages({ ...emptyPage, items: [msg("new"), ...listed.slice(0, 9)], total: 12 });
92 expect(badges()).toEqual(["1"]);
93});