import { render, screen, waitFor } from "@solidjs/testing-library";
import { afterEach, describe, expect, it } from "vitest";
import { Toast, toast } from "../../../src/components/toast/index";

afterEach(() => {
	toast.clear();
});

describe("Toast", () => {
	it("region has role=region", () => {
		render(() => <Toast.Region />);
		expect(screen.getByRole("region")).toBeTruthy();
	});

	it("region has aria-label", () => {
		render(() => <Toast.Region />);
		expect(screen.getByRole("region").getAttribute("aria-label")).toBeTruthy();
	});

	it("toast() adds a toast to the region", () => {
		render(() => <Toast.Region />);
		toast("Hello world");
		expect(screen.getByText("Hello world")).toBeTruthy();
	});

	it("toast.success() creates a success toast", () => {
		render(() => <Toast.Region />);
		toast.success("Saved!");
		expect(screen.getByText("Saved!")).toBeTruthy();
	});

	it("toast.error() creates an error toast", () => {
		render(() => <Toast.Region />);
		toast.error("Failed");
		expect(screen.getByText("Failed")).toBeTruthy();
	});

	it("toast.dismiss() removes a toast", async () => {
		render(() => <Toast.Region />);
		const id = toast("Dismissable");
		expect(screen.getByText("Dismissable")).toBeTruthy();
		toast.dismiss(id);
		await waitFor(() => expect(screen.queryByText("Dismissable")).toBeNull());
	});

	it("toast.clear() removes all toasts", async () => {
		render(() => <Toast.Region />);
		toast("One");
		toast("Two");
		expect(screen.getByText("One")).toBeTruthy();
		expect(screen.getByText("Two")).toBeTruthy();
		toast.clear();
		await waitFor(() => {
			expect(screen.queryByText("One")).toBeNull();
			expect(screen.queryByText("Two")).toBeNull();
		});
	});

	it("toast returns an ID", () => {
		render(() => <Toast.Region />);
		const id = toast("Test");
		expect(typeof id).toBe("string");
		expect(id.length).toBeGreaterThan(0);
	});
});
