import React, { useEffect, useRef, useState } from 'react';
import { render, fireEvent, waitFor, screen } from '@testing-library/react';
import { FormHandles } from '@unform/core';
import { Form } from '@unform/web';
import { Checkbox } from './Checkbox';
import 'jest-styled-components';

it('renderizar default', () => {
   function Component(): any {
      return (
         <Form onSubmit={() => {}}>
            <Checkbox name="default" title="Checkbox" />
         </Form>
      );
   }

   const { container } = render(<Component />);

   expect(container.firstChild).toMatchSnapshot();
});

it('renderizar with color', () => {
   function Component(): any {
      return (
         <Form onSubmit={() => {}} initialData={{ checkbox: true }}>
            <Checkbox name="checkbox" title="Checkbox" color="orange" />
         </Form>
      );
   }

   const { container } = render(<Component />);

   expect(container.firstChild).toMatchSnapshot();
});

it('renderizar with object value', () => {
   function Component(): any {
      return (
         <>
            <Form onSubmit={() => {}} initialData={{ checkbox: { id: 1 } }}>
               <Checkbox name="checkbox" title="Checkbox" color="red" value={{ id: 1 }} />
            </Form>
            <Form onSubmit={() => {}} initialData={{ checkbox: { id: 2 } }}>
               <Checkbox name="checkbox" title="Checkbox" color="red" value={{ id: 1 }} />
            </Form>
         </>
      );
   }

   const { container } = render(<Component />);

   expect(container.firstChild).toMatchSnapshot();
});

it('renderizar with object value async', () => {
   function Component(): any {
      const formRef = useRef<FormHandles>();

      useEffect(() => {
         formRef.current.setFieldValue('checkbox', { id: 1 });
      }, []);

      return (
         <Form onSubmit={() => {}} initialData={{ checkbox: { id: 2 } }} ref={formRef}>
            <Checkbox name="checkbox" title="Checkbox" color="red" value={{ id: 1 }} />
         </Form>
      );
   }

   const { container } = render(<Component />);

   expect(container.firstChild).toMatchSnapshot();
});

it('renderizar with error', () => {
   function Component(): any {
      const formRef = useRef<FormHandles>();

      useEffect(() => {
         formRef.current.setFieldError('checkbox', 'Field is required');
      }, []);

      return (
         <Form onSubmit={() => {}} initialData={{ checkbox: { id: 2 } }} ref={formRef}>
            <Checkbox name="checkbox" title="Checkbox" color="red" value={{ id: 1 }} />
         </Form>
      );
   }

   const { container } = render(<Component />);

   expect(screen.getByTestId('input-error')).not.toBeUndefined();

   expect(container.firstChild).toMatchSnapshot();
});

it('renderizar with disabled', () => {
   function Component(): any {
      const formRef = useRef<FormHandles>();

      useEffect(() => {
         formRef.current.setFieldError('checkbox', 'Field is required');
      }, []);

      return (
         <Form onSubmit={() => {}} initialData={{ checkbox: { id: 2 } }} ref={formRef}>
            <Checkbox name="checkbox" title="Checkbox" disabled value={{ id: 1 }} />
         </Form>
      );
   }

   const { container } = render(<Component />);

   expect(screen.getByTestId('input-checkbox-error').className).toContain('disabled');

   expect(container.firstChild).toMatchSnapshot();
});
