hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { render, waitFor } from "@solidjs/testing-library"; |
| 2 | import { createSignal } from "solid-js"; |
| 3 | import type { User } from "@/api/client"; |
| 4 | import { ShellFeedProvider, useShellFeed } from "@/stores/shell-feed-context"; |
| 5 | |
| 6 | // The provider only reads `auth.user()`; drive it from a signal the test owns. |
| 7 | const [user, setUser] = createSignal<User | null>(null); |
| 8 | vi.mock("@/stores/auth-context", () => ({ useAuth: () => ({ user }) })); |
| 9 | // Every module on: the feed skips switched-off modules, which is not what these cases test. |
| 10 | vi.mock("@/stores/modules-context", () => ({ useModules: () => ({ isEnabled: () => true, enabled: () => null, loading: () => false }) })); |
| 11 | |
| 12 | const 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. |
| 15 | vi.mock("@/api/messages", () => ({ |
| 16 | getMessages: async () => page([{ id: `${user()?.id}-msg`, read: false }]), |
| 17 | })); |
| 18 | vi.mock("@/api/events", () => ({ getEvents: async () => page([]) })); |
| 19 | vi.mock("@/api/exams", () => ({ getExams: async () => page([]) })); |
| 20 | vi.mock("@/api/appointments", () => ({ getAppointments: async () => page([]) })); |
| 21 | vi.mock("@/api/homework", () => ({ getHomework: async () => page([]) })); |
| 22 | |
| 23 | const as = (id: string) => ({ id }) as User; |
| 24 | |
| 25 | function Probe() { |
| 26 | const feed = useShellFeed(); |
| 27 | return <span data-testid="ids">{feed.messages().items.map((m) => m.id).join(",")}</span>; |
| 28 | } |
| 29 | |
| 30 | test("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 | |
| 47 | test("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 | |
| 60 | afterEach(() => { |
| 61 | setUser(null); |
| 62 | vi.restoreAllMocks(); |
| 63 | }); |