"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 _reduxMockStore = _interopRequireDefault(require("redux-mock-store"));

var _reduxThunk = _interopRequireDefault(require("redux-thunk"));

var _reduxMulti = _interopRequireDefault(require("redux-multi"));

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

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

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

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

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

var _BankEntry = _interopRequireDefault(require("../../../common/records/BankEntry"));

var _Item = _interopRequireDefault(require("../../../common/records/Item"));

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

var _bankEntries = require("../bankEntries");

describe('bank entries api actions', function () {
  var bankEntryData = {
    id: 1,
    entry_type: 'Item',
    entry: {
      id: 2,
      itemBody: 'fjkasndgkjs'
    }
  };
  var bankEntryStimulusData = {
    id: 2,
    entry_type: 'Stimulus',
    entry: {
      id: 3
    }
  };
  var bankEntriesData = [bankEntryData];
  var bankSearchData = {
    entries: bankEntriesData,
    filters: {
      some: 'filters'
    }
  };
  var fetchBankEntriesStub = (0, _fakeFetch.default)((0, _jsonify.jsoniblePromise)(bankSearchData));
  var fetchBankEntryStub = (0, _fakeFetch.default)((0, _jsonify.jsoniblePromise)(bankEntryData));
  var fetchBankEntryStimulusStub = (0, _fakeFetch.default)((0, _jsonify.jsoniblePromise)(bankEntryStimulusData));
  useFakeFetching();
  var mockStore = (0, _reduxMockStore.default)([_reduxMulti.default, _reduxThunk.default]);
  afterEach(function () {
    fetchBankEntriesStub.resetHistory();
    fetchBankEntryStub.resetHistory();
    fetchBankEntryStimulusStub.resetHistory();
  });
  describe('getBankEntries', function () {
    beforeEach(function () {
      _Fetcher.default.fakeFetch = fetchBankEntriesStub;
    });
    it('should hit the right endpoint', function () {
      var store = mockStore();
      store.dispatch((0, _bankEntries.getBankEntries)(123, true));
      expect(fetchBankEntriesStub).to.have.been.calledWithMatch('/api/banks/123/bank_entries/search');
    });
    it('should trigger additional actions', function (done) {
      var store = mockStore();
      store.dispatch((0, _bankEntries.getBankEntries)(123, true)).then(function () {
        var expectedFilter = (0, _banks.setBankFilters)(123, bankSearchData.filters);
        var expectedEntries = (0, _callHandlers.handleBankEntriesResponse)(bankEntriesData);
        expect(store.getActions()).to.deep.include(expectedEntries[0]);
        expect(store.getActions()).to.deep.include(expectedFilter);
      }).then(done, done);
    });
    it('does not update filters with search results',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee() {
      var store, filter, actions;
      return _regenerator.default.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              store = mockStore();
              _context.next = 3;
              return store.dispatch((0, _bankEntries.getBankEntries)(123, true, {
                text: 'example'
              }));

            case 3:
              filter = (0, _banks.setBankFilters)(123, bankSearchData.filters);
              actions = store.getActions().filter(function (a) {
                return a.type === filter.type;
              });
              expect(actions.length).to.equal(0);

            case 6:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    })));
  });
  describe('createBankEntry', function () {
    var bankId = '1';
    var entryType = 'Item';
    var entryId = '1';
    beforeEach(function () {
      _Fetcher.default.fakeFetch = fetchBankEntryStub;
    });
    it('should hit the right endpoint', function () {
      var store = mockStore();
      store.dispatch((0, _bankEntries.createBankEntry)(bankId, entryType, entryId));
      expect(fetchBankEntryStub).to.have.been.calledWith('/api/banks/1/bank_entries');
    });
    it('should trigger additional actions', function (done) {
      var store = mockStore();
      store.dispatch((0, _bankEntries.createBankEntry)(bankId, entryType, entryId)).then(function () {
        var expectedResponse = (0, _callHandlers.handleBankEntryResponse)(bankEntryData);
        expect(store.getActions()).to.deep.include(expectedResponse[0]);
      }).then(done, done);
    });
    it('should fetch the affected bank', function (done) {
      var store = mockStore();
      store.dispatch((0, _bankEntries.createBankEntry)(bankId, entryType, entryId)).then(function () {
        expect(fetchBankEntryStub).to.have.been.calledWith('/api/banks/1');
      }).then(done, done);
    });
  });
  describe('createEntryAndBankEntry', function () {
    var bankId = '7';
    var bankEntry = new _BankEntry.default({
      id: '22',
      entryType: 'Item'
    });
    var dispatchItemStub = sinon.stub().resolves(fakeItemData);
    var stubbedBankEntryCreation = sinon.stub();
    var stubbedItemCreation = sinon.stub();
    var fakeItemData = {
      id: 'foo'
    };

    var fakeCreateEntryAndBankEntry = function fakeCreateEntryAndBankEntry() {
      var response = (0, _jsonify.jsoniblePromise)(bankEntry);
      var fakeFetch = (0, _fakeFetch.default)(response);
      _Fetcher.default.fakeFetch = fakeFetch;
      var createEntryAndBankEntryThunk = (0, _bankEntries.createEntryAndBankEntry)(bankId, fakeItemData, bankEntry, stubbedItemCreation, stubbedBankEntryCreation);
      return createEntryAndBankEntryThunk(dispatchItemStub);
    };

    it('calls createItem with the right argument', function () {
      var fetchPromise = fakeCreateEntryAndBankEntry();
      fetchPromise.then(function () {
        expect(stubbedItemCreation.calledWith(fakeItemData)).to.be.true();
      });
    });
    it('calls createBankEntry with the right arguments', function () {
      var fetchPromise = fakeCreateEntryAndBankEntry();
      fetchPromise.then(function () {
        expect(stubbedBankEntryCreation.getCall(0).args[0]).to.eq('7');
        expect(stubbedBankEntryCreation.getCall(0).args[1]).to.eq('Item');
        expect(stubbedBankEntryCreation.getCall(0).args[2]).to.eq('foo');
      });
    });
  });
  describe('updateBankEntry', function () {
    var bankEntryId = '1';
    var bankId = '5';
    beforeEach(function () {
      _Fetcher.default.fakeFetch = fetchBankEntryStub;
    });
    it('should hit the right endpoint', function () {
      var store = mockStore();
      store.dispatch((0, _bankEntries.updateBankEntry)(bankId, bankEntryId, {
        bankId: '5',
        entryId: '12'
      }));
      expect(fetchBankEntryStub).to.have.been.calledWith('/api/banks/5/bank_entries/1', sinon.match({
        body: JSON.stringify({
          bank_entry: {
            bank_id: '5',
            entry_id: '12'
          }
        })
      }));
    });
    it('should trigger additional actions', function (done) {
      var store = mockStore();
      store.dispatch((0, _bankEntries.updateBankEntry)(bankId, bankEntryId, {
        bankId: '5',
        entryId: '12'
      })).then(function () {
        var expectedResponse = (0, _callHandlers.handleBankEntryResponse)(bankEntryData);
        expect(store.getActions()).to.deep.include(expectedResponse[0]);
        expect(store.getActions()).not.to.deep.include((0, _ui.clear)('BANKS_BANK_ENTRY_PAGINATION:5'));
        expect(store.getActions()).not.to.deep.include((0, _ui.clear)('BANKS_BANK_ENTRY_PAGINATION:6'));
        expect(fetchBankEntryStub).not.to.have.been.calledWith('/api/banks/5');
        expect(fetchBankEntryStub).not.to.have.been.calledWith('/api/banks/6');
      }).then(done, done);
    });
    it('clears pagination state for the banks if entry was moved', function (done) {
      var store = mockStore();
      store.dispatch((0, _bankEntries.updateBankEntry)(bankId, bankEntryId, {
        bankId: '6'
      })).then(function () {
        expect(store.getActions()).to.deep.include((0, _ui.clear)('BANKS_BANK_ENTRY_PAGINATION:5'));
        expect(store.getActions()).to.deep.include((0, _ui.clear)('BANKS_BANK_ENTRY_PAGINATION:6'));
      }).then(done, done);
    });
    it('should fetch the affected banks if entry was moved', function (done) {
      var store = mockStore();
      store.dispatch((0, _bankEntries.updateBankEntry)(bankId, bankEntryId, {
        bankId: '6'
      })).then(function () {
        expect(fetchBankEntryStub).to.have.been.calledWith('/api/banks/5');
        expect(fetchBankEntryStub).to.have.been.calledWith('/api/banks/6');
      }).then(done, done);
    });
  });
  describe('cloneBankEntry', function () {
    var bankEntry = new _BankEntry.default({
      id: '5',
      bankId: '1',
      entryId: '1',
      entryType: 'Item'
    });
    var item = new _Item.default();
    sinon.stub(bankEntry, 'getEntry').returns(item);
    sinon.stub(item, 'getWorkingInstance').returns(item);
    var stubbedItemCreation = sinon.stub();
    var stubbedBankEntryUpdate = sinon.stub();
    var dispatchCreateItemStub = sinon.stub().resolves({
      id: '2'
    });

    var stubbedCloneBankEntryCall = function stubbedCloneBankEntryCall() {
      var response = (0, _jsonify.jsoniblePromise)(bankEntryData);
      _Fetcher.default.fakeFetch = (0, _fakeFetch.default)(response);
      var cloneBankEntruThunk = (0, _bankEntries.cloneBankEntry)(bankEntry, stubbedItemCreation, stubbedBankEntryUpdate);
      return cloneBankEntruThunk(dispatchCreateItemStub);
    };

    it('should hit the right endpoint', function (done) {
      var fetchPromise = stubbedCloneBankEntryCall();
      fetchPromise.then(function () {
        expect(stubbedItemCreation.called).to.be.true();
        done();
      });
    });
    it('should trigger additional actions', function (done) {
      var fetchPromise = stubbedCloneBankEntryCall();
      fetchPromise.then(function () {
        expect(stubbedBankEntryUpdate.getCall(0).args[0]).to.eq('1');
        expect(stubbedBankEntryUpdate.getCall(0).args[1]).to.eq('5');
        expect(stubbedBankEntryUpdate.getCall(0).args[2]).to.deep.equal({
          entryId: '2'
        });
        done();
      });
    });
  });
  describe('getBankEntry', function () {
    var bankEntryId = '1';
    var bankId = '5';
    beforeEach(function () {
      _Fetcher.default.fakeFetch = fetchBankEntryStub;
    });
    it('should hit the right endpoint', function () {
      var store = mockStore();
      store.dispatch((0, _bankEntries.getBankEntry)(bankId, bankEntryId));
      expect(fetchBankEntryStub).to.have.been.calledWith('/api/banks/5/bank_entries/1');
    });
    it('should return bank entry data',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee2() {
      var store, response;
      return _regenerator.default.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              store = mockStore();
              _context2.next = 3;
              return store.dispatch((0, _bankEntries.getBankEntry)(bankId, bankEntryId));

            case 3:
              response = _context2.sent;
              expect(response).to.eq(bankEntryData);

            case 5:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2);
    })));
    it('should trigger additional actions', function (done) {
      var store = mockStore();
      store.dispatch((0, _bankEntries.getBankEntry)(bankId, bankEntryId)).then(function () {
        var expectedResponse = (0, _callHandlers.handleBankEntryResponse)(bankEntryData);
        expect(store.getActions()).to.deep.include(expectedResponse[0]);
      }).then(done, done);
    });
  });
  describe('removeBankEntry', function () {
    var bankEntryId = '1';
    var bankId = '5';
    beforeEach(function () {
      _Fetcher.default.fakeFetch = fetchBankEntryStub;
    });
    it('should hit the right endpoint', function () {
      var store = mockStore();
      store.dispatch((0, _bankEntries.removeBankEntry)(bankId, bankEntryId));
      expect(fetchBankEntryStub).to.have.been.calledWith('/api/banks/5/bank_entries/1');
    });
    it('should fetch the affected bank', function (done) {
      var store = mockStore();
      store.dispatch((0, _bankEntries.removeBankEntry)(bankId, bankEntryId)).then(function () {
        expect(fetchBankEntryStub).to.have.been.calledWith('/api/banks/5');
        var actions = store.getActions();
        expect(actions.map(function (a) {
          return {
            type: a.type
          };
        })).to.deep.include({
          type: 'ADD_BANKS'
        });
      }).then(done, done);
    });
  });
});