"use strict";

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

var _immutable = require("immutable");

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

var _constants = require("@instructure/quiz-common/lib/constants");

describe('SharingModal', function () {
  var onDismiss = sinon.stub();
  var onSubmit = sinon.stub();
  var getUserInfoStub = sinon.stub();
  var sharedBankStub = sinon.stub();
  var getSharedBanksStub = sinon.stub();
  var removeSharedBankStub = sinon.stub();
  var getBankStub = sinon.stub();

  var search = function search() {
    return {
      then: function then(fn) {
        return fn([{
          uuid: '0',
          fullName: 'first fullName',
          avatar: 'http://avatars.com/avatar0'
        }, {
          uuid: '1',
          fullName: 'second fullName',
          avatar: 'http://avatars.com/avatar1'
        }, {
          uuid: '2',
          fullName: 'third fullName',
          avatar: 'http://avatars.com/avatar2'
        }, {
          uuid: '3',
          fullName: 'fourth fullName',
          avatar: 'http://avatars.com/avatar3'
        }]);
      }
    };
  };

  var props = {
    isOpen: true,
    bank: (0, _immutable.Map)({
      bankId: '1',
      title: 'This is a title'
    }),
    onDismiss: onDismiss,
    onSubmit: onSubmit,
    search: search,
    getUserInfo: getUserInfoStub,
    shareBank: sharedBankStub,
    getSharedBanks: getSharedBanksStub,
    removeSharedBank: removeSharedBankStub,
    getBank: getBankStub,
    searchSelection: [{
      uuid: '3',
      fullName: 'third fullName',
      avatar: 'http://avatars.com/avatar3',
      permission: _constants.CAN_EDIT
    }, {
      uuid: '4',
      fullName: 'forth fullName',
      avatar: 'http://avatars.com/avatar4',
      permission: _constants.CAN_EDIT
    }]
  };
  var testbed = createTestbed(withContext(_presenter.default), props);
  afterEach(function () {
    onDismiss.resetHistory();
    onSubmit.resetHistory();
  });
  shouldRenderComponent(testbed, {}, _presenter.default);
  describe('handleDismiss', function () {
    it('calls focus on the modalTrigger if passed', function () {
      var focusStub = sinon.stub();
      var subject = testbed.render({
        modalTrigger: {
          focus: focusStub
        }
      });
      subject.refs.composedComponent.handleDismiss();
      expect(focusStub).to.have.been.called();
    });
    it('removes the owner temp record', function () {
      var focusStub = sinon.stub();
      var subject = testbed.render({
        modalTrigger: {
          focus: focusStub
        }
      });
      subject.refs.composedComponent.handleDismiss();
      expect(removeSharedBankStub).to.have.been.calledWith('-1');
    });
  });
  describe('handleSubmit', function () {
    it('calls focus on the modalTrigger if passed', function () {
      var focusStub = sinon.stub();
      var subject = testbed.render({
        modalTrigger: {
          focus: focusStub
        }
      });
      subject.refs.composedComponent.handleSubmit();
      expect(focusStub).to.have.been.called();
    });
    it('should call getBank correctly', function (done) {
      var subject = testbed.render({
        bank: {
          id: '1',
          title: 'This is a title',
          sharedCount: 2
        }
      });
      subject.refs.composedComponent.handleSearchSelectionChange([{
        uuid: '0',
        fullName: 'first fullName',
        avatar: 'http://avatars.com/avatar1'
      }]);
      subject.refs.composedComponent.handleShare();
      subject.refs.composedComponent.handleSubmit();
      setTimeout(function () {
        expect(getBankStub).to.have.been.calledWith('1');
        done();
      });
    });
    it('should call removeSharedBank correctly', function (done) {
      var subject = testbed.render({
        bank: {
          id: '1',
          title: 'This is a title',
          sharedCount: 2
        }
      });
      subject.refs.composedComponent.handleSearchSelectionChange([{
        uuid: '0',
        fullName: 'first fullName',
        avatar: 'http://avatars.com/avatar1'
      }]);
      subject.refs.composedComponent.handleShare();
      subject.refs.composedComponent.handleSubmit();
      setTimeout(function () {
        expect(removeSharedBankStub).to.have.been.calledWith('-1');
        done();
      });
    });
  });
  it('should update search selection correctly', function () {
    var subject = testbed.render();
    subject.refs.composedComponent.handleSearchSelectionChange([1, 2, 3]);
    expect(subject.refs.composedComponent.state.searchSelection).to.deep.equal([1, 2, 3]);
  });
  it('should update newPeople correctly', function () {
    var subject = testbed.render({
      bank: {
        bankId: '1',
        title: 'This is a title'
      }
    });
    subject.refs.composedComponent.handleSearchSelectionChange([{
      uuid: '0',
      fullName: 'first fullName',
      avatar: 'http://avatars.com/avatar1'
    }]);
    subject.refs.composedComponent.handleShare();
    expect(subject.refs.composedComponent.state.newPeople).to.deep.equal((0, _immutable.List)([(0, _immutable.Map)({
      id: '0',
      userUuid: '0',
      fullName: 'first fullName',
      avatar: 'http://avatars.com/avatar1',
      permission: _constants.CAN_EDIT
    })]));
  });
  it('should call getSharedBanks with right parameters', function () {
    var bank = {
      id: '1',
      title: 'This is a title'
    };
    testbed.render({
      bank: bank
    });
    expect(getSharedBanksStub).to.have.been.calledWith(bank, getUserInfoStub);
  });
  it('should not call getSharedBanks when sharedBanks has data', function () {
    var getSharedBanksStub = sinon.stub();
    testbed.render({
      sharedBanks: (0, _immutable.List)([(0, _immutable.Map)({
        id: '1',
        userUuid: '1',
        bankId: '1',
        permission: _constants.CAN_EDIT,
        full_name: 'firstFullName',
        avatar: 'http://avatars.com/avatar1'
      }), (0, _immutable.Map)({
        id: '2',
        userUuid: '2',
        bankId: '1',
        permission: _constants.CAN_EDIT,
        full_name: 'secondFullName',
        avatar: 'http://avatars.com/avatar2'
      })])
    });
    expect(getSharedBanksStub).to.not.have.been.called();
  });
  describe('handleSearchFilter', function () {
    it('should filter out current user when sharedBanks has no data', function () {
      var subject = testbed.render({
        bank: {
          title: 'bank1',
          owner: '1',
          sharedCount: 0
        }
      });
      var result = subject.refs.composedComponent.handleSearchFilter([{
        value: '1',
        fullName: 'first fullName',
        avatar: 'http://avatars.com/avatar1'
      }, {
        value: '2',
        fullName: 'second fullName',
        avatar: 'http://avatars.com/avatar2'
      }]);
      expect(result).to.deep.equal([{
        value: '2',
        fullName: 'second fullName',
        avatar: 'http://avatars.com/avatar2'
      }]);
    });
    it('should NOT filter out current user when sharedBanks has data', function () {
      var subject = testbed.render({
        bank: {
          title: 'bank1',
          owner: '1',
          sharedCount: 2
        }
      });
      var result = subject.refs.composedComponent.handleSearchFilter([{
        value: '0',
        fullName: 'first fullName',
        avatar: 'http://avatars.com/avatar0'
      }, {
        value: '1',
        fullName: 'second fullName',
        avatar: 'http://avatars.com/avatar1'
      }]);
      expect(result).to.deep.equal([{
        value: '0',
        fullName: 'first fullName',
        avatar: 'http://avatars.com/avatar0'
      }, {
        value: '1',
        fullName: 'second fullName',
        avatar: 'http://avatars.com/avatar1'
      }]);
    });
    it('should filter out people from autocomplete', function () {
      var subject = testbed.render({
        bank: {
          title: 'bank1',
          owner: '1',
          sharedCount: 2
        },
        sharedBanks: (0, _immutable.List)([(0, _immutable.Map)({
          id: '1',
          userUuid: '1',
          bankId: '1',
          permission: _constants.CAN_EDIT,
          full_name: 'first FullName',
          avatar: 'http://avatars.com/avatar1'
        }), (0, _immutable.Map)({
          id: '2',
          userUuid: '2',
          bankId: '1',
          permission: _constants.CAN_EDIT,
          full_name: 'second FullName',
          avatar: 'http://avatars.com/avatar2'
        })])
      });
      var result = subject.refs.composedComponent.handleSearchFilter([{
        value: '2',
        fullName: 'second fullName',
        avatar: 'http://avatars.com/avatar2'
      }, {
        value: '3',
        fullName: 'third fullName',
        avatar: 'http://avatars.com/avatar3'
      }]);
      expect(result).to.deep.equal([{
        value: '3',
        fullName: 'third fullName',
        avatar: 'http://avatars.com/avatar3'
      }]);
    });
    it('should populate peopleToRemove correctly', function () {
      var subject = testbed.render({
        sharedBanks: (0, _immutable.List)([(0, _immutable.Map)({
          id: '1',
          userUuid: '1',
          bankId: '1',
          permission: _constants.CAN_EDIT,
          full_name: 'first FullName',
          avatar: 'http://avatars.com/avatar1'
        }), (0, _immutable.Map)({
          id: '2',
          userUuid: '2',
          bankId: '1',
          permission: _constants.CAN_EDIT,
          full_name: 'second FullName',
          avatar: 'http://avatars.com/avatar2'
        })])
      });
      subject.refs.composedComponent.handlePersonRemove('2', false);
      expect(subject.refs.composedComponent.state.peopleToRemove).to.deep.equal(['2']);
    });
    it('should remove people from peopleToRemove correctly', function () {
      var subject = testbed.render({
        bank: {
          title: 'bank1',
          owner: '1',
          sharedCount: 0
        },
        sharedBanks: (0, _immutable.List)([(0, _immutable.Map)({
          id: '1',
          userUuid: '1',
          bankId: '1',
          permission: _constants.CAN_EDIT,
          full_name: 'first FullName',
          avatar: 'http://avatars.com/avatar1'
        }), (0, _immutable.Map)({
          id: '2',
          userUuid: '2',
          bankId: '1',
          permission: _constants.CAN_EDIT,
          full_name: 'second FullName',
          avatar: 'http://avatars.com/avatar2'
        })])
      });
      subject.refs.composedComponent.handlePersonRemove('1', false);
      subject.refs.composedComponent.handlePersonRemove('2', false);
      subject.refs.composedComponent.handlePersonRemove('1', true);
      expect(subject.refs.composedComponent.state.peopleToRemove).to.deep.equal(['2']);
    });
    it('should render spinner if sharedBanks has not loaded yet', function () {
      testbed.render();
      expect(testbed.getText()).to.include('Loading...');
    });
    it('should NOT render spinner if sharedBanks has data', function () {
      testbed.render({
        bank: {
          title: 'bank1',
          owner: '1',
          sharedCount: 2
        },
        sharedBanks: (0, _immutable.List)([(0, _immutable.Map)({
          id: '1',
          userUuid: '1',
          bankId: '1',
          permission: _constants.CAN_EDIT,
          full_name: 'first FullName',
          avatar: 'http://avatars.com/avatar1'
        }), (0, _immutable.Map)({
          id: '2',
          userUuid: '2',
          bankId: '1',
          permission: _constants.CAN_EDIT,
          full_name: 'second FullName',
          avatar: 'http://avatars.com/avatar2'
        })])
      });
      expect(testbed.getText()).to.not.include('Loading...');
    });
  });
});