"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _react = _interopRequireDefault(require("react"));

var _enzyme = require("enzyme");

var _ = _interopRequireDefault(require("."));

var _RefinementList = _interopRequireDefault(require("./components/RefinementList"));

var _SearchResults = _interopRequireDefault(require("./components/SearchResults"));

describe('AlgoliaSearch', function () {
  var node;
  beforeEach(function () {
    var props = {
      appId: 'T28YKFATPY',
      searchApiKey: 'c9c9c6147013bac3d8882019147443e0',
      indexName: 'readme_search_v2',
      projectsMeta: [{
        subdomain: 'testsub',
        modules: {
          docs: true,
          reference: true,
          custompages: false
        }
      }, {
        subdomain: 'sub2',
        modules: {
          docs: false,
          reference: true,
          custompages: false
        }
      }]
    };
    var div = document.createElement('div');
    window.domNode = div;
    document.body.appendChild(div);
    node = (0, _enzyme.mount)( /*#__PURE__*/_react["default"].createElement(_["default"], props), {
      attachTo: window.domNode
    });
  });
  it('displays the <AlgoliaSearch> element', function () {
    var sbox = node.find('input[type="search"]');
    var tabs = node.find('.Tabs-listItem');
    sbox.instance().value = 'all'; // have to get the node...

    sbox.first().simulate('change'); // have to use the wrapper...

    sbox.first().simulate('input'); // have to use the wrapper...

    tabs.map(function (tab) {
      return tab.simulate('click');
    });
    expect(node.state().tabFilters).toStrictEqual(['docs', 'reference']);
    expect(node.find('.AlgoliaSearch').hasClass('AlgoliaSearch')).toBe(true);
  });
  it('should whitelist modules based on refinementlist selection', function () {
    var refinementList = node.find(_RefinementList["default"]);
    refinementList.prop('onRefine')('sub2');
    expect(refinementList).toHaveLength(1);
    expect(node.state().tabFilters).toStrictEqual(['reference']);
  });
  it.skip('should handle refinemenlist onBlur', function () {
    var addListener = jest.fn();
    window.matchMedia = jest.fn().mockReturnValue({
      addListener: addListener
    });
    var refinementList = node.find(_RefinementList["default"]);
    var preventDefault = jest.fn();
    refinementList.prop('onBlur')({
      target: {},
      preventDefault: preventDefault,
      stopPropagation: jest.fn()
    });
    expect(preventDefault).toHaveBeenCalled();
  });
  it('should handle search result reload', function () {
    var searchRes = node.find(_SearchResults["default"]);
    var resultSpy = jest.spyOn(document, 'querySelector');
    searchRes.prop('onReload')();
    expect(resultSpy).toHaveBeenCalled();
  });
  describe('handleTabChange()', function () {
    it('should return if tab type is object', function () {
      var res = node.instance().handleTabChange({});
      expect(res).toBeUndefined();
    });
    it('should not call setState if tab does not exist in state', function () {
      var stateSpy = jest.spyOn(node.instance(), 'setState');
      node.instance().handleTabChange('test');
      expect(stateSpy).toHaveBeenCalledTimes(0);
    });
  });
});