import React from 'react';
import 'jest-styled-components';
import { FlatList, Modal } from 'react-native';
import { render, fireEvent, act, waitFor } from '@testing-library/react-native';
import { Pressable } from '../../Pressable';
import {
   PageListingRealWordMock,
   PageListingRealWordMockError,
   PageListingRealWordMockMoreError,
   PageListingRealWordMockNotFound,
} from './__mocks__/PageListingRealWordMock';

// Hack para NavigationContainer funcionar
async function wait(ms = 0) {
   return await act(async () => {
      return new Promise((resolve) => {
         setTimeout(resolve, ms);
      });
   });
}

it('snapshot - PageListing com card skeleton', async () => {
   const { toJSON, getAllByTestId } = render(<PageListingRealWordMock />);

   expect(getAllByTestId('skeleton-card').length).toBe(10);
   expect(toJSON()).toMatchSnapshot();
   await wait();
});

it('snapshot - PageListing com cards', async () => {
   const { toJSON, getAllByTestId } = render(<PageListingRealWordMock />);

   await wait();

   expect(getAllByTestId('card-1').length).toBe(1);
   expect(getAllByTestId('card-5').length).toBe(1);

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

it('snapshot - PageListing com nav actions', async () => {
   const { toJSON, getByTestId, getAllByTestId } = render(<PageListingRealWordMock />);

   await wait();

   fireEvent.press(getByTestId('card-1').findByType(Pressable));

   await wait();
   expect(getAllByTestId('nav-actions-container').length).toBe(1);

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

it('snapshot - PageListing com not found', async () => {
   const { toJSON, getByTestId, getAllByTestId } = render(<PageListingRealWordMockNotFound />);

   await wait();
   expect(getAllByTestId('not-found-page').length).toBe(1);

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

it('snapshot - PageListing com error', async () => {
   const { toJSON, getByTestId, getAllByTestId } = render(<PageListingRealWordMockError />);

   await wait();
   expect(getAllByTestId('error-page-listing').length).toBe(1);

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

it('snapshot - PageListing com filtros aberto', async () => {
   const { toJSON, getByTestId, getAllByTestId, UNSAFE_getByType } = render(<PageListingRealWordMockError />);

   fireEvent.press(getByTestId('open-filter'));

   await wait();

   expect(UNSAFE_getByType(Modal).props.visible).toBe(true);

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