import {BaseListResult} from '@models';
import {useQueryDataInfo} from '.';
import {render, waitFor} from '@testing-library/react';
import {FC} from 'react';
import {QueryClient, QueryClientProvider} from '@tanstack/react-query';

function mockRequest(num: number): BaseListResult<number> {
  return Promise.resolve({
    msg: '200',
    data: {
      total: 1,
      list: Array.from({length: num}).map((_, idx) => idx),
      pageNum: 1,
      pageSize: 1,
      size: 1,
      startRow: 1,
      endRow: 1,
      pages: 1,
      prePage: 1,
      nextPage: 1,
      isFirstPage: true,
      isLastPage: true,
      hasPreviousPage: true,
      hasNextPage: true,
      navigatePages: 1,
      navigatepageNums: [],
      navigateFirstPage: 1,
      navigateLastPage: 1,
    },
  });
}

describe('useQueryDataInfo', function () {
  it('声明正确', function () {
    expect(useQueryDataInfo).toBeDefined();
    expect(useQueryDataInfo).toBeInstanceOf(Function);
  });

  it('返回值正确', async function () {
    const App: FC = function () {
      const data = useQueryDataInfo({
        queryFn: mockRequest,
        params: [5],
        enabled: true,
      });

      return <p data-testid='data'>{data}</p>;
    };

    const client = new QueryClient();

    const {getByTestId} = render(
      <QueryClientProvider client={client}>
        <App />
      </QueryClientProvider>,
    );

    await waitFor(function () {
      expect(getByTestId('data').innerHTML).toBe('0');
    });
  });
});
