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 & { 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(); expect(container).toBeEmptyDOMElement(); expect(screen.queryByRole("region")).toBeNull(); }); it("renders an image slide with alt text", () => { render( , ); 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( , ); 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( , ); 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( , ); 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( , ); expect(screen.queryByRole("link")).toBeNull(); }); it("renders the caption as a visible overlay on an image slide when present", () => { render( , ); 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( , ); expect(screen.getByTestId("home-promo-caption")).toHaveTextContent("Now streaming"); }); it("renders no caption overlay when the promo has no caption", () => { render( , ); expect(screen.queryByTestId("home-promo-caption")).toBeNull(); }); it("auto-advances to the next slide after a delay", () => { vi.useFakeTimers(); render( , ); 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( , ); 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( , ); 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( , ); 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( , ); 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( , ); 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( , ); 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( , ); 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( , ); expect(screen.getByTestId("home-promo-dot-0")).toHaveStyle({ background: "rgb(255, 107, 0)" }); }); it("uses the light-mode inactive dot color by default", () => { render( , ); // 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( , ); await waitFor(() => expect(screen.getByTestId("home-promo-dot-1")).toHaveStyle({ background: "rgba(255, 255, 255, 0.35)" }), ); } finally { localStorage.removeItem("te-theme"); } }); });