import {act, render, renderHook} from '@testing-library/react';
import {createSearchContext, useTableSearchState} from '.';
import {FC} from 'react';
import {SearchProvider} from '@components';

describe('useSearchContext', function () {
  it('basic', function () {
    expect(useTableSearchState).toBeDefined();
    expect(useTableSearchState).toBeInstanceOf(Function);
  });

  it('返回值类型', function () {
    const {result} = renderHook(function () {
      const context = createSearchContext();

      return useTableSearchState(context);
    });

    expect(result.current[0]).toBe(false);
    expect(result.current[1]).toBeInstanceOf(Function);
  });

  it('修改值', function () {
    const context = createSearchContext();

    const App: FC = function () {
      const [isSearching, onSearchChange] = useTableSearchState(context);

      return (
        <>
          <p data-testid='state'>{isSearching.toString()}</p>
          <button data-testid='btn' onClick={() => onSearchChange(true)} />
        </>
      );
    };

    const {getByTestId} = render(
      <SearchProvider context={context}>
        <App />
      </SearchProvider>,
    );

    expect(getByTestId('state').innerHTML).toBe('false');

    act(() => getByTestId('btn').click());

    expect(getByTestId('state').innerHTML).toBe('true');
  });
});
