"use strict";

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

var _asyncSearch = _interopRequireDefault(require("../asyncSearch"));

describe('AsyncSearch', function () {
  var onSelectionChange = sinon.stub();
  var promiseStub = sinon.stub();
  var debounceDelay = 500;
  var props = {
    search: function search() {
      return {
        then: promiseStub
      };
    },
    onSelectionChange: onSelectionChange,
    selectedOption: [],
    debounceDelay: debounceDelay
  };
  var testbed = createTestbed(_asyncSearch.default, props);
  var clock = null;
  beforeEach(function () {
    clock = sinon.useFakeTimers();
  });
  afterEach(function () {
    clock.restore();
    onSelectionChange.resetHistory();
    promiseStub.resetHistory();
  });
  shouldRenderComponent(testbed, {}, _asyncSearch.default);
  it('clears search results when empty input', function () {
    var subject = testbed.render();
    sinon.spy(subject, 'setState');
    subject.handleSearchInputChange({}, 'new search value');
    subject.handleSearchInputChange({}, '');
    expect(subject.setState).to.have.been.calledWith({
      emptyOption: 'Write to search',
      searchResult: []
    });
  });
  it('clears seach results when the menu closes if empty input', function () {
    var subject = testbed.render();
    sinon.spy(subject, 'setState');
    subject.handleClose();
    expect(subject.setState).to.have.been.calledWith({
      emptyOption: 'Write to search',
      searchResult: []
    });
  });
  it('should delay the search call', function () {
    var shallowTestbed = testbed.renderShallowly();
    var change = shallowTestbed.find('Select').at(0).props().onInputChange;
    change({}, 'new search value');
    clock.tick(debounceDelay - 1);
    expect(promiseStub).to.not.be.called();
    clock.tick(1);
    expect(promiseStub).to.be.calledOnce();
  });
  it('should call search with a debounce', function () {
    var shallowTestbed = testbed.renderShallowly();
    var change = shallowTestbed.find('Select').at(0).props().onInputChange;
    change({}, 'new search value');
    clock.tick(debounceDelay - 1);
    expect(promiseStub).to.not.be.called();
    change({}, 'haha! foiled!');
    clock.tick(debounceDelay - 1);
    expect(promiseStub).to.not.be.called();
    clock.tick(1);
    expect(promiseStub).to.be.calledOnce();
  });
  it('should populate the search results once they arrive', function () {
    var shallowTestbed = testbed.renderShallowly({
      search: function search() {
        return {
          then: function then(fn) {
            return fn([{
              uuid: '0',
              fullName: 'first fullName',
              avatar: 'http://avatars.com/avatar1'
            }, {
              uuid: '1',
              fullName: 'second fullName',
              avatar: 'http://avatars.com/avatar2'
            }]);
          }
        };
      }
    });
    var change = shallowTestbed.find('Select').at(0).props().onInputChange;
    change({}, 'new search value');
    clock.tick(100);
    expect(shallowTestbed.find('Select').at(0).props().children.length).to.equal(0);
    clock.tick(debounceDelay);
    expect(shallowTestbed.find('Select').at(0).props().children.length).to.equal(2);
  });
});