"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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

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

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

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

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

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

var _react = _interopRequireWildcard(require("react"));

var _Button = _interopRequireDefault(require("@instructure/ui-buttons/lib/components/Button"));

var _makeScrollable = require("../makeScrollable");

var componentDidUpdateStub = sinon.stub();

var TestComponent =
/*#__PURE__*/
function (_Component) {
  (0, _inherits2.default)(TestComponent, _Component);

  function TestComponent() {
    (0, _classCallCheck2.default)(this, TestComponent);
    return (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(TestComponent).apply(this, arguments));
  }

  (0, _createClass2.default)(TestComponent, [{
    key: "componentDidUpdate",
    value: function componentDidUpdate() {
      componentDidUpdateStub();
    }
  }, {
    key: "render",
    value: function render() {
      return _react.default.createElement("div", null);
    }
  }]);
  TestComponent.displayName = "TestComponent";
  return TestComponent;
}(_react.Component);

var ExtendedComponent = (0, _makeScrollable.unconnectedExtension)(TestComponent);
var ExtendedComponentButton = (0, _makeScrollable.unconnectedExtension)(_Button.default);
describe('makeScrollable', function () {
  var keyForComponent = 'keyForComponent';
  var clearsScrollStub = sinon.stub();
  var scrollSpy = sinon.spy(ExtendedComponent.prototype, 'scrollIntoView');
  var setFocusSpy = sinon.spy(ExtendedComponentButton.prototype, 'setFocus');
  var testbed = createTestbed(ExtendedComponent, {
    scrollKey: keyForComponent,
    scrolledComponentKey: 'someotherkey',
    scrollingFocus: true,
    scrollingActions: {
      clearsScrolledComponentKey: clearsScrollStub
    }
  });
  var testbedFocus = createTestbed(ExtendedComponentButton, {
    shouldFocusOnMount: true
  });
  afterEach(function () {
    scrollSpy.resetHistory();
    setFocusSpy.resetHistory();
    clearsScrollStub.resetHistory();
    componentDidUpdateStub.resetHistory();
  });
  it('calls the original componentDidUpdate', function () {
    testbed.render({});
    testbed.render({
      scrolledComponentKey: keyForComponent
    });
    expect(componentDidUpdateStub.called).to.be.true();
  });
  it('scrolls if rendered with a new scrolledComponentKey that matches scrollKey', function () {
    testbed.render({});
    testbed.render({
      scrolledComponentKey: keyForComponent
    });
    expect(scrollSpy).to.have.been.calledWith(true);
  });
  it('does not scroll if rendered with a new scrolledComponentKey does not match scrollKey', function () {
    testbed.render({});
    expect(scrollSpy).not.to.have.been.called();
  });
  it('does not re-scroll scrollKey matches but scrolledComponentKey has not changed', function () {
    testbed.render({});
    testbed.render({
      scrolledComponentKey: keyForComponent
    });
    testbed.render({
      scrolledComponentKey: keyForComponent,
      someUnrelatedVal: 'trigger update'
    });
    expect(scrollSpy).to.have.been.calledOnce();
  });
  it('calls to clear the scrolled component key', function () {
    testbed.render({});
    testbed.render({
      scrolledComponentKey: keyForComponent
    });
    expect(clearsScrollStub).to.have.been.calledOnce();
  });
  it('calls the set focus function', function () {
    testbedFocus.render({});
    expect(setFocusSpy).to.have.been.called();
  });
  it('does not call focus again when the component updates', function () {
    var subject = testbedFocus.mount();
    subject.setProps({
      scrolledComponentKey: 'foo'
    });
    expect(setFocusSpy).to.have.been.calledOnce();
  });
  it('matches the active focus element with the button defined on the makescrollable object', function () {
    var wrapper = testbedFocus.render({});
    expect(document.activeElement).to.equal(wrapper._button);
  });
});