"use strict";

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

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

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

var _immutable = _interopRequireDefault(require("immutable"));

var _presenter = _interopRequireDefault(require("../presenter"));

describe('TagSuggest', function () {
  var tags = [{
    id: '1',
    tag_value: 'waldo'
  }];
  var props = {
    parentType: 'Bank',
    parentId: '0',
    tags: tags,
    setTagInput: sinon.stub(),
    getTagSuggestions: sinon.stub(),
    setTagSuggestions: sinon.stub(),
    onChange: sinon.stub()
  };
  var connectedTestbed = createTestbed(withContext(_presenter.default), props);
  var clock = null;
  beforeEach(
  /*#__PURE__*/
  (0, _asyncToGenerator2.default)(
  /*#__PURE__*/
  _regenerator.default.mark(function _callee() {
    return _regenerator.default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            clock = sinon.useFakeTimers();

          case 1:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  })));
  afterEach(
  /*#__PURE__*/
  (0, _asyncToGenerator2.default)(
  /*#__PURE__*/
  _regenerator.default.mark(function _callee2() {
    return _regenerator.default.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            clock.restore();

          case 1:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  })));
  it('sets tag input on tag input change',
  /*#__PURE__*/
  (0, _asyncToGenerator2.default)(
  /*#__PURE__*/
  _regenerator.default.mark(function _callee3() {
    var suggest;
    return _regenerator.default.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            connectedTestbed.render();
            suggest = connectedTestbed.findChildrenByType(_presenter.default);
            suggest[0].handleTagInputChange('boo');
            expect(suggest[0].props.setTagInput).to.be.calledWith('Bank', '0', 'boo');

          case 4:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  })));
  it('calls tag suggestion on tag input change',
  /*#__PURE__*/
  (0, _asyncToGenerator2.default)(
  /*#__PURE__*/
  _regenerator.default.mark(function _callee4() {
    var suggest;
    return _regenerator.default.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            connectedTestbed.render();
            suggest = connectedTestbed.findChildrenByType(_presenter.default);
            suggest[0].handleTagInputChange('boo');
            clock.tick(200);
            expect(suggest[0].props.getTagSuggestions).to.be.calledWith('Bank', '0', 'boo');

          case 5:
          case "end":
            return _context4.stop();
        }
      }
    }, _callee4);
  })));
  it('does not get tag suggestion and resets null tag input',
  /*#__PURE__*/
  (0, _asyncToGenerator2.default)(
  /*#__PURE__*/
  _regenerator.default.mark(function _callee5() {
    var suggest;
    return _regenerator.default.wrap(function _callee5$(_context5) {
      while (1) {
        switch (_context5.prev = _context5.next) {
          case 0:
            connectedTestbed.render();
            suggest = connectedTestbed.findChildrenByType(_presenter.default);
            suggest[0].handleTagInputChange(null);
            clock.tick(200);
            expect(suggest[0].props.setTagSuggestions).to.be.calledWith('Bank', '0', null);

          case 5:
          case "end":
            return _context5.stop();
        }
      }
    }, _callee5);
  })));
  it('proposes default suggestions on null input',
  /*#__PURE__*/
  (0, _asyncToGenerator2.default)(
  /*#__PURE__*/
  _regenerator.default.mark(function _callee6() {
    var suggest, suggestions;
    return _regenerator.default.wrap(function _callee6$(_context6) {
      while (1) {
        switch (_context6.prev = _context6.next) {
          case 0:
            connectedTestbed.render({
              defaultSuggestions: [{
                id: '1',
                value: 'boo'
              }],
              tags: []
            });
            suggest = connectedTestbed.findChildrenByType(_presenter.default);
            suggestions = suggest[0].getTagSuggestions().toJS();
            expect(suggestions).to.deep.equal([{
              id: '1',
              value: 'boo'
            }]);

          case 4:
          case "end":
            return _context6.stop();
        }
      }
    }, _callee6);
  })));
  it('excludes tag suggestions for tags already selected',
  /*#__PURE__*/
  (0, _asyncToGenerator2.default)(
  /*#__PURE__*/
  _regenerator.default.mark(function _callee7() {
    var suggest, suggestions;
    return _regenerator.default.wrap(function _callee7$(_context7) {
      while (1) {
        switch (_context7.prev = _context7.next) {
          case 0:
            connectedTestbed.render({
              tagSuggestions: _immutable.default.fromJS([{
                id: '1',
                value: 'boo'
              }, {
                id: '2',
                value: 'hey'
              }]),
              tags: [{
                id: '1',
                value: 'boo'
              }]
            });
            suggest = connectedTestbed.findChildrenByType(_presenter.default);
            suggestions = suggest[0].getTagSuggestions().toJS();
            expect(suggestions).to.deep.equal([{
              id: '2',
              value: 'hey'
            }]);

          case 4:
          case "end":
            return _context7.stop();
        }
      }
    }, _callee7);
  })));
});