"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 _index = _interopRequireDefault(require("../index"));

var _Tag = _interopRequireDefault(require("@instructure/ui-elements/lib/components/Tag"));

describe('TagSelect', function () {
  var onChangeStub = sinon.stub();
  var onInputChangeStub = sinon.stub();
  var ConnectedTagSelect = withContext(_index.default);
  var testbed = createTestbed(ConnectedTagSelect, {
    noMatchText: '404 TAG NOT FOUND',
    onChange: onChangeStub,
    onInputChange: onInputChangeStub
  });
  shouldRenderComponent(testbed, {}, _index.default);
  afterEach(function () {
    onChangeStub.resetHistory();
    onInputChangeStub.resetHistory();
  });
  it('should render select label', function () {
    testbed.render();
    var text = testbed.getText();
    expect(text).to.include('Tags and Metadata');
  });
  it('should render one selected option', function () {
    testbed.render({
      tags: [{
        id: '1',
        value: 'waldo'
      }]
    });
    var text = testbed.getText();
    expect(text).to.include('waldo');
  });
  it('should call onChange when a tag is removed', function () {
    testbed.render({
      tags: [{
        id: '1',
        value: 'waldo'
      }]
    });
    testbed.click(testbed.findChildByType(_Tag.default));
    expect(onChangeStub).to.be.called();
  });
  it('display empty option when entering text', function () {
    testbed.render();
    testbed.click(testbed.findChildByTagName('input'));
    var text = testbed.getText();
    expect(text).to.include('404 TAG NOT FOUND');
  });
  it('offers tag creation when text is entered',
  /*#__PURE__*/
  (0, _asyncToGenerator2.default)(
  /*#__PURE__*/
  _regenerator.default.mark(function _callee() {
    var input, createOption, text;
    return _regenerator.default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            testbed.render({
              allowNewTags: true
            });
            input = testbed.findChildByTagName('input');
            testbed.click(input);
            testbed.change(input, 'hi');
            createOption = testbed.findChildrenBySelector('[role=option]')[0];
            text = createOption.innerHTML;
            expect(text).to.include('Create new tag: hi');
            testbed.click(createOption);
            expect(onChangeStub).to.be.calledWith([{
              value: 'hi'
            }]);

          case 9:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  })));
  it('does not offer to create a tag that is already selected',
  /*#__PURE__*/
  (0, _asyncToGenerator2.default)(
  /*#__PURE__*/
  _regenerator.default.mark(function _callee2() {
    var input, emptyOption, text;
    return _regenerator.default.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            testbed.render({
              tags: [{
                value: 'waldo'
              }],
              allowNewTags: true
            });
            input = testbed.findChildrenBySelector('input[role=combobox]')[0];
            testbed.click(input);
            testbed.change(input, 'waldo');
            emptyOption = testbed.findChildrenBySelector('[role=option]')[0];
            text = emptyOption.innerHTML;
            expect(text).to.include('Tag already selected: waldo');
            testbed.click(emptyOption);
            expect(onChangeStub).to.not.be.called();

          case 9:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  })));
  it('offers all tag suggestions', function () {
    testbed.render({
      tagSuggestions: [{
        id: '1',
        value: 'tag-1'
      }, {
        id: '2',
        value: 'tag-2'
      }]
    });
    testbed.click(testbed.findChildByTagName('input'));
    var text = testbed.getText();
    expect(text).to.include('tag-1');
    expect(text).to.include('tag-2');
  });
  it('does not offer tag creation on suggested tags', function () {
    testbed.render({
      allowNewTags: true,
      tagSuggestions: [{
        id: '1',
        value: 'tag-1'
      }]
    });
    testbed.click(testbed.findChildByTagName('input'));
    var text = testbed.getText();
    expect(text).to.not.include('Create new tag');
  });
  it('does not remove existing tag with text matching the tag id', function () {
    testbed.render({
      tags: [{
        value: 'waldo'
      }],
      tagSuggestions: [{
        id: '1',
        value: 'tag-1'
      }]
    });
    var text = testbed.getText();
    expect(text).to.include('waldo');
  });
  describe('a11y tests', function () {
    it('should meet a11y standards', function (done) {
      testbed.render();
      testbed.checkA11yStandards(done);
    });
  });
});