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-feed-context.test.tsx63 lines, 2.4 KiBHistoryRawDownload
1import { render, waitFor } from "@solidjs/testing-library";
2import { createSignal } from "solid-js";
3import type { User } from "@/api/client";
4import { ShellFeedProvider, useShellFeed } from "@/stores/shell-feed-context";
5
6// The provider only reads `auth.user()`; drive it from a signal the test owns.
7const [user, setUser] = createSignal<User | null>(null);
8vi.mock("@/stores/auth-context", () => ({ useAuth: () => ({ user }) }));
9// Every module on: the feed skips switched-off modules, which is not what these cases test.
10vi.mock("@/stores/modules-context", () => ({ useModules: () => ({ isEnabled: () => true, enabled: () => null, loading: () => false }) }));
11
12const page = <T,>(items: T[]) => ({ items, total: items.length, limit: null, offset: 0 });
13
14// Message previews are the account-scoped payload under test; the rest stay empty.
15vi.mock("@/api/messages", () => ({
16 getMessages: async () => page([{ id: `${user()?.id}-msg`, read: false }]),
17}));
18vi.mock("@/api/events", () => ({ getEvents: async () => page([]) }));
19vi.mock("@/api/exams", () => ({ getExams: async () => page([]) }));
20vi.mock("@/api/appointments", () => ({ getAppointments: async () => page([]) }));
21vi.mock("@/api/homework", () => ({ getHomework: async () => page([]) }));
22
23const as = (id: string) => ({ id }) as User;
24
25function Probe() {
26 const feed = useShellFeed();
27 return <span data-testid="ids">{feed.messages().items.map((m) => m.id).join(",")}</span>;
28}
29
30test("logout clears the feed and login as another user never shows the previous one's rows", async () => {
31 setUser(as("alice"));
32 const { getByTestId } = render(() => (
33 <ShellFeedProvider>
34 <Probe />
35 </ShellFeedProvider>
36 ));
37 await waitFor(() => expect(getByTestId("ids").textContent).toBe("alice-msg"));
38
39 setUser(null);
40 expect(getByTestId("ids").textContent).toBe(""); // no leftover value after logout
41
42 setUser(as("bob"));
43 expect(getByTestId("ids").textContent).toBe(""); // and none while bob's fetch is in flight
44 await waitFor(() => expect(getByTestId("ids").textContent).toBe("bob-msg"));
45});
46
47test("no fetch fires while logged out", async () => {
48 const messages = await import("@/api/messages");
49 const spy = vi.spyOn(messages, "getMessages");
50 setUser(null);
51 render(() => (
52 <ShellFeedProvider>
53 <Probe />
54 </ShellFeedProvider>
55 ));
56 await Promise.resolve();
57 expect(spy).not.toHaveBeenCalled();
58});
59
60afterEach(() => {
61 setUser(null);
62 vi.restoreAllMocks();
63});