forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

shell-unread-badge.test.tsx131 lines, 5.0 KiBHistoryRawDownload
1import { createSignal } from "solid-js";
2import { fireEvent, render, screen, within } 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, navigate: vi.fn() }));
10
11vi.mock("@/stores/shell-feed-context", () => ({ useShellFeed: () => h.feed }));
12vi.mock("@/api/messages", () => ({ patchMessageById: vi.fn(async () => ({})) }));
13vi.mock("@/stores/auth-context", () => ({ useAuth: () => ({ user: () => ({ id: "u-1" }) }) }));
14vi.mock("@tanstack/solid-router", () => ({
15 Link: (props: never) => <a href={(props as { to: string }).to}>{(props as { children: unknown }).children}</a>,
16 useNavigate: () => h.navigate,
17}));
18// Kobalte's popover keeps its content unmounted in jsdom; the badge and the
19// "dismiss all" button are the surfaces under test, so render both inline.
20vi.mock("@/components/ui/popover", () => ({
21 Popover: (props: never) => (props as { children: unknown }).children,
22 PopoverTrigger: (props: never) => <div>{(props as { children: unknown }).children}</div>,
23 PopoverContent: (props: never) => <div>{(props as { children: unknown }).children}</div>,
24}));
25
26const emptyPage = { items: [], total: 0, limit: null, offset: 0 };
27const msg = (id: string) => ({
28 id,
29 sender: { id: "s", name: "Ada", surname: "L" },
30 subject: `subject ${id}`,
31 body: "",
32 read: false,
33 sent_at: 1,
34});
35
36function mountFeed(inbox: unknown[], unread: { items: unknown[]; total: number }) {
37 const [messages, setMessages] = createSignal({ ...emptyPage, items: inbox, total: inbox.length });
38 const [unreadMessages, setUnreadMessages] = createSignal({ ...emptyPage, ...unread });
39 h.feed = {
40 messages,
41 unreadMessages,
42 events: () => emptyPage,
43 exams: () => emptyPage,
44 appointments: () => emptyPage,
45 homework: () => emptyPage,
46 nowMs: () => Date.now(),
47 refetchMessages: () => {},
48 refreshAll: () => {},
49 } as never;
50 return { setMessages, setUnreadMessages };
51}
52
53const badges = () => Array.from(document.querySelectorAll("span.bg-destructive")).map((n) => n.textContent);
54
55beforeEach(() => localStorage.clear());
56afterEach(() => vi.restoreAllMocks());
57
58// DEFECT 1: shell message badge used to filter the capped `limit:100` inbox page, so an
59// unread message that fell off that page was counted by one badge only.
60test("both shell badges report the same unread count", () => {
61 // 7 unread server-side, but only 2 of them are on the capped inbox page.
62 mountFeed([msg("m6"), msg("m7")], { items: [msg("m3"), msg("m6"), msg("m7")], total: 7 });
63
64 render(() => (
65 <PreferencesProvider>
66 <ShellMessagesButton />
67 <NotificationCenter />
68 </PreferencesProvider>
69 ));
70
71 expect(badges()).toEqual(["7", "7"]);
72});
73
74// DEFECT 2: unlisted unread ignored dismissal, so dismiss-all left the badge
75// stuck at (total - listed) over an empty popover.
76test("dismiss all clears the badge, a later message raises it again", async () => {
77 const listed = Array.from({ length: 10 }, (_, i) => msg(`m${i}`));
78 const { setUnreadMessages } = mountFeed([], { items: listed, total: 11 });
79
80 render(() => (
81 <PreferencesProvider>
82 <NotificationCenter />
83 </PreferencesProvider>
84 ));
85
86 expect(badges()).toEqual(["9+"]);
87
88 // Clearing everything asks first; nothing is cleared until it is confirmed.
89 fireEvent.click(screen.getByTitle("Clear all"));
90 expect(badges()).toEqual(["9+"]);
91 const dialog = await screen.findByRole("alertdialog");
92 fireEvent.click(within(dialog).getByRole("button", { name: "Clear all" }));
93 await vi.waitFor(() => expect(badges()).toEqual([]));
94
95 // A genuinely new message pushes the oldest listed one off the page.
96 setUnreadMessages({ ...emptyPage, items: [msg("new"), ...listed.slice(0, 9)], total: 12 });
97 expect(badges()).toEqual(["1"]);
98});
99
100test("the bell badge and the list header show the same capped unread count", () => {
101 const listed = Array.from({ length: 10 }, (_, i) => msg(`m${i}`));
102 mountFeed([], { items: listed, total: 20 });
103
104 render(() => (
105 <PreferencesProvider>
106 <NotificationCenter />
107 </PreferencesProvider>
108 ));
109
110 expect(badges()).toEqual(["9+"]);
111 const header = screen.getByLabelText("20 unread");
112 expect(header.textContent).toBe("9+");
113});
114
115test("a notification with a target page opens it directly and is marked read", async () => {
116 h.navigate.mockClear();
117 mountFeed([], { items: [msg("m1")], total: 1 });
118
119 render(() => (
120 <PreferencesProvider>
121 <NotificationCenter />
122 </PreferencesProvider>
123 ));
124
125 expect(badges()).toEqual(["1"]);
126 fireEvent.click(screen.getByText("subject m1"));
127 expect(h.navigate).toHaveBeenCalledWith({ to: "/messages" });
128 // No intermediate "go to page" step.
129 expect(screen.queryByText("Go to page")).toBeNull();
130 await vi.waitFor(() => expect(badges()).toEqual([]));
131});