32 lines
1.6 KiB
TypeScript
32 lines
1.6 KiB
TypeScript
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
|
|
import { ApiError } from "@/lib/api";
|
|
import BlocklistForm, { swallowMutationError } from "./BlocklistForm";
|
|
|
|
test("swallowMutationError drops an ApiError and rethrows anything else", () => {
|
|
expect(() => swallowMutationError(new ApiError(400, "bad url"))).not.toThrow();
|
|
expect(() => swallowMutationError(new TypeError("cannot read x of undefined"))).toThrow(TypeError);
|
|
expect(() => swallowMutationError("not an error at all")).toThrow();
|
|
});
|
|
|
|
test("a rejected submit leaves the typed values in place; a resolved one clears them", async () => {
|
|
const rejecting = vi.fn(() => Promise.reject(new ApiError(400, "bad url")));
|
|
const { rerender } = render(
|
|
<BlocklistForm busy={false} readOnly={false} error={null} onSubmit={rejecting} onCancel={undefined} />,
|
|
);
|
|
const url = screen.getByLabelText("URL") as HTMLInputElement;
|
|
const name = screen.getByLabelText("Name") as HTMLInputElement;
|
|
fireEvent.change(url, { target: { value: "https://example.com/list.txt" } });
|
|
fireEvent.change(name, { target: { value: "Example" } });
|
|
fireEvent.click(screen.getByRole("button", { name: "Add source" }));
|
|
|
|
await waitFor(() => expect(rejecting).toHaveBeenCalledTimes(1));
|
|
expect(url.value).toBe("https://example.com/list.txt");
|
|
expect(name.value).toBe("Example");
|
|
|
|
const resolving = vi.fn(() => Promise.resolve());
|
|
rerender(<BlocklistForm busy={false} readOnly={false} error={null} onSubmit={resolving} onCancel={undefined} />);
|
|
fireEvent.click(screen.getByRole("button", { name: "Add source" }));
|
|
await waitFor(() => expect(url.value).toBe(""));
|
|
expect(name.value).toBe("");
|
|
});
|