// jshint esversion: 6
'use strict';

import React from 'react';
import _ from 'lodash';
import {mount} from '../../../build/enzyme';

import {AutosuggestControl} from './AutosuggestControl';

const 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', () => {
    const props = {
        maxSuggestionsCount: 2,
        minCharactersToShowSuggestions: 3,
        getSuggestionText(item) {
            return item.email;
        },
        getSuggestionValue() {},
        values: predefinedData
    };

    it('no suggestions if length of searchstring below minCharactersToShowSuggestions', () => {
        const wrapper = mount(<AutosuggestControl {...props} />);
        const instance = wrapper.instance();
        const results = instance.getSuggestions('si');
        expect(results.length).toBe(0);
    });

    it('returns suggestions', () => {
        const wrapper = mount(<AutosuggestControl {...props} />);
        const instance = wrapper.instance();
        const result = instance.getSuggestions('silence');
        expect(result).toEqual([predefinedData[0]]);
    });

    it('returns up to a limit not exceeding maxSuggestionsCount', () => {
        const wrapper = mount(<AutosuggestControl {...props} />);
        const instance = wrapper.instance();
        const result = instance.getSuggestions('mail');
        expect(result.length).toBe(2);
    });

    it('returns custom suggestions', () => {
        let newProps = _.omit(props, 'values');
        const getSuggestions = (query, filter) => filter(predefinedData);
        const wrapper = mount(<AutosuggestControl {...newProps} getSuggestions={getSuggestions} />);
        const instance = wrapper.instance();
        const result = instance.getSuggestions('silence');
        expect(result.length).toBe(1);
    });

    it('returns Promise arrays filter with query from remote', next => {
        let newProps = _.omit(props, 'values');

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

        const wrapper = mount(
            <AutosuggestControl {...newProps} getSuggestions={loadSuggestions} />
        );
        const instance = wrapper.instance();
        const $result = instance.getSuggestions('gmail');

        expect($result instanceof Promise).toBe(true);

        $result.then(result => {
            expect(result.length).toBe(2);
            next();
        });
    });

    it('returns Promise arrays filter with innerFilter', next => {
        let newProps = _.omit(props, 'values');

        const loadAllSuggestions = () => {
            return new Promise(resolve => {
                setTimeout(() => {
                    resolve(predefinedData);
                }, 1);
            });
        };

        const getSuggestions = (query, innerFilter) =>
            loadAllSuggestions().then(list => innerFilter(list));

        const wrapper = mount(<AutosuggestControl {...newProps} getSuggestions={getSuggestions} />);
        const instance = wrapper.instance();
        const $result = instance.getSuggestions('gmail');

        expect($result instanceof Promise).toBe(true);

        $result.then(result => {
            expect(result.length).toBe(2);
            next();
        });
    });
});
