suggestion-item.test.tsx 1.0 KB

123456789101112131415161718192021222324252627282930
  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 { SuggestionItem } from "#/components/suggestion-item";
  5. describe("SuggestionItem", () => {
  6. const suggestionItem = { label: "suggestion1", value: "a long text value" };
  7. const onClick = vi.fn();
  8. afterEach(() => {
  9. vi.clearAllMocks();
  10. });
  11. it("should render a suggestion", () => {
  12. render(<SuggestionItem suggestion={suggestionItem} onClick={onClick} />);
  13. expect(screen.getByTestId("suggestion")).toBeInTheDocument();
  14. expect(screen.getByText(/suggestion1/i)).toBeInTheDocument();
  15. });
  16. it("should call onClick when clicking a suggestion", async () => {
  17. const user = userEvent.setup();
  18. render(<SuggestionItem suggestion={suggestionItem} onClick={onClick} />);
  19. const suggestion = screen.getByTestId("suggestion");
  20. await user.click(suggestion);
  21. expect(onClick).toHaveBeenCalledWith("a long text value");
  22. });
  23. });