import React from "react";
import { VimeoEmbed } from "./vimeo-embed";
import { render, screen } from "@testing-library/react";
jest.mock("@shared/utils", () => ({
cx: (...args: any[]) => args.filter(Boolean).join(" "),
}));
describe("VimeoEmbed", () => {
describe("Rendering", () => {
it("renders iframe with correct src for standard vimeo link", () => {
render();
const iframe = screen.getByTitle("Embedded vimeo");
expect(iframe).toBeInTheDocument();
expect(iframe.getAttribute("src")).toContain(
"player.vimeo.com/video/123456789"
);
});
it("renders null when link is empty", () => {
const { container } = render();
expect(container.innerHTML).toBe("");
});
it("renders null when link is undefined", () => {
const { container } = render();
expect(container.innerHTML).toBe("");
});
it("renders null when link has no valid vimeo id", () => {
const { container } = render(
);
expect(container.innerHTML).toBe("");
});
it("applies containerClassName", () => {
const { container } = render(
);
expect(container.firstElementChild?.className).toContain("custom");
});
it("sets autoplay=1 when autoplay is true", () => {
render();
const iframe = screen.getByTitle("Embedded vimeo");
expect(iframe.getAttribute("src")).toContain("autoplay=1");
});
it("sets autoplay=0 when autoplay is false", () => {
render(
);
const iframe = screen.getByTitle("Embedded vimeo");
expect(iframe.getAttribute("src")).toContain("autoplay=0");
});
it("has correct iframe attributes", () => {
render();
const iframe = screen.getByTitle("Embedded vimeo");
expect(iframe).toHaveAttribute(
"allow",
"autoplay; fullscreen; picture-in-picture"
);
expect(iframe).toHaveAttribute(
"referrerPolicy",
"strict-origin-when-cross-origin"
);
});
});
describe("extractVimeoIdandHash", () => {
it("extracts id from standard vimeo.com URL", () => {
render();
expect(screen.getByTitle("Embedded vimeo").getAttribute("src")).toContain(
"/video/123456789"
);
});
it("extracts id and hash from vimeo URL with hash path", () => {
render();
const src = screen.getByTitle("Embedded vimeo").getAttribute("src")!;
expect(src).toContain("/video/123456789");
expect(src).toContain("h=abc123def");
});
it("extracts id from player.vimeo.com URL", () => {
render();
expect(screen.getByTitle("Embedded vimeo").getAttribute("src")).toContain(
"/video/987654321"
);
});
it("strips query params from link before extracting", () => {
render();
expect(screen.getByTitle("Embedded vimeo").getAttribute("src")).toContain(
"/video/123456789"
);
});
it("strips trailing slashes from link", () => {
render();
expect(screen.getByTitle("Embedded vimeo").getAttribute("src")).toContain(
"/video/123456789"
);
});
it("handles vimeo URL with nested path", () => {
render(
);
expect(screen.getByTitle("Embedded vimeo").getAttribute("src")).toContain(
"/video/123456789"
);
});
});
describe("debug mode", () => {
it("logs to console when debug is true", () => {
const consoleSpy = jest.spyOn(console, "log").mockImplementation();
render();
expect(consoleSpy).toHaveBeenCalledWith(
"[VimeoEmbed] href:",
"https://vimeo.com/123456789",
"id:",
expect.any(String)
);
consoleSpy.mockRestore();
});
it("does not log when debug is false", () => {
const consoleSpy = jest.spyOn(console, "log").mockImplementation();
render();
expect(consoleSpy).not.toHaveBeenCalled();
consoleSpy.mockRestore();
});
});
});