modal_test.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import {
  2. act,
  3. assertEquals,
  4. cleanup,
  5. fireEvent,
  6. render,
  7. screen,
  8. } from "./setup.ts";
  9. import Modal from "../../islands/Modal.tsx";
  10. Deno.test({
  11. name: "Modal - initially hidden",
  12. fn() {
  13. const { container } = render(<Modal />);
  14. const overlay = container.firstElementChild!;
  15. assertEquals(overlay.className.includes("hidden"), true);
  16. cleanup();
  17. },
  18. sanitizeResources: false,
  19. sanitizeOps: false,
  20. });
  21. Deno.test({
  22. name: "Modal - registers globalThis.$modal",
  23. fn() {
  24. render(<Modal />);
  25. assertEquals(typeof globalThis.$modal, "object");
  26. assertEquals(typeof globalThis.$modal!.show, "function");
  27. assertEquals(typeof globalThis.$modal!.hide, "function");
  28. cleanup();
  29. },
  30. sanitizeResources: false,
  31. sanitizeOps: false,
  32. });
  33. Deno.test({
  34. name: "Modal - show() displays with title and content",
  35. fn() {
  36. const { container } = render(<Modal />);
  37. act(() => {
  38. globalThis.$modal!.show("Test Title", "Test Content", []);
  39. });
  40. const overlay = container.firstElementChild!;
  41. assertEquals(overlay.className.includes("hidden"), false);
  42. assertEquals(screen.getByText("Test Title") !== null, true);
  43. assertEquals(screen.getByText("Test Content") !== null, true);
  44. cleanup();
  45. },
  46. sanitizeResources: false,
  47. sanitizeOps: false,
  48. });
  49. Deno.test({
  50. name: "Modal - renders action buttons",
  51. fn() {
  52. render(<Modal />);
  53. act(() => {
  54. globalThis.$modal!.show("Title", "Content", [
  55. { text: "OK" },
  56. { text: "Cancel" },
  57. ]);
  58. });
  59. assertEquals(screen.getByText("OK").tagName, "BUTTON");
  60. assertEquals(screen.getByText("Cancel").tagName, "BUTTON");
  61. cleanup();
  62. },
  63. sanitizeResources: false,
  64. sanitizeOps: false,
  65. });
  66. Deno.test({
  67. name: "Modal - action button with onClick calls handler",
  68. fn() {
  69. let clicked = "";
  70. render(<Modal />);
  71. act(() => {
  72. globalThis.$modal!.show("Title", "Content", [
  73. {
  74. text: "Confirm",
  75. onClick: (text: string) => {
  76. clicked = text;
  77. },
  78. },
  79. ]);
  80. });
  81. fireEvent.click(screen.getByText("Confirm"));
  82. assertEquals(clicked, "Confirm");
  83. cleanup();
  84. },
  85. sanitizeResources: false,
  86. sanitizeOps: false,
  87. });
  88. Deno.test({
  89. name: "Modal - action button without onClick hides modal",
  90. fn() {
  91. const { container } = render(<Modal />);
  92. act(() => {
  93. globalThis.$modal!.show("Title", "Content", [{ text: "Close" }]);
  94. });
  95. act(() => {
  96. fireEvent.click(screen.getByText("Close"));
  97. });
  98. const overlay = container.firstElementChild!;
  99. assertEquals(overlay.className.includes("hidden"), true);
  100. cleanup();
  101. },
  102. sanitizeResources: false,
  103. sanitizeOps: false,
  104. });
  105. Deno.test({
  106. name: "Modal - close icon hides modal",
  107. fn() {
  108. const { container } = render(<Modal />);
  109. act(() => {
  110. globalThis.$modal!.show("Title", "Content", []);
  111. });
  112. const closeIcon = container.querySelector(".bi-x")!;
  113. act(() => {
  114. fireEvent.click(closeIcon);
  115. });
  116. const overlay = container.firstElementChild!;
  117. assertEquals(overlay.className.includes("hidden"), true);
  118. cleanup();
  119. },
  120. sanitizeResources: false,
  121. sanitizeOps: false,
  122. });
  123. Deno.test({
  124. name: "Modal - cleans up globalThis.$modal on unmount",
  125. fn() {
  126. render(<Modal />);
  127. assertEquals(globalThis.$modal !== undefined, true);
  128. cleanup();
  129. assertEquals(globalThis.$modal, undefined);
  130. },
  131. sanitizeResources: false,
  132. sanitizeOps: false,
  133. });