import { jsx, jsxs } from "react/jsx-runtime";
import React from "react";
import { render, fireEvent } from "@testing-library/react";
import RadioGroup from "../..";
import ThemeProvider from "../../../Theme";
import RadioGroupContext from "../../RadioGroupContext";
const originalError = console.error;
beforeEach(() => {
  console.error = jest.fn();
});
afterEach(() => {
  console.error = originalError;
});
describe("<RadioGroup />", () => {
  describe("<RadioGroup.Button />", () => {
    describe("Snapshots", () => {
      it("should match snapshot with default radio button", () => {
        const { container } = render(
          /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(RadioGroup.Button, { children: "Radio 1" }) })
        );
        expect(container).toMatchSnapshot();
      });
      it("should match snapshot with small radio button", () => {
        const { container } = render(
          /* @__PURE__ */ jsx(RadioGroupContext.Provider, { value: { small: true }, children: /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(RadioGroup.Button, { children: "Radio 1" }) }) })
        );
        expect(container).toMatchSnapshot();
      });
    });
    describe("Props", () => {
      it("should contains input type radio", () => {
        const { getByTestId } = render(
          /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(RadioGroup.Button, { "data-testid": "radio", children: "Radio 1" }) })
        );
        expect(getByTestId("radio").type).toBe("radio");
      });
      it("value should be as prop value", () => {
        const { getByTestId } = render(
          /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(RadioGroup.Button, { "data-testid": "radio", value: "radio1", children: "Radio 1" }) })
        );
        expect(getByTestId("radio").value).toBe("radio1");
      });
      it("value should be children if prop value is ommited", () => {
        const { getByTestId, getByText } = render(
          /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(RadioGroup.Button, { "data-testid": "radio", children: "Radio 1" }) })
        );
        const label = getByText("Radio 1");
        const radio = getByTestId("radio");
        expect(radio.value).toBe(label.textContent);
      });
      it("checked should be the selectedValue provided from Context", () => {
        const { getAllByTestId } = render(
          /* @__PURE__ */ jsx(RadioGroupContext.Provider, { value: { selectedValue: "Radio 2" }, children: /* @__PURE__ */ jsxs(ThemeProvider, { children: [
            /* @__PURE__ */ jsx(RadioGroup.Button, { "data-testid": "radio", children: "Radio 1" }),
            /* @__PURE__ */ jsx(RadioGroup.Button, { "data-testid": "radio", children: "Radio 2" })
          ] }) })
        );
        const [radio, radio2] = getAllByTestId("radio");
        expect(radio.checked).toBe(false);
        expect(radio2.checked).toBe(true);
      });
      it("name should the one provided by context", () => {
        const name = "test";
        const { getAllByTestId } = render(
          /* @__PURE__ */ jsx(RadioGroupContext.Provider, { value: { name }, children: /* @__PURE__ */ jsxs(ThemeProvider, { children: [
            /* @__PURE__ */ jsx(RadioGroup.Button, { "data-testid": "radio", children: "Radio 1" }),
            /* @__PURE__ */ jsx(RadioGroup.Button, { "data-testid": "radio", children: "Radio 2" }),
            /* @__PURE__ */ jsx(RadioGroup.Button, { "data-testid": "radio", children: "Radio 3" })
          ] }) })
        );
        const radios = getAllByTestId("radio");
        radios.map((radio) => expect(radio.name).toBe(name));
      });
    });
    describe("Events", () => {
      it("should call onchange from Context", () => {
        const onChangeMock = jest.fn();
        const { getByText } = render(
          /* @__PURE__ */ jsx(RadioGroupContext.Provider, { value: { onChange: onChangeMock }, children: /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(RadioGroup.Button, { children: "Radio 1" }) }) })
        );
        const radio = getByText("Radio 1");
        fireEvent.click(radio);
        expect(onChangeMock).toHaveBeenCalled();
      });
    });
  });
});
