Backend modules from tastefully 8bd93d15: core-libs, core-session, api-gateway, mobile-api, services-auth, services-system, services-counter, services-member, services-log, services-email. solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal from tastefully 8bd93d15. Copied as tracked files only. Literal secrets replaced with environment placeholders: - services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY, MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default) - services-system: JUMIO_TOKEN, JUMIO_SECRET (optional) - services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed - core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default removed, ipgeolocation key read from IPGEOLOCATION_API_KEY - services-system EkycAppConfig: commented Jumio credentials removed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
194 lines
7.7 KiB
TypeScript
194 lines
7.7 KiB
TypeScript
import { render, screen, waitFor, within } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
const nav = vi.hoisted(() => ({ push: vi.fn() }));
|
|
vi.mock("next/navigation", () => ({ useRouter: () => ({ push: nav.push }) }));
|
|
|
|
const services = vi.hoisted(() => ({
|
|
listNotifications: vi.fn(),
|
|
getUnreadCount: vi.fn(),
|
|
markRead: vi.fn(),
|
|
markAllRead: vi.fn(),
|
|
}));
|
|
vi.mock("@/app/lib/services/notifications", () => services);
|
|
|
|
import { NotificationsBell } from "./NotificationsBell";
|
|
import type { AdminNotification } from "@/app/lib/types";
|
|
|
|
function item(overrides: Partial<AdminNotification> & { id: number }): AdminNotification {
|
|
return {
|
|
message: "Body text",
|
|
seen: false,
|
|
createDateTime: new Date(Date.now() - 5 * 60 * 1000).toISOString(),
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
nav.push.mockReset();
|
|
services.listNotifications.mockReset();
|
|
services.getUnreadCount.mockReset();
|
|
services.markRead.mockReset();
|
|
services.markAllRead.mockReset();
|
|
// Default stubs so best-effort fire-and-forget calls (markRead on open/click)
|
|
// always return a promise; individual tests override as needed.
|
|
services.listNotifications.mockResolvedValue({ items: [], total: 0 });
|
|
services.getUnreadCount.mockResolvedValue(0);
|
|
services.markRead.mockResolvedValue({ updated: 1 });
|
|
services.markAllRead.mockResolvedValue({ marked: 0 });
|
|
});
|
|
|
|
describe("NotificationsBell", () => {
|
|
it("shows the server unread count as a badge and lists the recent items", async () => {
|
|
services.listNotifications.mockResolvedValue({ items: [item({ id: 1, title: "Flash sale" })], total: 1 });
|
|
services.getUnreadCount.mockResolvedValue(2);
|
|
|
|
render(<NotificationsBell />);
|
|
|
|
expect(await screen.findByLabelText("Notifications, 2 unread")).toBeInTheDocument();
|
|
const user = userEvent.setup();
|
|
await user.click(screen.getByLabelText("Notifications, 2 unread"));
|
|
expect(await screen.findByText("Flash sale")).toBeInTheDocument();
|
|
});
|
|
|
|
it("renders the empty state when there are no notifications", async () => {
|
|
services.listNotifications.mockResolvedValue({ items: [], total: 0 });
|
|
services.getUnreadCount.mockResolvedValue(0);
|
|
|
|
render(<NotificationsBell />);
|
|
const user = userEvent.setup();
|
|
await user.click(screen.getByLabelText("Notifications"));
|
|
expect(await screen.findByText("No notifications yet.")).toBeInTheDocument();
|
|
});
|
|
|
|
it("renders an error state instead of throwing when the feed fails", async () => {
|
|
services.listNotifications.mockRejectedValue(new Error("boom"));
|
|
services.getUnreadCount.mockRejectedValue(new Error("boom"));
|
|
|
|
render(<NotificationsBell />);
|
|
const user = userEvent.setup();
|
|
await user.click(screen.getByLabelText("Notifications"));
|
|
expect(await screen.findByText(/couldn't load notifications/i)).toBeInTheDocument();
|
|
});
|
|
|
|
it("marks the visible unread items read when the panel opens", async () => {
|
|
services.listNotifications.mockResolvedValue({
|
|
items: [item({ id: 1, seen: false }), item({ id: 2, seen: false }), item({ id: 3, seen: true })],
|
|
total: 3,
|
|
});
|
|
services.getUnreadCount.mockResolvedValue(2);
|
|
services.markRead.mockResolvedValue({ updated: 1 });
|
|
|
|
render(<NotificationsBell />);
|
|
const user = userEvent.setup();
|
|
await user.click(await screen.findByLabelText("Notifications, 2 unread"));
|
|
|
|
await waitFor(() => expect(services.markRead).toHaveBeenCalledWith(1));
|
|
expect(services.markRead).toHaveBeenCalledWith(2);
|
|
expect(services.markRead).not.toHaveBeenCalledWith(3); // already-seen row is not re-marked
|
|
});
|
|
|
|
it("marks an item read and routes to its link when clicked", async () => {
|
|
services.listNotifications.mockResolvedValue({
|
|
items: [item({ id: 7, title: "Order update", link: "/orders/7" })],
|
|
total: 1,
|
|
});
|
|
services.getUnreadCount.mockResolvedValue(1);
|
|
services.markRead.mockResolvedValue({ updated: 1 });
|
|
|
|
render(<NotificationsBell />);
|
|
const user = userEvent.setup();
|
|
await user.click(await screen.findByLabelText("Notifications, 1 unread"));
|
|
await user.click(await screen.findByText("Order update"));
|
|
|
|
expect(services.markRead).toHaveBeenCalledWith(7);
|
|
expect(nav.push).toHaveBeenCalledWith("/orders/7");
|
|
});
|
|
|
|
// link-sink-allowlist (F47): a stored link that leaves the site is not followed.
|
|
it("does not follow an off-site link", async () => {
|
|
services.listNotifications.mockResolvedValue({
|
|
items: [item({ id: 8, title: "Claim your prize", link: "https://evil.example/login" })],
|
|
total: 1,
|
|
});
|
|
services.getUnreadCount.mockResolvedValue(1);
|
|
services.markRead.mockResolvedValue({ updated: 1 });
|
|
|
|
render(<NotificationsBell />);
|
|
const user = userEvent.setup();
|
|
await user.click(await screen.findByLabelText("Notifications, 1 unread"));
|
|
const row = await screen.findByText("Claim your prize");
|
|
expect(row.closest("a")).toBeNull();
|
|
await user.click(row);
|
|
|
|
expect(services.markRead).toHaveBeenCalledWith(8);
|
|
expect(nav.push).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("falls back safely for an unknown notification type (no throw)", async () => {
|
|
services.listNotifications.mockResolvedValue({
|
|
items: [item({ id: 1, title: "Strange kind", type: "mystery-category" })],
|
|
total: 1,
|
|
});
|
|
services.getUnreadCount.mockResolvedValue(0);
|
|
|
|
render(<NotificationsBell />);
|
|
const user = userEvent.setup();
|
|
await user.click(screen.getByLabelText("Notifications"));
|
|
expect(await screen.findByText("Strange kind")).toBeInTheDocument();
|
|
expect(screen.queryByTestId("notifications-panel")).not.toBeNull();
|
|
});
|
|
|
|
it("renders a relative 'at' phrase from createDateTime", async () => {
|
|
services.listNotifications.mockResolvedValue({
|
|
items: [item({ id: 1, title: "Points added" })],
|
|
total: 1,
|
|
});
|
|
services.getUnreadCount.mockResolvedValue(1);
|
|
|
|
render(<NotificationsBell />);
|
|
const user = userEvent.setup();
|
|
await user.click(await screen.findByLabelText("Notifications, 1 unread"));
|
|
const panel = await screen.findByTestId("notifications-panel");
|
|
expect(within(panel).getByText(/5 minutes ago/i)).toBeInTheDocument();
|
|
});
|
|
|
|
it("marks all notifications read via the panel action and refreshes", async () => {
|
|
services.listNotifications.mockResolvedValue({ items: [item({ id: 1 })], total: 1 });
|
|
services.getUnreadCount.mockResolvedValue(1);
|
|
services.markAllRead.mockResolvedValue({ marked: 1 });
|
|
|
|
render(<NotificationsBell />);
|
|
const user = userEvent.setup();
|
|
await user.click(await screen.findByLabelText("Notifications, 1 unread"));
|
|
await user.click(await screen.findByRole("button", { name: /mark all read/i }));
|
|
|
|
await waitFor(() => expect(services.markAllRead).toHaveBeenCalled());
|
|
});
|
|
});
|
|
|
|
describe("NotificationsBell — fulfilment notices", () => {
|
|
async function open(message: string) {
|
|
services.listNotifications.mockResolvedValue({
|
|
items: [item({ id: 9, type: "fulfilment", title: "FulHealthNoticeTitle", message, link: "/inventory/delivery-orders" })],
|
|
total: 1,
|
|
});
|
|
services.getUnreadCount.mockResolvedValue(1);
|
|
render(<NotificationsBell />);
|
|
await userEvent.setup().click(await screen.findByLabelText("Notifications, 1 unread"));
|
|
}
|
|
|
|
it("bellRendersFulfilmentNoticeThroughT", async () => {
|
|
await open(JSON.stringify({ code: "FulHealthNoticeBody", params: { count: 7, places: 2 } }));
|
|
expect(await screen.findByText("Overdue parcels")).toBeInTheDocument();
|
|
expect(screen.getByText("7 overdue across 2 places — open Delivery orders")).toBeInTheDocument();
|
|
});
|
|
|
|
it("bellFallsBackToRawTextOnBadJson", async () => {
|
|
await open("not json {");
|
|
expect(await screen.findByText("not json {")).toBeInTheDocument();
|
|
});
|
|
});
|