import React from "react";
import HiddenField, { IHiddenFieldProps } from "@ax/components/Fields/HiddenField";
import { ThemeProvider } from "styled-components";
import { parseTheme } from "@ax/helpers";
import globalTheme from "@ax/themes/theme.json";
import { mock } from "jest-mock-extended";
import { render, screen, cleanup, fireEvent } from "@testing-library/react";

afterEach(cleanup);

const defaultProps = mock<IHiddenFieldProps>();

describe("Component rendering", () => {
	it("should render the component", () => {
		defaultProps.hasMultipleOptions = true;

		render(
			<ThemeProvider theme={parseTheme(globalTheme)}>
				<HiddenField {...defaultProps} />
			</ThemeProvider>,
		);

		expect(screen.getByTestId("field-multiple-options")).toBeTruthy();
	});

	it("should render the component when hasMultipleOptions is true", () => {
		defaultProps.hasMultipleOptions = false;

		render(
			<ThemeProvider theme={parseTheme(globalTheme)}>
				<HiddenField {...defaultProps} />
			</ThemeProvider>,
		);

		expect(screen.getByTestId("field-single-option")).toBeTruthy();
		expect(screen.getByText("Show")).toBeTruthy();
	});
});

describe("onClick events", () => {
	it("should call showField with multiple options when clicking the component", () => {
		defaultProps.hasMultipleOptions = true;
		const showField = jest.fn();

		render(
			<ThemeProvider theme={parseTheme(globalTheme)}>
				<HiddenField {...defaultProps} showField={showField} />
			</ThemeProvider>,
		);

		const wrapperMultipleOptions = screen.getByTestId("field-multiple-options");
		fireEvent.click(wrapperMultipleOptions);
		expect(showField).toHaveBeenCalledTimes(1);
	});

	it("should call showField with single option when clicking the component", () => {
		defaultProps.hasMultipleOptions = false;
		const showField = jest.fn();

		render(
			<ThemeProvider theme={parseTheme(globalTheme)}>
				<HiddenField {...defaultProps} showField={showField} />
			</ThemeProvider>,
		);

		const wrapperSingleOption = screen.getByTestId("field-single-option");
		fireEvent.click(wrapperSingleOption);
		expect(showField).toHaveBeenCalledTimes(1);
	});
});
