post_list_test.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { assertEquals, cleanup, fireEvent, render, screen } from "./setup.ts";
  2. import PostList from "../../islands/PostList.tsx";
  3. const mockPosts = [
  4. { id: "1", title: "First Post", content: "Hello world", shared: false },
  5. { id: "2", title: "Second Post", content: "Another post", shared: true },
  6. { id: "3", title: "", content: "", shared: false },
  7. ];
  8. Deno.test({
  9. name: "PostList - renders correct number of post cards",
  10. fn() {
  11. const { container } = render(<PostList posts={mockPosts} />);
  12. const cards = container.querySelectorAll(".grid > div");
  13. assertEquals(cards.length, 3);
  14. cleanup();
  15. },
  16. sanitizeResources: false,
  17. sanitizeOps: false,
  18. });
  19. Deno.test({
  20. name: "PostList - displays post title",
  21. fn() {
  22. render(<PostList posts={mockPosts} />);
  23. assertEquals(screen.getByText("First Post") !== null, true);
  24. assertEquals(screen.getByText("Second Post") !== null, true);
  25. cleanup();
  26. },
  27. sanitizeResources: false,
  28. sanitizeOps: false,
  29. });
  30. Deno.test({
  31. name: "PostList - shows Untitled for empty title",
  32. fn() {
  33. render(<PostList posts={mockPosts} />);
  34. assertEquals(screen.getByText("Untitled") !== null, true);
  35. cleanup();
  36. },
  37. sanitizeResources: false,
  38. sanitizeOps: false,
  39. });
  40. Deno.test({
  41. name: "PostList - displays post content snippet",
  42. fn() {
  43. render(<PostList posts={mockPosts} />);
  44. assertEquals(screen.getByText("Hello world") !== null, true);
  45. assertEquals(screen.getByText("Another post") !== null, true);
  46. cleanup();
  47. },
  48. sanitizeResources: false,
  49. sanitizeOps: false,
  50. });
  51. Deno.test({
  52. name: "PostList - shows No content for empty content",
  53. fn() {
  54. render(<PostList posts={mockPosts} />);
  55. assertEquals(screen.getByText("No content") !== null, true);
  56. cleanup();
  57. },
  58. sanitizeResources: false,
  59. sanitizeOps: false,
  60. });
  61. Deno.test({
  62. name: "PostList - each card has an Edit button",
  63. fn() {
  64. render(<PostList posts={mockPosts} />);
  65. const editButtons = screen.getAllByText("Edit");
  66. assertEquals(editButtons.length, 3);
  67. cleanup();
  68. },
  69. sanitizeResources: false,
  70. sanitizeOps: false,
  71. });
  72. Deno.test({
  73. name: "PostList - each card has a delete icon",
  74. fn() {
  75. const { container } = render(<PostList posts={mockPosts} />);
  76. const deleteIcons = container.querySelectorAll(".bi-x");
  77. assertEquals(deleteIcons.length, 3);
  78. cleanup();
  79. },
  80. sanitizeResources: false,
  81. sanitizeOps: false,
  82. });
  83. Deno.test({
  84. name: "PostList - delete icon triggers $modal.show",
  85. fn() {
  86. let modalTitle = "";
  87. let modalContent = "";
  88. globalThis.$modal = {
  89. show: (title: string, content: string | preact.JSX.Element) => {
  90. modalTitle = title;
  91. modalContent = content as string;
  92. },
  93. hide: () => {},
  94. };
  95. const { container } = render(<PostList posts={[mockPosts[0]]} />);
  96. const deleteIcon = container.querySelector(".bi-x")!;
  97. fireEvent.click(deleteIcon);
  98. assertEquals(modalTitle, "Confirm delete");
  99. assertEquals(modalContent.includes("First Post"), true);
  100. delete globalThis.$modal;
  101. cleanup();
  102. },
  103. sanitizeResources: false,
  104. sanitizeOps: false,
  105. });
  106. Deno.test({
  107. name: "PostList - renders empty grid with no posts",
  108. fn() {
  109. const { container } = render(<PostList posts={[]} />);
  110. const cards = container.querySelectorAll(".grid > div");
  111. assertEquals(cards.length, 0);
  112. cleanup();
  113. },
  114. sanitizeResources: false,
  115. sanitizeOps: false,
  116. });