// jshint esversion: 6
'use strict';

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

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

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

var _lodash = _interopRequireDefault(require("lodash"));

var _enzyme = require("../../../build/enzyme");

var _AutosuggestControl = require("./AutosuggestControl");

var predefinedData = [{
  first: 'James',
  email: 'silence@yandex.ru'
}, {
  first: 'Stephan',
  email: 'bear@gmail.com'
}, {
  first: 'David',
  email: 'drdavid@mail.ru'
}, {
  first: 'Mike',
  email: 'fish@gmail.com'
}];
describe('Autosuggest', function () {
  var props = {
    maxSuggestionsCount: 2,
    minCharactersToShowSuggestions: 3,
    getSuggestionText: function getSuggestionText(item) {
      return item.email;
    },
    getSuggestionValue: function getSuggestionValue() {},
    values: predefinedData
  };
  it('no suggestions if length of searchstring below minCharactersToShowSuggestions', function () {
    var wrapper = (0, _enzyme.mount)(_react.default.createElement(_AutosuggestControl.AutosuggestControl, props));
    var instance = wrapper.instance();
    var results = instance.getSuggestions('si');
    expect(results.length).toBe(0);
  });
  it('returns suggestions', function () {
    var wrapper = (0, _enzyme.mount)(_react.default.createElement(_AutosuggestControl.AutosuggestControl, props));
    var instance = wrapper.instance();
    var result = instance.getSuggestions('silence');
    expect(result).toEqual([predefinedData[0]]);
  });
  it('returns up to a limit not exceeding maxSuggestionsCount', function () {
    var wrapper = (0, _enzyme.mount)(_react.default.createElement(_AutosuggestControl.AutosuggestControl, props));
    var instance = wrapper.instance();
    var result = instance.getSuggestions('mail');
    expect(result.length).toBe(2);
  });
  it('returns custom suggestions', function () {
    var newProps = _lodash.default.omit(props, 'values');

    var getSuggestions = function getSuggestions(query, filter) {
      return filter(predefinedData);
    };

    var wrapper = (0, _enzyme.mount)(_react.default.createElement(_AutosuggestControl.AutosuggestControl, (0, _extends2.default)({}, newProps, {
      getSuggestions: getSuggestions
    })));
    var instance = wrapper.instance();
    var result = instance.getSuggestions('silence');
    expect(result.length).toBe(1);
  });
  it('returns Promise arrays filter with query from remote', function (next) {
    var newProps = _lodash.default.omit(props, 'values');

    var loadSuggestions = function loadSuggestions(query) {
      return new Promise(function (resolve) {
        setTimeout(function () {
          resolve(predefinedData.filter(function (item) {
            return item.email.indexOf(query) >= 0;
          }));
        }, 1);
      });
    };

    var wrapper = (0, _enzyme.mount)(_react.default.createElement(_AutosuggestControl.AutosuggestControl, (0, _extends2.default)({}, newProps, {
      getSuggestions: loadSuggestions
    })));
    var instance = wrapper.instance();
    var $result = instance.getSuggestions('gmail');
    expect($result instanceof Promise).toBe(true);
    $result.then(function (result) {
      expect(result.length).toBe(2);
      next();
    });
  });
  it('returns Promise arrays filter with innerFilter', function (next) {
    var newProps = _lodash.default.omit(props, 'values');

    var loadAllSuggestions = function loadAllSuggestions() {
      return new Promise(function (resolve) {
        setTimeout(function () {
          resolve(predefinedData);
        }, 1);
      });
    };

    var getSuggestions = function getSuggestions(query, innerFilter) {
      return loadAllSuggestions().then(function (list) {
        return innerFilter(list);
      });
    };

    var wrapper = (0, _enzyme.mount)(_react.default.createElement(_AutosuggestControl.AutosuggestControl, (0, _extends2.default)({}, newProps, {
      getSuggestions: getSuggestions
    })));
    var instance = wrapper.instance();
    var $result = instance.getSuggestions('gmail');
    expect($result instanceof Promise).toBe(true);
    $result.then(function (result) {
      expect(result.length).toBe(2);
      next();
    });
  });
});