// jshint esversion: 6
'use strict';

import _extends from "@babel/runtime/helpers/extends";
import React from 'react';
import _ from 'lodash';
import { mount } from '../../../build/enzyme';
import { AutosuggestControl } from './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 = mount(React.createElement(AutosuggestControl, props));
    var instance = wrapper.instance();
    var results = instance.getSuggestions('si');
    expect(results.length).toBe(0);
  });
  it('returns suggestions', function () {
    var wrapper = mount(React.createElement(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 = mount(React.createElement(AutosuggestControl, props));
    var instance = wrapper.instance();
    var result = instance.getSuggestions('mail');
    expect(result.length).toBe(2);
  });
  it('returns custom suggestions', function () {
    var newProps = _.omit(props, 'values');

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

    var wrapper = mount(React.createElement(AutosuggestControl, _extends({}, 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 = _.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 = mount(React.createElement(AutosuggestControl, _extends({}, 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 = _.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 = mount(React.createElement(AutosuggestControl, _extends({}, 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();
    });
  });
});