import * as React from "react";

import { ThemeProvider } from "styled-components";
import { render, cleanup, screen, fireEvent } from "@testing-library/react";
import { parseTheme } from "@ax/helpers";
import "@testing-library/jest-dom";

import IconAction, { IIconActionProps } from "@ax/components/IconAction";
import globalTheme from "@ax/themes/theme.json";

afterEach(cleanup);

describe("IconAction component rendering", () => {
	it("should render the component", () => {
		const defaultProps: IIconActionProps = {
			icon: "delete",
			size: "m",
		};

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

		const button = screen.getByTestId("icon-action-component");
		expect(button).toBeTruthy();
	});

	it("should render the component inversed", () => {
		const defaultProps: IIconActionProps = {
			icon: "delete",
			size: "m",
			inversed: true,
		};

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

		const button = screen.getByTestId("icon-action-component");
		expect(button).toBeTruthy();
	});
});

describe("onClick events", () => {
	test("onClick been called if enabled", () => {
		const onClickMock = jest.fn();
		const defaultProps: IIconActionProps = {
			icon: "delete",
			size: "m",
			onClick: onClickMock,
		};

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

		const button = screen.getByTestId("icon-action-component");
		expect(button).toBeTruthy();

		fireEvent.click(button);
		expect(onClickMock).toBeCalledTimes(1);
	});

	test("onClick not to be called if disabled", () => {
		const onClickMock = jest.fn();
		const defaultProps: IIconActionProps = {
			icon: "delete",
			size: "m",
			onClick: onClickMock,
			disabled: true,
		};

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

		const button = screen.getByTestId("icon-action-component");
		expect(button).toBeTruthy();

		fireEvent.click(button);
		expect(onClickMock).toBeCalledTimes(0);
	});
});
