components.test.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { describe, expect, it, vi } from "vitest";
  2. import { fireEvent, render, screen } from "@testing-library/svelte";
  3. import ButtonHarness from "./test-fixtures/ButtonHarness.test.svelte";
  4. import InputStateHarness from "./test-fixtures/InputStateHarness.test.svelte";
  5. import InputValidationHarness from "./test-fixtures/InputValidationHarness.test.svelte";
  6. import DialogHarness from "./test-fixtures/DialogHarness.test.svelte";
  7. describe("shadcn-svelte component behavior", () => {
  8. it("emits press for Button", async () => {
  9. const onPress = vi.fn();
  10. render(ButtonHarness, { onPress });
  11. await fireEvent.click(screen.getByRole("button", { name: "Submit" }));
  12. expect(onPress).toHaveBeenCalledTimes(1);
  13. });
  14. it("writes bound Input value to state path", async () => {
  15. const onStateChange = vi.fn();
  16. render(InputStateHarness, { onStateChange });
  17. const input = screen.getByLabelText("Name");
  18. await fireEvent.input(input, { target: { value: "Alice" } });
  19. expect(onStateChange).toHaveBeenCalledWith([
  20. { path: "/form/name", value: "Alice" },
  21. ]);
  22. });
  23. it("validates Input on blur when validateOn is blur", async () => {
  24. render(InputValidationHarness);
  25. const input = screen.getByLabelText("Email");
  26. await fireEvent.blur(input);
  27. expect(screen.getByText("Required")).toBeTruthy();
  28. });
  29. it("reads and updates openPath for Dialog", async () => {
  30. const onStateChange = vi.fn();
  31. render(DialogHarness, { onStateChange });
  32. expect(screen.getByText("Dialog content")).toBeTruthy();
  33. const closeButton = screen.getByRole("button", { name: "Close" });
  34. await fireEvent.click(closeButton);
  35. expect(onStateChange).toHaveBeenCalledWith([
  36. { path: "/dialog/open", value: false },
  37. ]);
  38. });
  39. });