import { fireEvent, render, screen } from "@testing-library/react";
import { expect, test } from "vitest";
import { ToastProvider, useToast } from "./ToastProvider.jsx";

function ToastTrigger({ payload, severity }) {
  const { showToast } = useToast();
  return (
    <button type="button" onClick={() => showToast(payload, severity)}>
      show
    </button>
  );
}

test("renders object-style toast payloads as text", () => {
  render(
    <ToastProvider>
      <ToastTrigger payload={{ message: "工资结算失败", severity: "error" }} />
    </ToastProvider>,
  );

  fireEvent.click(screen.getByRole("button", { name: "show" }));

  expect(screen.getByText("工资结算失败")).toBeInTheDocument();
});

test("keeps tuple-style toast payloads working", () => {
  render(
    <ToastProvider>
      <ToastTrigger payload="区域已更新" severity="success" />
    </ToastProvider>,
  );

  fireEvent.click(screen.getByRole("button", { name: "show" }));

  expect(screen.getByText("区域已更新")).toBeInTheDocument();
});
