Files
co-lite/solvway-customer-portal/app/lib/ToastContext.test.tsx
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
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
2026-11-26 15:55:45 +08:00

123 lines
3.5 KiB
TypeScript

import { render, screen, fireEvent, act, waitFor } from "@testing-library/react";
import { describe, it, expect, vi, afterEach } from "vitest";
vi.mock("@/app/lib/ThemeContext", () => ({
useTheme: () => ({ isDark: false, toggle: vi.fn() }),
}));
// Skip exit animations so unmount timing reflects the auto-dismiss timer only.
vi.mock("framer-motion", () => ({
AnimatePresence: ({ children }: { children: React.ReactNode }) => <>{children}</>,
motion: {
div: ({
children,
role,
onClick,
className,
initial,
animate,
exit,
}: {
children: React.ReactNode;
role?: string;
onClick?: () => void;
className?: string;
initial?: { x?: string };
animate?: { x?: string };
exit?: { x?: string };
}) => (
<div
role={role}
onClick={onClick}
className={className}
data-initial-x={initial?.x}
data-animate-x={animate?.x}
data-exit-x={exit?.x}
>
{children}
</div>
),
},
}));
import { ToastProvider, useToast } from "./ToastContext";
const MSG = "Profile updated";
// Minimal consumer that fires a toast when its button is clicked.
function Trigger({ message = MSG }: { message?: string }) {
const { showToast } = useToast();
return <button onClick={() => showToast(message)}>fire</button>;
}
afterEach(() => {
vi.useRealTimers();
});
describe("ToastContext", () => {
it("renders the message when showToast is called", () => {
render(
<ToastProvider>
<Trigger />
</ToastProvider>,
);
expect(screen.queryByText(MSG)).toBeNull();
fireEvent.click(screen.getByText("fire"));
expect(screen.getByText(MSG)).toBeInTheDocument();
});
it("stays visible for 3.5s and is gone by 4s", async () => {
vi.useFakeTimers({ shouldAdvanceTime: true });
render(
<ToastProvider>
<Trigger />
</ToastProvider>,
);
fireEvent.click(screen.getByText("fire"));
expect(screen.getByText(MSG)).toBeInTheDocument();
await act(async () => {
await vi.advanceTimersByTimeAsync(3500);
});
expect(screen.getByText(MSG)).toBeInTheDocument();
await act(async () => {
await vi.advanceTimersByTimeAsync(500);
});
await waitFor(() => expect(screen.queryByText(MSG)).toBeNull());
});
it("dismisses the toast when it is clicked", async () => {
render(
<ToastProvider>
<Trigger />
</ToastProvider>,
);
fireEvent.click(screen.getByText("fire"));
const toast = screen.getByText(MSG);
fireEvent.click(toast);
await waitFor(() => expect(screen.queryByText(MSG)).toBeNull());
});
it("centres the toast via framer-motion transform, not the -translate-x-1/2 class", async () => {
render(
<ToastProvider>
<Trigger message="Profile updated · +500 points earned" />
</ToastProvider>,
);
fireEvent.click(screen.getByText("fire"));
const toast = screen.getByRole("status");
// framer-motion is mocked, so assert the x offsets it is given (it emits translateX(-50%)).
expect(toast).toHaveAttribute("data-initial-x", "-50%");
expect(toast).toHaveAttribute("data-animate-x", "-50%");
expect(toast).toHaveAttribute("data-exit-x", "-50%");
expect(toast).not.toHaveClass("-translate-x-1/2");
});
it("is a no-op (does not throw) when useToast is used outside a provider", () => {
render(<Trigger />);
expect(() => fireEvent.click(screen.getByText("fire"))).not.toThrow();
expect(screen.queryByText(MSG)).toBeNull();
});
});