The bell pushed the user menu off a 360 px screen; the right-hand controls now use smaller gaps and a capped company picker on phones. Mark all read on the page now tells the bell to recount at once instead of on its next poll. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
95 lines
4.1 KiB
TypeScript
95 lines
4.1 KiB
TypeScript
import { render, screen, waitFor } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
const svc = vi.hoisted(() => ({
|
|
getUnreadCount: vi.fn(),
|
|
listNotifications: vi.fn(),
|
|
markRead: vi.fn(),
|
|
markAllRead: vi.fn(),
|
|
}));
|
|
vi.mock("@/app/lib/services/notifications", async (orig) => ({
|
|
...(await orig<typeof import("@/app/lib/services/notifications")>()),
|
|
...svc,
|
|
}));
|
|
|
|
import NotificationsBell from "./NotificationsBell";
|
|
|
|
const row = (id: number, seen: boolean, link: string | null = `/leads/${id}`) => ({
|
|
id, title: "NoticeLeadAssigned", type: "LEAD_ASSIGNED", link, seen, createDateTime: "2026-12-02T09:00:00",
|
|
message: '{"code":"NoticeLeadBody","params":{"lead":"Ali","event":"Expo"}}',
|
|
});
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
svc.getUnreadCount.mockResolvedValue(2);
|
|
svc.listNotifications.mockResolvedValue({ items: [row(1, false), row(2, false), row(3, true)], total: 3 });
|
|
svc.markRead.mockResolvedValue(undefined);
|
|
svc.markAllRead.mockResolvedValue(undefined);
|
|
});
|
|
|
|
// add-lead-notifications: the header bell.
|
|
describe("NotificationsBell", () => {
|
|
it("shows the unread count from the server", async () => {
|
|
render(<NotificationsBell />);
|
|
expect(await screen.findByText("2")).toBeInTheDocument();
|
|
expect(screen.getByRole("button", { name: /2 unread/i })).toBeInTheDocument();
|
|
});
|
|
|
|
it("opening it lists the newest 10 in the reader's language and marks the unread ones read", async () => {
|
|
render(<NotificationsBell />);
|
|
await screen.findByText("2");
|
|
svc.getUnreadCount.mockResolvedValue(0);
|
|
await userEvent.click(screen.getByRole("button", { name: /notifications/i }));
|
|
|
|
expect(svc.listNotifications).toHaveBeenCalledWith(0, 10);
|
|
expect(await screen.findAllByText("Lead assigned to you")).toHaveLength(3);
|
|
expect(screen.getAllByText("Ali · Expo")).toHaveLength(3);
|
|
await waitFor(() => expect(svc.markRead).toHaveBeenCalledTimes(2));
|
|
expect(svc.markRead).toHaveBeenCalledWith(1);
|
|
expect(svc.markRead).toHaveBeenCalledWith(2);
|
|
await waitFor(() => expect(screen.queryByText("2")).not.toBeInTheDocument());
|
|
});
|
|
|
|
it("each item links to its target, and only to a path on this site", async () => {
|
|
svc.listNotifications.mockResolvedValue({ items: [row(1, true), row(2, true, "//evil.example")], total: 2 });
|
|
render(<NotificationsBell />);
|
|
await userEvent.click(await screen.findByRole("button", { name: /notifications/i }));
|
|
|
|
const items = await screen.findAllByRole("menuitem", { name: /Lead assigned to you/ });
|
|
expect(items[0]).toHaveAttribute("href", "/leads/1");
|
|
expect(items[1]).not.toHaveAttribute("href");
|
|
expect(screen.getByRole("menuitem", { name: /see all/i })).toHaveAttribute("href", "/notifications");
|
|
});
|
|
|
|
it("says so when there are none, or when loading fails", async () => {
|
|
svc.listNotifications.mockResolvedValue({ items: [], total: 0 });
|
|
const { unmount } = render(<NotificationsBell />);
|
|
await userEvent.click(await screen.findByRole("button", { name: /notifications/i }));
|
|
expect(await screen.findByText("No notifications yet.")).toBeInTheDocument();
|
|
unmount();
|
|
|
|
svc.listNotifications.mockRejectedValue(new Error("down"));
|
|
render(<NotificationsBell />);
|
|
await userEvent.click(await screen.findByRole("button", { name: /notifications/i }));
|
|
expect(await screen.findByRole("alert")).toHaveTextContent("Couldn't load notifications.");
|
|
});
|
|
|
|
it("recounts at once when another screen changes read state", async () => {
|
|
render(<NotificationsBell />);
|
|
await screen.findByText("2");
|
|
svc.getUnreadCount.mockResolvedValue(0);
|
|
|
|
window.dispatchEvent(new Event("notifications:changed"));
|
|
|
|
await waitFor(() => expect(screen.queryByText("2")).not.toBeInTheDocument());
|
|
});
|
|
|
|
it("a failing unread count leaves the bell quiet instead of breaking the header", async () => {
|
|
svc.getUnreadCount.mockRejectedValue(new Error("down"));
|
|
render(<NotificationsBell />);
|
|
expect(await screen.findByRole("button", { name: /notifications/i })).toBeInTheDocument();
|
|
expect(screen.queryByText("0")).not.toBeInTheDocument();
|
|
});
|
|
});
|