import React, { useEffect, useRef, useState } from 'react';
import { Button } from 'react-native';
import { render, fireEvent, act } from '@testing-library/react-native';
import { ITab, ITabsRef, Tabs } from '.';

const optionsTabs = [
   {
      id: 1,
      name: 'AAA',
   },
   {
      id: 2,
      name: 'BBB',
   },
   {
      id: 3,
      name: 'CCC',
   },
   {
      id: 4,
      name: 'DDD',
   },
];

it('renderizar padrão', () => {
   const { toJSON, getByTestId } = render(<Tabs options={optionsTabs} onSelect={() => {}} />);
   expect(toJSON()).toMatchSnapshot();
});

it('renderizar com skeleton', () => {
   const { toJSON, getByTestId } = render(<Tabs options={optionsTabs} onSelect={() => {}} skeleton />);
   expect(toJSON()).toMatchSnapshot();
});

it('renderizar com valor defaultTab', () => {
   const { toJSON, getByTestId } = render(<Tabs options={optionsTabs} onSelect={() => {}} defaultTab={3} />);
   expect(toJSON()).toMatchSnapshot();
});

it('renderizar com valor click', () => {
   const { toJSON, getByTestId } = render(<Tabs options={optionsTabs} onSelect={() => {}} />);

   fireEvent.press(getByTestId('tab-2'));

   expect(toJSON()).toMatchSnapshot();
});

it('testar onClick', (done) => {
   function handleClick(tab: ITab): void {
      expect(tab).toEqual({ id: 2, name: 'BBB' });
      done();
   }

   const { getByTestId } = render(<Tabs options={optionsTabs} onSelect={handleClick} />);

   fireEvent.press(getByTestId('tab-1'));
});

it('testar ref setTab', () => {
   const ref = React.createRef<ITabsRef>();

   const { getByTestId } = render(<Tabs options={optionsTabs} onSelect={() => {}} ref={ref} />);

   expect(getByTestId('tab-0').props.selected).toBeTruthy();
   expect(getByTestId('tab-1').props.selected).toBeFalsy();
   expect(getByTestId('tab-2').props.selected).toBeFalsy();
   expect(getByTestId('tab-3').props.selected).toBeFalsy();

   act(() => {
      // id = 4
      ref.current.setTab(4);
   });

   expect(getByTestId('tab-0').props.selected).toBeFalsy();
   expect(getByTestId('tab-1').props.selected).toBeFalsy();
   expect(getByTestId('tab-2').props.selected).toBeFalsy();
   expect(getByTestId('tab-3').props.selected).toBeTruthy();
});
