| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- import { assertEquals, cleanup, fireEvent, render, screen } from "./setup.ts";
- import PostList from "../../islands/PostList.tsx";
- const mockPosts = [
- { id: "1", title: "First Post", content: "Hello world", shared: false },
- { id: "2", title: "Second Post", content: "Another post", shared: true },
- { id: "3", title: "", content: "", shared: false },
- ];
- Deno.test({
- name: "PostList - renders correct number of post cards",
- fn() {
- const { container } = render(<PostList posts={mockPosts} />);
- const cards = container.querySelectorAll(".grid > div");
- assertEquals(cards.length, 3);
- cleanup();
- },
- sanitizeResources: false,
- sanitizeOps: false,
- });
- Deno.test({
- name: "PostList - displays post title",
- fn() {
- render(<PostList posts={mockPosts} />);
- assertEquals(screen.getByText("First Post") !== null, true);
- assertEquals(screen.getByText("Second Post") !== null, true);
- cleanup();
- },
- sanitizeResources: false,
- sanitizeOps: false,
- });
- Deno.test({
- name: "PostList - shows Untitled for empty title",
- fn() {
- render(<PostList posts={mockPosts} />);
- assertEquals(screen.getByText("Untitled") !== null, true);
- cleanup();
- },
- sanitizeResources: false,
- sanitizeOps: false,
- });
- Deno.test({
- name: "PostList - displays post content snippet",
- fn() {
- render(<PostList posts={mockPosts} />);
- assertEquals(screen.getByText("Hello world") !== null, true);
- assertEquals(screen.getByText("Another post") !== null, true);
- cleanup();
- },
- sanitizeResources: false,
- sanitizeOps: false,
- });
- Deno.test({
- name: "PostList - shows No content for empty content",
- fn() {
- render(<PostList posts={mockPosts} />);
- assertEquals(screen.getByText("No content") !== null, true);
- cleanup();
- },
- sanitizeResources: false,
- sanitizeOps: false,
- });
- Deno.test({
- name: "PostList - each card has an Edit button",
- fn() {
- render(<PostList posts={mockPosts} />);
- const editButtons = screen.getAllByText("Edit");
- assertEquals(editButtons.length, 3);
- cleanup();
- },
- sanitizeResources: false,
- sanitizeOps: false,
- });
- Deno.test({
- name: "PostList - each card has a delete icon",
- fn() {
- const { container } = render(<PostList posts={mockPosts} />);
- const deleteIcons = container.querySelectorAll(".bi-x");
- assertEquals(deleteIcons.length, 3);
- cleanup();
- },
- sanitizeResources: false,
- sanitizeOps: false,
- });
- Deno.test({
- name: "PostList - delete icon triggers $modal.show",
- fn() {
- let modalTitle = "";
- let modalContent = "";
- globalThis.$modal = {
- show: (title: string, content: string | preact.JSX.Element) => {
- modalTitle = title;
- modalContent = content as string;
- },
- hide: () => {},
- };
- const { container } = render(<PostList posts={[mockPosts[0]]} />);
- const deleteIcon = container.querySelector(".bi-x")!;
- fireEvent.click(deleteIcon);
- assertEquals(modalTitle, "Confirm delete");
- assertEquals(modalContent.includes("First Post"), true);
- delete globalThis.$modal;
- cleanup();
- },
- sanitizeResources: false,
- sanitizeOps: false,
- });
- Deno.test({
- name: "PostList - renders empty grid with no posts",
- fn() {
- const { container } = render(<PostList posts={[]} />);
- const cards = container.querySelectorAll(".grid > div");
- assertEquals(cards.length, 0);
- cleanup();
- },
- sanitizeResources: false,
- sanitizeOps: false,
- });
|