import { describe, expect, test, vi } from "vitest";
import { screen } from "@testing-library/react";
import { userEvent } from "@testing-library/user-event";
import { PlaylistCard } from "../../../components/playlist/PlaylistCard.tsx";
import { makePlaylist } from "../../fixtures.ts";
import { currentPath, renderWithContexts } from "../../render.tsx";
import { markPlaylistVisited } from "../../../utils/shared/visited.ts";
describe("PlaylistCard", () => {
test("title, badge, owner, count; clicking opens it and marks it seen", async () => {
const user = userEvent.setup();
const p = makePlaylist({
id: "p1",
slug: "mix",
title: "Mix",
ownerUsername: "ann",
dumpCount: 1,
isPublic: false,
description: "Tunes",
});
const { container } = renderWithContexts(
,
);
expect(screen.getByRole("link", { name: /Mix/ })).toHaveAttribute(
"href",
"/@ann/playlists/mix",
);
expect(screen.getByText("private")).toBeInTheDocument();
expect(screen.getByRole("link", { name: "@ann" })).toHaveAttribute(
"href",
"/@ann",
);
expect(screen.getByText("1 dump")).toBeInTheDocument();
expect(screen.getByText("Tunes")).toBeInTheDocument();
await user.click(container.querySelector(".playlist-card-inner")!);
expect(currentPath()).toBe("/@ann/playlists/mix");
});
test("the owner doesn't see their own name, or an unread dot", () => {
const p = makePlaylist({ ownerUsername: "me", createdAt: new Date() });
const { container } = renderWithContexts(
,
);
expect(screen.queryByText("@me")).not.toBeInTheDocument();
expect(container.querySelector(".unread-dot")).toBeNull();
});
test("a new playlist has a dot until visited", () => {
const p = makePlaylist({ id: "fresh", createdAt: new Date() });
const first = renderWithContexts(
,
);
expect(first.container.querySelector(".unread-dot")).not.toBeNull();
first.unmount();
markPlaylistVisited("fresh");
const again = renderWithContexts(
,
);
expect(again.container.querySelector(".unread-dot")).toBeNull();
});
test("privateBadgeOnly shows only the owner's private badge", () => {
const cases = [
{ isPublic: true, isOwner: true, shown: false },
{ isPublic: false, isOwner: false, shown: false },
{ isPublic: false, isOwner: true, shown: true },
];
for (const { isPublic, isOwner, shown } of cases) {
const { container, unmount } = renderWithContexts(
,
);
expect(container.querySelector(".playlist-badge") !== null).toBe(shown);
unmount();
}
});
test("delete doesn't open the playlist", async () => {
const onDelete = vi.fn();
const user = userEvent.setup();
renderWithContexts(
,
);
await user.click(screen.getByRole("button", { name: "Delete playlist" }));
expect(onDelete).toHaveBeenCalled();
expect(currentPath()).toBe("/");
});
});