feedback-form.test.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { render, screen } from "@testing-library/react";
  2. import userEvent from "@testing-library/user-event";
  3. import { afterEach, describe, expect, it, vi } from "vitest";
  4. import { FeedbackForm } from "#/components/feedback-form";
  5. describe("FeedbackForm", () => {
  6. const user = userEvent.setup();
  7. const onSubmitMock = vi.fn();
  8. const onCloseMock = vi.fn();
  9. afterEach(() => {
  10. vi.clearAllMocks();
  11. });
  12. it("should render correctly", () => {
  13. render(<FeedbackForm onSubmit={onSubmitMock} onClose={onCloseMock} />);
  14. screen.getByLabelText("Email");
  15. screen.getByLabelText("Private");
  16. screen.getByLabelText("Public");
  17. screen.getByRole("button", { name: "Submit" });
  18. screen.getByRole("button", { name: "Cancel" });
  19. });
  20. it("should switch between private and public permissions", async () => {
  21. render(<FeedbackForm onSubmit={onSubmitMock} onClose={onCloseMock} />);
  22. const privateRadio = screen.getByLabelText("Private");
  23. const publicRadio = screen.getByLabelText("Public");
  24. expect(privateRadio).toBeChecked(); // private is the default value
  25. expect(publicRadio).not.toBeChecked();
  26. await user.click(publicRadio);
  27. expect(publicRadio).toBeChecked();
  28. expect(privateRadio).not.toBeChecked();
  29. await user.click(privateRadio);
  30. expect(privateRadio).toBeChecked();
  31. expect(publicRadio).not.toBeChecked();
  32. });
  33. it("should call onSubmit when the form is submitted", async () => {
  34. render(<FeedbackForm onSubmit={onSubmitMock} onClose={onCloseMock} />);
  35. const email = screen.getByLabelText("Email");
  36. await user.type(email, "test@test.test");
  37. await user.click(screen.getByRole("button", { name: "Submit" }));
  38. expect(onSubmitMock).toHaveBeenCalledWith("private", "test@test.test"); // private is the default value
  39. });
  40. it("should not call onSubmit when the email is invalid", async () => {
  41. render(<FeedbackForm onSubmit={onSubmitMock} onClose={onCloseMock} />);
  42. const email = screen.getByLabelText("Email");
  43. const submitButton = screen.getByRole("button", { name: "Submit" });
  44. await user.click(submitButton);
  45. expect(onSubmitMock).not.toHaveBeenCalled();
  46. await user.type(email, "test");
  47. await user.click(submitButton);
  48. expect(onSubmitMock).not.toHaveBeenCalled();
  49. });
  50. it("should submit public permissions when the public radio is checked", async () => {
  51. render(<FeedbackForm onSubmit={onSubmitMock} onClose={onCloseMock} />);
  52. const email = screen.getByLabelText("Email");
  53. const publicRadio = screen.getByLabelText("Public");
  54. await user.type(email, "test@test.test");
  55. await user.click(publicRadio);
  56. await user.click(screen.getByRole("button", { name: "Submit" }));
  57. expect(onSubmitMock).toHaveBeenCalledWith("public", "test@test.test");
  58. });
  59. it("should call onClose when the close button is clicked", async () => {
  60. render(<FeedbackForm onSubmit={onSubmitMock} onClose={onCloseMock} />);
  61. await user.click(screen.getByRole("button", { name: "Cancel" }));
  62. expect(onSubmitMock).not.toHaveBeenCalled();
  63. expect(onCloseMock).toHaveBeenCalled();
  64. });
  65. it("should disable the buttons if isSubmitting is true", () => {
  66. const { rerender } = render(
  67. <FeedbackForm onSubmit={onSubmitMock} onClose={onCloseMock} />,
  68. );
  69. const submitButton = screen.getByRole("button", { name: "Submit" });
  70. const cancelButton = screen.getByRole("button", { name: "Cancel" });
  71. expect(submitButton).not.toBeDisabled();
  72. expect(cancelButton).not.toBeDisabled();
  73. rerender(
  74. <FeedbackForm
  75. onSubmit={onSubmitMock}
  76. onClose={onCloseMock}
  77. isSubmitting
  78. />,
  79. );
  80. expect(submitButton).toBeDisabled();
  81. expect(cancelButton).toBeDisabled();
  82. });
  83. });