"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 _Fetcher = _interopRequireDefault(require("../../../common/util/Fetcher"));

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

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

var _tagAssociations = require("../../../common/actions/tagAssociations");

var _tagAssociations2 = require("../tagAssociations");

describe('tag associations api actions', function () {
  var tagAssociation = {
    id: '2',
    tag_id: '2',
    tag_value: 'quiz'
  };
  useFakeFetching();
  var mockStore = (0, _reduxMockStore.default)([_reduxMulti.default, _reduxThunk.default]);
  describe('createTagAssociations', function () {
    var createFetch = (0, _fakeFetch.default)((0, _jsonify.jsoniblePromise)(tagAssociation));
    beforeEach(function () {
      _Fetcher.default.fakeFetch = createFetch;
    });
    afterEach(function () {
      createFetch.resetHistory();
    });
    it('should hit the right endpoint', function () {
      var store = mockStore();
      store.dispatch((0, _tagAssociations2.createTagAssociations)('1', 'Item', '2', ['quiz']));
      expect(createFetch).to.have.been.calledWith('/api/bank_entries/1/tag_associations');
    });
    it('should trigger additional actions',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee() {
      var store, addAction, removePendingAction;
      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, _tagAssociations2.createTagAssociations)('1', 'Item', '2', ['quiz']));

            case 3:
              addAction = (0, _tagAssociations.addTagAssociation)('Item', '2', tagAssociation);
              expect(store.getActions()).to.deep.include(addAction);
              removePendingAction = (0, _tagAssociations.removePendingAdditionTagAssociation)('Item', '2', 'quiz');
              expect(store.getActions()).to.deep.include(removePendingAction);

            case 7:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    })));
  });
  describe('deleteTagAssociations', function () {
    var deleteFetch = (0, _fakeFetch.default)((0, _jsonify.jsoniblePromise)());
    beforeEach(function () {
      _Fetcher.default.fakeFetch = deleteFetch;
    });
    afterEach(function () {
      deleteFetch.resetHistory();
    });
    it('should hit the right endpoint', function () {
      var store = mockStore();
      store.dispatch((0, _tagAssociations2.deleteTagAssociations)('1', 'Item', '2', ['3']));
      expect(deleteFetch).to.have.been.calledWith('/api/bank_entries/1/tag_associations/3');
    });
    it('should trigger additional actions',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee2() {
      var store, removeAction, removePendingAction;
      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, _tagAssociations2.deleteTagAssociations)('1', 'Item', '2', ['3']));

            case 3:
              removeAction = (0, _tagAssociations.removeTagAssociation)('Item', '2', '3');
              expect(store.getActions()).to.deep.include(removeAction);
              removePendingAction = (0, _tagAssociations.removePendingDeletionTagAssociation)('Item', '2', '3');
              expect(store.getActions()).to.deep.include(removePendingAction);

            case 7:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2);
    })));
  });
  describe('getTagSuggestions', function () {
    var getFetch = (0, _fakeFetch.default)((0, _jsonify.jsoniblePromise)([tagAssociation]));
    beforeEach(function () {
      _Fetcher.default.fakeFetch = getFetch;
    });
    afterEach(function () {
      getFetch.resetHistory();
    });
    it('should hit the right endpoint', function () {
      var store = mockStore();
      store.dispatch((0, _tagAssociations2.getTagSuggestions)('Item', '2', 'qu'));
      expect(getFetch).to.have.been.calledWith('/api/tags?filter=qu');
    });
    it('should encode special characters properly', function () {
      var store = mockStore();
      store.dispatch((0, _tagAssociations2.getTagSuggestions)('Item', '2', '?'));
      expect(getFetch).to.have.been.calledWith('/api/tags?filter=%3F');
    });
    it('should trigger additional actions',
    /*#__PURE__*/
    (0, _asyncToGenerator2.default)(
    /*#__PURE__*/
    _regenerator.default.mark(function _callee3() {
      var store, action;
      return _regenerator.default.wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              store = mockStore();
              _context3.next = 3;
              return store.dispatch((0, _tagAssociations2.getTagSuggestions)('Item', '2', 'tags'));

            case 3:
              action = (0, _tagAssociations.setTagSuggestions)('Item', '2', [tagAssociation], 'tags');
              expect(store.getActions()).to.deep.include(action);

            case 5:
            case "end":
              return _context3.stop();
          }
        }
      }, _callee3);
    })));
  });
});