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
354 lines
13 KiB
TypeScript
354 lines
13 KiB
TypeScript
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
import { render, screen, fireEvent, act, waitFor } from "@testing-library/react";
|
|
import HomePromoCarousel from "./HomePromoCarousel";
|
|
import { ThemeProvider } from "@/app/lib/ThemeContext";
|
|
import type { AppPromo } from "@/app/lib/types";
|
|
|
|
function promo(
|
|
over: Partial<AppPromo> & { id: number; mediaType: AppPromo["mediaType"]; mediaUrl: string },
|
|
): AppPromo {
|
|
return { sortIndex: 0, ...over };
|
|
}
|
|
|
|
/** Stubs window.matchMedia so components reading prefers-reduced-motion get a fixed answer. */
|
|
function mockReducedMotion(matches: boolean) {
|
|
window.matchMedia = vi.fn().mockImplementation((query: string) => ({
|
|
matches,
|
|
media: query,
|
|
addEventListener: vi.fn(),
|
|
removeEventListener: vi.fn(),
|
|
addListener: vi.fn(),
|
|
removeListener: vi.fn(),
|
|
dispatchEvent: vi.fn(),
|
|
}));
|
|
}
|
|
|
|
beforeEach(() => {
|
|
mockReducedMotion(false);
|
|
HTMLMediaElement.prototype.play = vi.fn().mockResolvedValue(undefined);
|
|
HTMLMediaElement.prototype.pause = vi.fn();
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
describe("HomePromoCarousel", () => {
|
|
it("renders nothing when there are no promos", () => {
|
|
const { container } = render(<HomePromoCarousel promos={[]} />);
|
|
expect(container).toBeEmptyDOMElement();
|
|
expect(screen.queryByRole("region")).toBeNull();
|
|
});
|
|
|
|
it("renders an image slide with alt text", () => {
|
|
render(
|
|
<HomePromoCarousel
|
|
promos={[
|
|
promo({ id: 1, mediaType: "IMAGE", mediaUrl: "https://cdn.example.com/one.jpg", altText: "Grand sale banner" }),
|
|
]}
|
|
/>,
|
|
);
|
|
const img = screen.getByAltText("Grand sale banner");
|
|
expect(img).toHaveAttribute("src", "https://cdn.example.com/one.jpg");
|
|
});
|
|
|
|
it("autoplays only the active video slide (muted, looped, inline) and pauses the inactive one", () => {
|
|
render(
|
|
<HomePromoCarousel
|
|
promos={[
|
|
promo({ id: 1, mediaType: "VIDEO", mediaUrl: "https://cdn.example.com/one.mp4" }),
|
|
promo({ id: 2, mediaType: "VIDEO", mediaUrl: "https://cdn.example.com/two.mp4" }),
|
|
]}
|
|
/>,
|
|
);
|
|
|
|
const videos = document.querySelectorAll("video");
|
|
expect(videos).toHaveLength(2);
|
|
const [first, second] = Array.from(videos) as HTMLVideoElement[];
|
|
|
|
expect(first.muted).toBe(true);
|
|
expect(first.loop).toBe(true);
|
|
expect(first.playsInline).toBe(true);
|
|
expect(first.preload).toBe("none");
|
|
expect(second.preload).toBe("none");
|
|
expect(first.play).toHaveBeenCalled();
|
|
expect(second.pause).toHaveBeenCalled();
|
|
|
|
// Advance to the second slide via its dot indicator.
|
|
fireEvent.click(screen.getByTestId("home-promo-dot-1"));
|
|
|
|
expect(second.play).toHaveBeenCalled();
|
|
expect(first.pause).toHaveBeenCalled();
|
|
});
|
|
|
|
it("renders an internal-link promo as a Next Link to that path", () => {
|
|
render(
|
|
<HomePromoCarousel
|
|
promos={[
|
|
promo({ id: 1, mediaType: "IMAGE", mediaUrl: "https://cdn.example.com/one.jpg", altText: "Sale", linkUrl: "/store" }),
|
|
]}
|
|
/>,
|
|
);
|
|
const link = screen.getByRole("link");
|
|
expect(link).toHaveAttribute("href", "/store");
|
|
expect(link).not.toHaveAttribute("target", "_blank");
|
|
});
|
|
|
|
it("opens an external-link promo in a new tab with a safe rel", () => {
|
|
render(
|
|
<HomePromoCarousel
|
|
promos={[
|
|
promo({
|
|
id: 1,
|
|
mediaType: "IMAGE",
|
|
mediaUrl: "https://cdn.example.com/one.jpg",
|
|
altText: "Sale",
|
|
linkUrl: "https://example.com",
|
|
}),
|
|
]}
|
|
/>,
|
|
);
|
|
const link = screen.getByRole("link");
|
|
expect(link).toHaveAttribute("href", "https://example.com");
|
|
expect(link).toHaveAttribute("target", "_blank");
|
|
expect(link).toHaveAttribute("rel", expect.stringContaining("noopener"));
|
|
});
|
|
|
|
it("renders a promo without a link as non-interactive (no anchor)", () => {
|
|
render(
|
|
<HomePromoCarousel
|
|
promos={[promo({ id: 1, mediaType: "IMAGE", mediaUrl: "https://cdn.example.com/one.jpg", altText: "Sale" })]}
|
|
/>,
|
|
);
|
|
expect(screen.queryByRole("link")).toBeNull();
|
|
});
|
|
|
|
it("renders the caption as a visible overlay on an image slide when present", () => {
|
|
render(
|
|
<HomePromoCarousel
|
|
promos={[
|
|
promo({
|
|
id: 1,
|
|
mediaType: "IMAGE",
|
|
mediaUrl: "https://cdn.example.com/one.jpg",
|
|
altText: "Sale",
|
|
caption: "Big summer sale",
|
|
}),
|
|
]}
|
|
/>,
|
|
);
|
|
const overlay = screen.getByTestId("home-promo-caption");
|
|
expect(overlay).toHaveTextContent("Big summer sale");
|
|
expect(overlay).toHaveStyle({ pointerEvents: "none" });
|
|
});
|
|
|
|
it("renders the caption as a visible overlay on a video slide when present", () => {
|
|
render(
|
|
<HomePromoCarousel
|
|
promos={[
|
|
promo({
|
|
id: 1,
|
|
mediaType: "VIDEO",
|
|
mediaUrl: "https://cdn.example.com/one.mp4",
|
|
caption: "Now streaming",
|
|
}),
|
|
]}
|
|
/>,
|
|
);
|
|
expect(screen.getByTestId("home-promo-caption")).toHaveTextContent("Now streaming");
|
|
});
|
|
|
|
it("renders no caption overlay when the promo has no caption", () => {
|
|
render(
|
|
<HomePromoCarousel
|
|
promos={[promo({ id: 1, mediaType: "IMAGE", mediaUrl: "https://cdn.example.com/one.jpg", altText: "Sale" })]}
|
|
/>,
|
|
);
|
|
expect(screen.queryByTestId("home-promo-caption")).toBeNull();
|
|
});
|
|
|
|
it("auto-advances to the next slide after a delay", () => {
|
|
vi.useFakeTimers();
|
|
render(
|
|
<HomePromoCarousel
|
|
promos={[
|
|
promo({ id: 1, mediaType: "IMAGE", mediaUrl: "https://cdn.example.com/one.jpg", altText: "First" }),
|
|
promo({ id: 2, mediaType: "IMAGE", mediaUrl: "https://cdn.example.com/two.jpg", altText: "Second" }),
|
|
]}
|
|
/>,
|
|
);
|
|
expect(screen.getByTestId("home-promo-dot-0")).toHaveAttribute("aria-current", "true");
|
|
act(() => {
|
|
vi.advanceTimersByTime(6000);
|
|
});
|
|
expect(screen.getByTestId("home-promo-dot-1")).toHaveAttribute("aria-current", "true");
|
|
});
|
|
|
|
it("does not auto-advance or autoplay video when the user prefers reduced motion", () => {
|
|
mockReducedMotion(true);
|
|
vi.useFakeTimers();
|
|
render(
|
|
<HomePromoCarousel
|
|
promos={[
|
|
promo({ id: 1, mediaType: "VIDEO", mediaUrl: "https://cdn.example.com/one.mp4" }),
|
|
promo({ id: 2, mediaType: "IMAGE", mediaUrl: "https://cdn.example.com/two.jpg", altText: "Second" }),
|
|
]}
|
|
/>,
|
|
);
|
|
|
|
const video = document.querySelector("video") as HTMLVideoElement;
|
|
expect(video.play).not.toHaveBeenCalled();
|
|
|
|
act(() => {
|
|
vi.advanceTimersByTime(10000);
|
|
});
|
|
expect(screen.getByTestId("home-promo-dot-0")).toHaveAttribute("aria-current", "true");
|
|
});
|
|
|
|
it("advances to the next slide on a left swipe past the threshold", () => {
|
|
render(
|
|
<HomePromoCarousel
|
|
promos={[
|
|
promo({ id: 1, mediaType: "IMAGE", mediaUrl: "https://cdn.example.com/one.jpg", altText: "First" }),
|
|
promo({ id: 2, mediaType: "IMAGE", mediaUrl: "https://cdn.example.com/two.jpg", altText: "Second" }),
|
|
promo({ id: 3, mediaType: "IMAGE", mediaUrl: "https://cdn.example.com/three.jpg", altText: "Third" }),
|
|
]}
|
|
/>,
|
|
);
|
|
const carousel = screen.getByTestId("home-promo-carousel");
|
|
expect(screen.getByTestId("home-promo-dot-0")).toHaveAttribute("aria-current", "true");
|
|
|
|
// Finger travels from right to left by more than the swipe threshold.
|
|
fireEvent.touchStart(carousel, { touches: [{ clientX: 200 }] });
|
|
fireEvent.touchEnd(carousel, { changedTouches: [{ clientX: 150 }] });
|
|
|
|
expect(screen.getByTestId("home-promo-dot-1")).toHaveAttribute("aria-current", "true");
|
|
});
|
|
|
|
it("returns to the previous slide on a right swipe past the threshold", () => {
|
|
render(
|
|
<HomePromoCarousel
|
|
promos={[
|
|
promo({ id: 1, mediaType: "IMAGE", mediaUrl: "https://cdn.example.com/one.jpg", altText: "First" }),
|
|
promo({ id: 2, mediaType: "IMAGE", mediaUrl: "https://cdn.example.com/two.jpg", altText: "Second" }),
|
|
promo({ id: 3, mediaType: "IMAGE", mediaUrl: "https://cdn.example.com/three.jpg", altText: "Third" }),
|
|
]}
|
|
/>,
|
|
);
|
|
const carousel = screen.getByTestId("home-promo-carousel");
|
|
fireEvent.click(screen.getByTestId("home-promo-dot-1"));
|
|
expect(screen.getByTestId("home-promo-dot-1")).toHaveAttribute("aria-current", "true");
|
|
|
|
// Finger travels from left to right by more than the swipe threshold.
|
|
fireEvent.touchStart(carousel, { touches: [{ clientX: 150 }] });
|
|
fireEvent.touchEnd(carousel, { changedTouches: [{ clientX: 200 }] });
|
|
|
|
expect(screen.getByTestId("home-promo-dot-0")).toHaveAttribute("aria-current", "true");
|
|
});
|
|
|
|
it("ignores a drag shorter than the swipe threshold", () => {
|
|
render(
|
|
<HomePromoCarousel
|
|
promos={[
|
|
promo({ id: 1, mediaType: "IMAGE", mediaUrl: "https://cdn.example.com/one.jpg", altText: "First" }),
|
|
promo({ id: 2, mediaType: "IMAGE", mediaUrl: "https://cdn.example.com/two.jpg", altText: "Second" }),
|
|
]}
|
|
/>,
|
|
);
|
|
const carousel = screen.getByTestId("home-promo-carousel");
|
|
|
|
// Drag of only 10px is below the 40px threshold, so it should not change slides.
|
|
fireEvent.touchStart(carousel, { touches: [{ clientX: 200 }] });
|
|
fireEvent.touchEnd(carousel, { changedTouches: [{ clientX: 190 }] });
|
|
|
|
expect(screen.getByTestId("home-promo-dot-0")).toHaveAttribute("aria-current", "true");
|
|
});
|
|
|
|
it("applies an exact pixel height to the media viewport when a height prop is given", () => {
|
|
render(
|
|
<HomePromoCarousel
|
|
height={140}
|
|
promos={[promo({ id: 1, mediaType: "IMAGE", mediaUrl: "https://cdn.example.com/one.jpg", altText: "Sale" })]}
|
|
/>,
|
|
);
|
|
const viewport = screen.getByTestId("home-promo-viewport");
|
|
expect(viewport).toHaveStyle({ height: "140px" });
|
|
expect(viewport.className).not.toContain("aspect-[16/9]");
|
|
});
|
|
|
|
it("falls back to the 16/9 aspect ratio when no height prop is given", () => {
|
|
render(
|
|
<HomePromoCarousel
|
|
promos={[promo({ id: 1, mediaType: "IMAGE", mediaUrl: "https://cdn.example.com/one.jpg", altText: "Sale" })]}
|
|
/>,
|
|
);
|
|
const viewport = screen.getByTestId("home-promo-viewport");
|
|
expect(viewport.className).toContain("aspect-[16/9]");
|
|
expect(viewport).not.toHaveStyle({ height: "140px" });
|
|
});
|
|
|
|
it("renders the scroll dots below the media box, not overlaid on it", () => {
|
|
render(
|
|
<HomePromoCarousel
|
|
promos={[
|
|
promo({ id: 1, mediaType: "IMAGE", mediaUrl: "https://cdn.example.com/one.jpg", altText: "First" }),
|
|
promo({ id: 2, mediaType: "IMAGE", mediaUrl: "https://cdn.example.com/two.jpg", altText: "Second" }),
|
|
]}
|
|
/>,
|
|
);
|
|
const viewport = screen.getByTestId("home-promo-viewport");
|
|
const firstDot = screen.getByTestId("home-promo-dot-0");
|
|
// Dot is a sibling AFTER the viewport, not nested inside the overflow-clipped media box.
|
|
expect(viewport.contains(firstDot)).toBe(false);
|
|
expect(viewport.compareDocumentPosition(firstDot) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
|
|
// The dot's row is in normal flow, not absolutely positioned over the media.
|
|
expect((firstDot.parentElement as HTMLElement).className).not.toContain("absolute");
|
|
});
|
|
|
|
it("colors the active dot with the orange accent", () => {
|
|
render(
|
|
<HomePromoCarousel
|
|
promos={[
|
|
promo({ id: 1, mediaType: "IMAGE", mediaUrl: "https://cdn.example.com/one.jpg", altText: "First" }),
|
|
promo({ id: 2, mediaType: "IMAGE", mediaUrl: "https://cdn.example.com/two.jpg", altText: "Second" }),
|
|
]}
|
|
/>,
|
|
);
|
|
expect(screen.getByTestId("home-promo-dot-0")).toHaveStyle({ background: "rgb(255, 107, 0)" });
|
|
});
|
|
|
|
it("uses the light-mode inactive dot color by default", () => {
|
|
render(
|
|
<HomePromoCarousel
|
|
promos={[
|
|
promo({ id: 1, mediaType: "IMAGE", mediaUrl: "https://cdn.example.com/one.jpg", altText: "First" }),
|
|
promo({ id: 2, mediaType: "IMAGE", mediaUrl: "https://cdn.example.com/two.jpg", altText: "Second" }),
|
|
]}
|
|
/>,
|
|
);
|
|
// Dot 1 is inactive (dot 0 is active on first render).
|
|
expect(screen.getByTestId("home-promo-dot-1")).toHaveStyle({ background: "rgba(0, 0, 0, 0.2)" });
|
|
});
|
|
|
|
it("uses the dark-mode inactive dot color under the dark theme", async () => {
|
|
localStorage.setItem("te-theme", "dark");
|
|
try {
|
|
render(
|
|
<ThemeProvider>
|
|
<HomePromoCarousel
|
|
promos={[
|
|
promo({ id: 1, mediaType: "IMAGE", mediaUrl: "https://cdn.example.com/one.jpg", altText: "First" }),
|
|
promo({ id: 2, mediaType: "IMAGE", mediaUrl: "https://cdn.example.com/two.jpg", altText: "Second" }),
|
|
]}
|
|
/>
|
|
</ThemeProvider>,
|
|
);
|
|
await waitFor(() =>
|
|
expect(screen.getByTestId("home-promo-dot-1")).toHaveStyle({ background: "rgba(255, 255, 255, 0.35)" }),
|
|
);
|
|
} finally {
|
|
localStorage.removeItem("te-theme");
|
|
}
|
|
});
|
|
});
|