import React from "react";
import { YoutubeEmbed } from "./youtube-embed";
import { render, screen } from "@testing-library/react";
jest.mock("@shared/utils", () => ({
cx: (...args: any[]) => args.filter(Boolean).join(" "),
}));
describe("YoutubeEmbed", () => {
it("renders iframe with extracted video id from watch URL", () => {
render();
const iframe = screen.getByTitle("Embedded youtube");
expect(iframe.getAttribute("src")).toBe(
"https://www.youtube.com/embed/dQw4w9WgXcQ"
);
});
it("extracts id from youtu.be short URL", () => {
render();
expect(screen.getByTitle("Embedded youtube").getAttribute("src")).toContain(
"/embed/dQw4w9WgXcQ"
);
});
it("extracts id from embed URL", () => {
render();
expect(screen.getByTitle("Embedded youtube").getAttribute("src")).toContain(
"/embed/dQw4w9WgXcQ"
);
});
it("returns empty embedId for invalid URL", () => {
render();
expect(screen.getByTitle("Embedded youtube").getAttribute("src")).toBe(
"https://www.youtube.com/embed/"
);
});
it("returns empty embedId when link is empty", () => {
render();
expect(screen.getByTitle("Embedded youtube").getAttribute("src")).toBe(
"https://www.youtube.com/embed/"
);
});
it("returns empty embedId when link is undefined", () => {
render();
expect(screen.getByTitle("Embedded youtube").getAttribute("src")).toBe(
"https://www.youtube.com/embed/"
);
});
it("appends autoplay param when autoplay is true", () => {
render(
);
expect(screen.getByTitle("Embedded youtube").getAttribute("src")).toBe(
"https://www.youtube.com/embed/dQw4w9WgXcQ?autoplay=1"
);
});
it("does not append autoplay param when autoplay is false", () => {
render(
);
expect(
screen.getByTitle("Embedded youtube").getAttribute("src")
).not.toContain("autoplay");
});
it("applies containerClassName", () => {
const { container } = render(
);
expect(container.firstElementChild?.className).toContain("extra");
});
it("has correct iframe attributes", () => {
render();
const iframe = screen.getByTitle("Embedded youtube");
expect(iframe).toHaveAttribute(
"allow",
"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
);
expect(iframe).toHaveAttribute("allowFullScreen");
});
it("returns empty embedId when match length is not 11", () => {
render();
expect(screen.getByTitle("Embedded youtube").getAttribute("src")).toBe(
"https://www.youtube.com/embed/"
);
});
});