AutocompleteCombobox.test.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { act, render, screen } from "@testing-library/react";
  2. import userEvent from "@testing-library/user-event";
  3. import { describe, expect, it, vi } from "vitest";
  4. import { AutocompleteCombobox } from "#/components/modals/settings/AutocompleteCombobox";
  5. const onChangeMock = vi.fn();
  6. const renderComponent = () =>
  7. render(
  8. <AutocompleteCombobox
  9. ariaLabel="model"
  10. items={[
  11. { value: "m1", label: "model1" },
  12. { value: "m2", label: "model2" },
  13. { value: "m3", label: "model3" },
  14. ]}
  15. defaultKey="m1"
  16. tooltip="tooltip"
  17. onChange={onChangeMock}
  18. />,
  19. );
  20. describe("AutocompleteCombobox", () => {
  21. it("should render a combobox with the default value", () => {
  22. renderComponent();
  23. const modelInput = screen.getByRole("combobox", { name: "model" });
  24. expect(modelInput).toHaveValue("model1");
  25. });
  26. it("should open a dropdown with the available values", async () => {
  27. renderComponent();
  28. const modelInput = screen.getByRole("combobox", { name: "model" });
  29. expect(screen.queryByText("model2")).not.toBeInTheDocument();
  30. expect(screen.queryByText("model3")).not.toBeInTheDocument();
  31. await act(async () => {
  32. await userEvent.click(modelInput);
  33. });
  34. expect(screen.getByText("model2")).toBeInTheDocument();
  35. expect(screen.getByText("model3")).toBeInTheDocument();
  36. });
  37. it("should call the onChange handler when a new value is selected", async () => {
  38. renderComponent();
  39. const modelInput = screen.getByRole("combobox", { name: "model" });
  40. expect(modelInput).toHaveValue("model1");
  41. await act(async () => {
  42. await userEvent.click(modelInput);
  43. });
  44. const model2 = screen.getByText("model2");
  45. await act(async () => {
  46. await userEvent.click(model2);
  47. });
  48. expect(onChangeMock).toHaveBeenCalledWith("model2");
  49. });
  50. it("should set the input value to the default key if the default key is not in the list", () => {
  51. render(
  52. <AutocompleteCombobox
  53. ariaLabel="model"
  54. items={[{ value: "m1", label: "model1" }]}
  55. defaultKey="m2"
  56. tooltip="tooltip"
  57. onChange={onChangeMock}
  58. />,
  59. );
  60. const modelInput = screen.getByRole("combobox", { name: "model" });
  61. expect(modelInput).toHaveValue("m2");
  62. });
  63. it.todo("should show a tooltip after 0.5 seconds of focus");
  64. });