import React from 'react';
import '@testing-library/jest-dom/extend-expect';
import { fireEvent, render } from '@testing-library/react';
import { SearchBar } from '.';
describe('Searchbar', function () {
  var onSearchSpy;
  var wrapper;
  var setShowSearchSpy;
  describe('minimal props', function () {
    beforeEach(function () {
      onSearchSpy = jest.fn();
      setShowSearchSpy = jest.fn();
      var searchButton = {
        current: {
          contains: jest.fn().mockReturnValue(false)
        }
      };
      wrapper = render( /*#__PURE__*/React.createElement(SearchBar, {
        containerWidth: 100,
        onSearch: onSearchSpy,
        searchButtonRef: searchButton,
        setShowSearch: setShowSearchSpy
      }));
    });
    it('should render the search bar', function () {
      expect(wrapper.getByTestId('searchbar')).toBeInTheDocument();
    });
    it('should auto focus on the search field', function () {
      expect(wrapper.getByTestId('text-input-input')).toHaveFocus();
    });
    it('should set the `aria-label` attribute to `Search` on the search button', function () {
      expect(wrapper.queryByTestId('searchbar-submit-button')).toHaveAttribute('aria-label', 'Search');
    });
    describe('and the user clicks inside the search field', function () {
      beforeEach(function () {
        fireEvent(wrapper.getByTestId('searchbar'), new MouseEvent('mousedown', {
          bubbles: true,
          cancelable: true
        }));
      });
      it('should not hide the searchbar', function () {
        expect(setShowSearchSpy).not.toHaveBeenCalled();
      });
    });
    describe('and the user clicks outside the search field', function () {
      beforeEach(function () {
        fireEvent(document, new MouseEvent('mousedown', {
          bubbles: true,
          cancelable: true
        }));
      });
      it('should hide the searchbar', function () {
        expect(setShowSearchSpy).toHaveBeenCalledTimes(1);
        expect(setShowSearchSpy).toHaveBeenCalledWith(false);
      });
    });
    describe('and the user enters a search term', function () {
      beforeEach(function () {
        fireEvent.change(wrapper.getByTestId('text-input-input'), {
          target: {
            value: 'A'
          }
        });
      });
      describe('and the form is submitted', function () {
        beforeEach(function () {
          wrapper.getByTestId('searchbar-submit-button').click();
        });
        it('should call the `onSearch` callback with the search term', function () {
          expect(onSearchSpy).toHaveBeenCalledTimes(1);
          expect(onSearchSpy).toHaveBeenCalledWith(expect.objectContaining({
            _reactName: 'onSubmit'
          }), 'A');
        });
      });
    });
  });
  describe('when the user clicks the search button', function () {
    beforeEach(function () {
      var searchButton = {
        current: {
          contains: jest.fn().mockReturnValue(true)
        }
      };
      wrapper = render( /*#__PURE__*/React.createElement(SearchBar, {
        containerWidth: 100,
        onSearch: onSearchSpy,
        searchButtonRef: searchButton,
        setShowSearch: setShowSearchSpy
      }));
    });
    describe('and the user clicks the search open/close button', function () {
      beforeEach(function () {
        onSearchSpy = jest.fn();
        setShowSearchSpy = jest.fn();
        fireEvent(document, new MouseEvent('mousedown', {
          bubbles: true,
          cancelable: true
        }));
      });
      it('should not hide the searchbar', function () {
        expect(setShowSearchSpy).not.toHaveBeenCalled();
      });
    });
  });
  describe('when arbitrary props are specified', function () {
    beforeEach(function () {
      var searchButton = {
        current: {
          contains: jest.fn().mockReturnValue(false)
        }
      };
      wrapper = render( /*#__PURE__*/React.createElement(SearchBar, {
        "data-arbitrary": "arbitrary",
        containerWidth: 100,
        onSearch: onSearchSpy,
        searchButtonRef: searchButton,
        setShowSearch: setShowSearchSpy
      }));
    });
    it('should spread arbitrary props', function () {
      expect(wrapper.getByTestId('searchbar')).toHaveAttribute('data-arbitrary', 'arbitrary');
    });
  });
});