input_test.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { assertEquals, cleanup, render, screen } from "./setup.ts";
  2. import Input from "../../components/form/Input.tsx";
  3. Deno.test({
  4. name: "Input - renders bare input without label",
  5. fn() {
  6. render(<Input placeholder="Enter text" />);
  7. const input = screen.getByPlaceholderText("Enter text");
  8. assertEquals(input.tagName, "INPUT");
  9. // No wrapping div or label
  10. assertEquals(input.parentElement?.tagName, "DIV"); // render container
  11. cleanup();
  12. },
  13. sanitizeResources: false,
  14. sanitizeOps: false,
  15. });
  16. Deno.test({
  17. name: "Input - renders with label wrapper",
  18. fn() {
  19. render(<Input label="Email" placeholder="email" />);
  20. const label = screen.getByText("Email");
  21. assertEquals(label.tagName, "LABEL");
  22. const input = screen.getByPlaceholderText("email");
  23. assertEquals(input.tagName, "INPUT");
  24. cleanup();
  25. },
  26. sanitizeResources: false,
  27. sanitizeOps: false,
  28. });
  29. Deno.test({
  30. name: "Input - applies error border when error=true",
  31. fn() {
  32. render(<Input error={true} placeholder="err" />);
  33. const input = screen.getByPlaceholderText("err");
  34. assertEquals(input.className.includes("border-red-600"), true);
  35. cleanup();
  36. },
  37. sanitizeResources: false,
  38. sanitizeOps: false,
  39. });
  40. Deno.test({
  41. name: "Input - applies normal border when error=false",
  42. fn() {
  43. render(<Input error={false} placeholder="ok" />);
  44. const input = screen.getByPlaceholderText("ok");
  45. assertEquals(input.className.includes("border-gray-300"), true);
  46. assertEquals(input.className.includes("border-red-600"), false);
  47. cleanup();
  48. },
  49. sanitizeResources: false,
  50. sanitizeOps: false,
  51. });
  52. Deno.test({
  53. name: "Input - passes through type attribute",
  54. fn() {
  55. render(<Input type="password" placeholder="pw" />);
  56. const input = screen.getByPlaceholderText("pw") as HTMLInputElement;
  57. assertEquals(input.type, "password");
  58. cleanup();
  59. },
  60. sanitizeResources: false,
  61. sanitizeOps: false,
  62. });
  63. Deno.test({
  64. name: "Input - passes through disabled attribute",
  65. fn() {
  66. render(<Input disabled placeholder="disabled" />);
  67. const input = screen.getByPlaceholderText("disabled") as HTMLInputElement;
  68. assertEquals(input.disabled, true);
  69. cleanup();
  70. },
  71. sanitizeResources: false,
  72. sanitizeOps: false,
  73. });
  74. Deno.test({
  75. name: "Input - appends custom className",
  76. fn() {
  77. render(<Input className="my-input" placeholder="custom" />);
  78. const input = screen.getByPlaceholderText("custom");
  79. assertEquals(input.className.includes("my-input"), true);
  80. cleanup();
  81. },
  82. sanitizeResources: false,
  83. sanitizeOps: false,
  84. });