"use strict";

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

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

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

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

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

var _Fetcher = _interopRequireDefault(require("../../../common/util/Fetcher"));

var _fakeFetch = _interopRequireDefault(require("../../../../tests/util/fakeFetch"));

var _jsonify = require("../../../common/util/jsonify");

var _checkActionDispatched = _interopRequireWildcard(require("../../../../tests/util/checkActionDispatched"));

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

var _callHandlers = require("../../../common/api/callHandlers");

var _ui = require("../../../common/actions/ui");

var _banks = require("../../../common/actions/banks");

var _banks2 = require("../banks");

describe('banks api actions', function () {
  var bankData = {
    id: '1',
    title: 'Name',
    owner: '1'
  };
  var sharedBanksData = [{
    id: '1',
    user_uuid: '1',
    bank_id: '1',
    permission: _constants.CAN_EDIT
  }, {
    id: '2',
    user_uuid: '2',
    bank_id: '1',
    permission: _constants.CAN_EDIT
  }];
  var getUserInfoData = [{
    id: '1',
    uuid: '1',
    fullName: 'firstFullName',
    avatar: 'http://avatars.com/avatar1'
  }, {
    id: '2',
    uuid: '2',
    fullName: 'secondFullName',
    avatar: 'http://avatars.com/avatar2'
  }];
  var sharedBanksDataMerged = [{
    id: '1',
    userUuid: '1',
    bankId: '1',
    permission: _constants.CAN_EDIT,
    fullName: 'firstFullName',
    avatar: 'http://avatars.com/avatar1'
  }, {
    id: '2',
    userUuid: '2',
    bankId: '1',
    permission: _constants.CAN_EDIT,
    fullName: 'secondFullName',
    avatar: 'http://avatars.com/avatar2'
  }];
  var banksData = [bankData];
  var pagedBanksData = [banksData, [{
    id: '2',
    title: 'Foo',
    owner: '1'
  }]];
  var dispatchStub = sinon.stub().resolves(bankData);
  var getUserInfoStub = sinon.stub().resolves(getUserInfoData);
  var fetchStub = sinon.stub();
  var dispatchStubIgnore = sinon.stub();
  var getStateStub = sinon.stub();
  var fakeState = {
    ui: {
      activeBankId: '5'
    },
    quizzes: {
      activeQuizId: '7'
    }
  };
  useFakeFetching({
    dispatchStub: dispatchStub,
    getStateStub: getStateStub,
    fetchStub: fetchStub,
    fakeState: fakeState
  });
  describe('getBanks', function () {
    var stubbedGetBanksCall = function stubbedGetBanksCall() {
      var response = (0, _jsonify.jsoniblePromise)(banksData);
      _Fetcher.default.fakeFetch = (0, _fakeFetch.default)(response);
      return (0, _banks2.getBanks)()(dispatchStub, getStateStub);
    };

    it('should hit the right endpoint', function () {
      _Fetcher.default.fakeFetch = fetchStub;
      (0, _banks2.getBanks)()(dispatchStubIgnore, getStateStub);
      expect(fetchStub).to.have.been.calledWith('/api/banks');
    });
    it('should trigger additional actions',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee() {
      var expectedResponse;
      return _regenerator.default.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              _context.next = 2;
              return stubbedGetBanksCall();

            case 2:
              expectedResponse = (0, _callHandlers.handleBanksResponse)(banksData);
              (0, _checkActionDispatched.default)(dispatchStub, expectedResponse[0]);

            case 4:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    })));
    it('should accept parameters',
    /*#__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:
              _Fetcher.default.fakeFetch = fetchStub;
              (0, _banks2.getBanks)({
                page: 2
              })(dispatchStubIgnore, getStateStub);
              expect(fetchStub).to.have.been.calledWith('/api/banks?page=2');

            case 3:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2);
    })));
  });
  describe('getAllBanks', function () {
    var makeFakeFetch = function makeFakeFetch() {
      return sinon.stub().onCall(0).resolves((0, _jsonify.jsoniblePromise)(pagedBanksData[0], {
        headers: {
          'per-page': '1',
          total: '2'
        }
      })).onCall(1).resolves((0, _jsonify.jsoniblePromise)(pagedBanksData[1]));
    };

    it('should fetch all pages',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee3() {
      return _regenerator.default.wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              _Fetcher.default.fakeFetch = makeFakeFetch();
              _context3.next = 3;
              return (0, _banks2.getAllBanks)()(dispatchStubIgnore, getStateStub);

            case 3:
              expect(_Fetcher.default.fakeFetch).to.have.been.calledTwice();
              expect(_Fetcher.default.fakeFetch.args[0][0]).to.equal('/api/banks?per_page=100'); // second call matches page size returned by server

              expect(_Fetcher.default.fakeFetch.args[1][0]).to.equal('/api/banks?page=2&per_page=1');

            case 6:
            case "end":
              return _context3.stop();
          }
        }
      }, _callee3);
    })));
    it('should combine all the results and return the ids',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee4() {
      var response;
      return _regenerator.default.wrap(function _callee4$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              _Fetcher.default.fakeFetch = makeFakeFetch();
              _context4.next = 3;
              return (0, _banks2.getAllBanks)()(dispatchStubIgnore, getStateStub);

            case 3:
              response = _context4.sent;
              expect(response).to.have.length(2);
              expect(response).to.deep.equal(['1', '2']);

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, _callee4);
    })));
    it('should dispatch event with the full banks',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee5() {
      var expectedResponse;
      return _regenerator.default.wrap(function _callee5$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              _Fetcher.default.fakeFetch = makeFakeFetch();
              _context5.next = 3;
              return (0, _banks2.getAllBanks)()(dispatchStub, getStateStub);

            case 3:
              expectedResponse = (0, _callHandlers.handleBanksResponse)([].concat((0, _toConsumableArray2.default)(pagedBanksData[0]), (0, _toConsumableArray2.default)(pagedBanksData[1])));
              (0, _checkActionDispatched.default)(dispatchStub, expectedResponse[0]);

            case 5:
            case "end":
              return _context5.stop();
          }
        }
      }, _callee5);
    })));
  });
  describe('getBank', function () {
    var stubbedGetBankCall = function stubbedGetBankCall() {
      var response = (0, _jsonify.jsoniblePromise)(bankData);
      _Fetcher.default.fakeFetch = (0, _fakeFetch.default)(response);
      return (0, _banks2.getBank)(1)(dispatchStub, getStateStub);
    };

    it('should hit the right endpoint', function () {
      _Fetcher.default.fakeFetch = fetchStub;
      (0, _banks2.getBank)(1)(dispatchStubIgnore, getStateStub);
      expect(fetchStub).to.have.been.calledWith('/api/banks/1');
    });
    it('should trigger additional actions',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee6() {
      var expectedResponse;
      return _regenerator.default.wrap(function _callee6$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              _context6.next = 2;
              return stubbedGetBankCall();

            case 2:
              expectedResponse = (0, _callHandlers.handleBanksResponse)([bankData]);
              (0, _checkActionDispatched.default)(dispatchStub, expectedResponse[0]);

            case 4:
            case "end":
              return _context6.stop();
          }
        }
      }, _callee6);
    })));
  });
  describe('createBank', function () {
    var stubbedCreateBankCall = function stubbedCreateBankCall() {
      var response = (0, _jsonify.jsoniblePromise)(bankData);
      _Fetcher.default.fakeFetch = (0, _fakeFetch.default)(response);
      return (0, _banks2.createBank)({
        title: 'newbank'
      })(dispatchStub, getStateStub);
    };

    it('should hit the right endpoint', function () {
      _Fetcher.default.fakeFetch = fetchStub;
      (0, _banks2.createBank)({
        title: 'newbank'
      })(dispatchStubIgnore, getStateStub);
      expect(fetchStub).to.have.been.calledWith('/api/banks');
    });
    it('should provide bank language',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee7() {
      return _regenerator.default.wrap(function _callee7$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              _Fetcher.default.fakeFetch = fetchStub;
              (0, _banks2.createBank)({
                title: 'newbank',
                language: 'es'
              })(dispatchStubIgnore, getStateStub);
              expect(fetchStub).to.have.been.calledWith('/api/banks');
              expect(JSON.parse(fetchStub.args[0][1].body)).to.deep.equal({
                bank: {
                  title: 'newbank',
                  language: 'es'
                }
              });

            case 4:
            case "end":
              return _context7.stop();
          }
        }
      }, _callee7);
    })));
    it('should trigger additional actions',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee8() {
      var expectedResponse;
      return _regenerator.default.wrap(function _callee8$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              _context8.next = 2;
              return stubbedCreateBankCall();

            case 2:
              expectedResponse = (0, _callHandlers.handleBankResponse)(bankData);
              (0, _checkActionDispatched.default)(dispatchStub, expectedResponse[0]);

            case 4:
            case "end":
              return _context8.stop();
          }
        }
      }, _callee8);
    })));
  });
  describe('updateBank', function () {
    var updatedBank = {
      id: 50,
      title: 'updatedbank'
    };

    var stubbedUpdateBankCall = function stubbedUpdateBankCall() {
      var response = (0, _jsonify.jsoniblePromise)(updatedBank);
      _Fetcher.default.fakeFetch = (0, _fakeFetch.default)(response);
      return (0, _banks2.updateBank)(updatedBank)(dispatchStub, getStateStub);
    };

    it('should hit the right endpoint', function () {
      _Fetcher.default.fakeFetch = fetchStub;
      (0, _banks2.updateBank)(updatedBank)(dispatchStubIgnore, getStateStub);
      expect(fetchStub).to.have.been.calledWith('/api/banks/50');
    });
    it('should trigger additional actions',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee9() {
      var expectedResponse;
      return _regenerator.default.wrap(function _callee9$(_context9) {
        while (1) {
          switch (_context9.prev = _context9.next) {
            case 0:
              _context9.next = 2;
              return stubbedUpdateBankCall();

            case 2:
              expectedResponse = (0, _callHandlers.handleBankResponse)(updatedBank);
              (0, _checkActionDispatched.default)(dispatchStub, expectedResponse[0]);

            case 4:
            case "end":
              return _context9.stop();
          }
        }
      }, _callee9);
    })));
  });
  describe('deleteBank', function () {
    it('should hit the right endpoint', function () {
      _Fetcher.default.fakeFetch = fetchStub;
      (0, _banks2.deleteBank)('5')(dispatchStubIgnore, getStateStub);
      expect(fetchStub).to.have.been.calledWith('/api/banks/5');
    });
    it('should trigger additional actions',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee10() {
      var stubbedDeleteBankCall;
      return _regenerator.default.wrap(function _callee10$(_context10) {
        while (1) {
          switch (_context10.prev = _context10.next) {
            case 0:
              stubbedDeleteBankCall = function stubbedDeleteBankCall() {
                _Fetcher.default.fakeFetch = (0, _fakeFetch.default)((0, _jsonify.jsoniblePromise)());
                return (0, _banks2.deleteBank)('5')(dispatchStub, getStateStub);
              };

              _context10.next = 3;
              return stubbedDeleteBankCall();

            case 3:
              (0, _checkActionDispatched.default)(dispatchStub, (0, _ui.clear)(_constants.BANKS_BANK_PAGINATION));
              (0, _checkActionDispatched.default)(dispatchStub, (0, _banks.archiveBank)('5'));

            case 5:
            case "end":
              return _context10.stop();
          }
        }
      }, _callee10);
    })));
    it('should clear activeBankId and activeBankEntryId if deleting the active bank',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee11() {
      var stubbedDeleteBankCall;
      return _regenerator.default.wrap(function _callee11$(_context11) {
        while (1) {
          switch (_context11.prev = _context11.next) {
            case 0:
              stubbedDeleteBankCall = function stubbedDeleteBankCall() {
                _Fetcher.default.fakeFetch = (0, _fakeFetch.default)((0, _jsonify.jsoniblePromise)());
                return (0, _banks2.deleteBank)('5')(dispatchStub, getStateStub);
              };

              _context11.next = 3;
              return stubbedDeleteBankCall();

            case 3:
              (0, _checkActionDispatched.default)(dispatchStub, (0, _ui.newActiveBank)(void 0));
              (0, _checkActionDispatched.default)(dispatchStub, (0, _ui.newActiveBankEntry)(void 0));

            case 5:
            case "end":
              return _context11.stop();
          }
        }
      }, _callee11);
    })));
    it('should not clear activeBankId and activeBankEntryId if not deleting the active bank',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee12() {
      var stubbedDeleteBankCall;
      return _regenerator.default.wrap(function _callee12$(_context12) {
        while (1) {
          switch (_context12.prev = _context12.next) {
            case 0:
              stubbedDeleteBankCall = function stubbedDeleteBankCall() {
                _Fetcher.default.fakeFetch = (0, _fakeFetch.default)((0, _jsonify.jsoniblePromise)());
                return (0, _banks2.deleteBank)('4')(dispatchStub, getStateStub);
              };

              _context12.next = 3;
              return stubbedDeleteBankCall();

            case 3:
              (0, _checkActionDispatched.checkActionNotDispatched)(dispatchStub, (0, _ui.newActiveBank)(void 0));
              (0, _checkActionDispatched.checkActionNotDispatched)(dispatchStub, (0, _ui.newActiveBankEntry)(void 0));

            case 5:
            case "end":
              return _context12.stop();
          }
        }
      }, _callee12);
    })));
  });
  describe('shareBank', function () {
    var stubbedShareBankCall = function stubbedShareBankCall() {
      var response = (0, _jsonify.jsoniblePromise)({
        id: '1',
        userUuid: '1',
        bankId: '1',
        permission: _constants.CAN_EDIT
      });
      _Fetcher.default.fakeFetch = (0, _fakeFetch.default)(response);
      return (0, _banks2.shareBank)({
        id: '1',
        userUuid: '1',
        bankId: '1',
        permission: _constants.CAN_EDIT
      }, getUserInfoStub)(dispatchStub, getStateStub);
    };

    it('should hit the right endpoint', function () {
      _Fetcher.default.fakeFetch = fetchStub;
      (0, _banks2.shareBank)({
        id: '1',
        userUuid: '1',
        bankId: '2',
        permission: _constants.CAN_EDIT
      }, getUserInfoStub)(dispatchStubIgnore, getStateStub);
      expect(fetchStub).to.have.been.calledWith('/api/banks/2/shared_banks');
    });
    it('should trigger additional actions',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee13() {
      var expectedResponse;
      return _regenerator.default.wrap(function _callee13$(_context13) {
        while (1) {
          switch (_context13.prev = _context13.next) {
            case 0:
              _context13.next = 2;
              return stubbedShareBankCall();

            case 2:
              expectedResponse = (0, _callHandlers.handleShareBankResponse)({
                id: '1',
                userUuid: '1',
                bankId: '1',
                permission: _constants.CAN_EDIT,
                fullName: 'firstFullName',
                avatar: 'http://avatars.com/avatar1'
              });
              (0, _checkActionDispatched.default)(dispatchStub, expectedResponse[0]);

            case 4:
            case "end":
              return _context13.stop();
          }
        }
      }, _callee13);
    })));
  });
  describe('getSharedBanks', function () {
    var stubbedGetSharedBanksCall = function stubbedGetSharedBanksCall(sharedBanksData) {
      var response = (0, _jsonify.jsoniblePromise)(sharedBanksData);
      _Fetcher.default.fakeFetch = (0, _fakeFetch.default)(response);
      return (0, _banks2.getSharedBanks)(bankData, getUserInfoStub)(dispatchStub, getStateStub);
    };

    it('should hit the right endpoint', function () {
      _Fetcher.default.fakeFetch = fetchStub;
      (0, _banks2.getSharedBanks)(bankData, getUserInfoStub)(dispatchStubIgnore, getStateStub);
      expect(fetchStub).to.have.been.calledWith('/api/banks/1/shared_banks');
    });
    it('should trigger additional actions',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee14() {
      var expectedResponse;
      return _regenerator.default.wrap(function _callee14$(_context14) {
        while (1) {
          switch (_context14.prev = _context14.next) {
            case 0:
              _context14.next = 2;
              return stubbedGetSharedBanksCall(sharedBanksData);

            case 2:
              expectedResponse = (0, _callHandlers.handleShareBanksResponse)(sharedBanksDataMerged);
              (0, _checkActionDispatched.default)(dispatchStub, expectedResponse[0]);

            case 4:
            case "end":
              return _context14.stop();
          }
        }
      }, _callee14);
    })));
    it('should dispatch action with the owner when the api returns no data',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee15() {
      var expectedResponse;
      return _regenerator.default.wrap(function _callee15$(_context15) {
        while (1) {
          switch (_context15.prev = _context15.next) {
            case 0:
              _context15.next = 2;
              return stubbedGetSharedBanksCall([]);

            case 2:
              expectedResponse = (0, _callHandlers.handleShareBanksResponse)([{
                id: '-1',
                userUuid: bankData.owner,
                fullName: 'firstFullName',
                avatar: 'http://avatars.com/avatar1',
                permission: _constants.CAN_EDIT,
                bankId: bankData.id
              }]);
              (0, _checkActionDispatched.default)(dispatchStub, expectedResponse[0]);

            case 4:
            case "end":
              return _context15.stop();
          }
        }
      }, _callee15);
    })));
  });
  describe('updateSharedBankPermission', function () {
    it('should hit the right endpoint', function () {
      _Fetcher.default.fakeFetch = fetchStub;
      (0, _banks2.updateSharedBankPermission)('1', '1', _constants.REMOVED_ACCESS)(dispatchStubIgnore, getStateStub);
      expect(fetchStub).to.have.been.calledWith('/api/banks/1/shared_banks/1');
    });
    it('should trigger additional actions',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee16() {
      var stubbedUpdateSharedBankPermissionCall;
      return _regenerator.default.wrap(function _callee16$(_context16) {
        while (1) {
          switch (_context16.prev = _context16.next) {
            case 0:
              stubbedUpdateSharedBankPermissionCall = function stubbedUpdateSharedBankPermissionCall() {
                _Fetcher.default.fakeFetch = (0, _fakeFetch.default)((0, _jsonify.jsoniblePromise)());
                return (0, _banks2.updateSharedBankPermission)('1', '1', _constants.REMOVED_ACCESS)(dispatchStub, getStateStub);
              };

              _context16.next = 3;
              return stubbedUpdateSharedBankPermissionCall();

            case 3:
              (0, _checkActionDispatched.default)(dispatchStub, (0, _ui.clear)(_constants.BANKS_BANK_PAGINATION));

            case 4:
            case "end":
              return _context16.stop();
          }
        }
      }, _callee16);
    })));
  });
});