import { describe, it, expect, vi } from "vitest";
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { ConfirmDialog } from "../ConfirmDialog";

describe("ConfirmDialog", () => {
	it("renders the title and message and wires confirm/cancel", async () => {
		const onCancel = vi.fn();
		const onConfirm = vi.fn();
		render(
			<ConfirmDialog
				open
				title="Delete machine?"
				message="This cannot be undone."
				confirmLabel="Delete"
				onCancel={onCancel}
				onConfirm={onConfirm}
			/>,
		);
		expect(screen.getByText("Delete machine?")).toBeInTheDocument();
		expect(screen.getByText("This cannot be undone.")).toBeInTheDocument();
		await userEvent.click(screen.getByRole("button", { name: "Delete" }));
		expect(onConfirm).toHaveBeenCalledTimes(1);
		await userEvent.click(screen.getByRole("button", { name: "Cancel" }));
		expect(onCancel).toHaveBeenCalledTimes(1);
	});

	it("renders nothing when closed", () => {
		render(
			<ConfirmDialog
				open={false}
				title="Hidden"
				message="nope"
				onCancel={() => {}}
				onConfirm={() => {}}
			/>,
		);
		expect(screen.queryByText("Hidden")).not.toBeInTheDocument();
	});
});
