"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 _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

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

var _ExhaustivePaginatedFetcher = _interopRequireDefault(require("../ExhaustivePaginatedFetcher"));

var FakeFetcher =
/*#__PURE__*/
function () {
  function FakeFetcher() {
    var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
        _ref$perPage = _ref.perPage,
        perPage = _ref$perPage === void 0 ? '5' : _ref$perPage,
        _ref$total = _ref.total,
        total = _ref$total === void 0 ? '20' : _ref$total,
        _ref$onePage = _ref.onePage,
        onePage = _ref$onePage === void 0 ? [0, 1, 2, 3, 4] : _ref$onePage,
        _ref$getStub = _ref.getStub,
        getStub = _ref$getStub === void 0 ? sinon.stub() : _ref$getStub;

    (0, _classCallCheck2.default)(this, FakeFetcher);
    this.onePage = onePage;
    this.total = total;
    this.perPage = perPage;
    this.getStub = getStub.resolves(this.onePage);
  }

  (0, _createClass2.default)(FakeFetcher, [{
    key: "get",
    value: function get(url, params) {
      return this.getStub.apply(null, arguments);
    }
  }, {
    key: "fetch",
    value: function fetch() {
      var _this = this;

      return Promise.resolve({
        headers: {
          get: function get(type) {
            return type === 'total' ? _this.total : _this.perPage;
          }
        },
        json: function json() {
          return Promise.resolve(_this.onePage);
        }
      });
    }
  }, {
    key: "callHeaders",
    value: function callHeaders() {}
  }, {
    key: "appendQueryParams",
    value: function appendQueryParams(url) {
      return url;
    }
  }]);
  return FakeFetcher;
}();

describe('ExhaustivePaginatedFetcher', function () {
  it('combines pages', function () {
    var fetcher = new _ExhaustivePaginatedFetcher.default();
    var pages = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
    var result = fetcher.combinePages(pages);
    var shouldBe = [1, 2, 3, 4, 5, 6, 7, 8, 9];
    expect(result).to.be.an('array').that.is.not.empty();
    expect(result).to.eql(shouldBe);
  });
  it('fetch can use mock superclass',
  /*#__PURE__*/
  (0, _asyncToGenerator2.default)(
  /*#__PURE__*/
  _regenerator.default.mark(function _callee() {
    var fetcher, res;
    return _regenerator.default.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            fetcher = new _ExhaustivePaginatedFetcher.default({
              SuperClass: FakeFetcher
            });
            _context.next = 3;
            return fetcher.fetch();

          case 3:
            res = _context.sent;
            expect(res.headers.get('total')).to.equal('20');

          case 5:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  })));
  it('uses returned page size on future requests',
  /*#__PURE__*/
  (0, _asyncToGenerator2.default)(
  /*#__PURE__*/
  _regenerator.default.mark(function _callee2() {
    var getStub, fetcher;
    return _regenerator.default.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            getStub = sinon.stub();
            fetcher = new _ExhaustivePaginatedFetcher.default({
              SuperClass: FakeFetcher,
              total: '10',
              getStub: getStub
            });
            _context2.next = 4;
            return fetcher.get('/foo', {
              per_page: 1000
            });

          case 4:
            // calls fetch for first stub, then get for subsequent, so only expect one get call
            expect(getStub).to.have.been.calledOnce();
            expect(getStub.args[0][1]).to.include({
              per_page: 5
            });

          case 6:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  })));
  it('exhausts all pages from a get request',
  /*#__PURE__*/
  (0, _asyncToGenerator2.default)(
  /*#__PURE__*/
  _regenerator.default.mark(function _callee3() {
    var fetcher, res;
    return _regenerator.default.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            fetcher = new _ExhaustivePaginatedFetcher.default({
              SuperClass: FakeFetcher
            });
            _context3.next = 3;
            return fetcher.get();

          case 3:
            res = _context3.sent;
            expect(res).to.eql([0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4]);

          case 5:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  })));
  it('handles responses of 1 page',
  /*#__PURE__*/
  (0, _asyncToGenerator2.default)(
  /*#__PURE__*/
  _regenerator.default.mark(function _callee4() {
    var fetcher, res;
    return _regenerator.default.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            fetcher = new _ExhaustivePaginatedFetcher.default({
              SuperClass: FakeFetcher,
              perPage: '10',
              total: '5',
              onePage: [0, 1, 2, 3, 4]
            });
            _context4.next = 3;
            return fetcher.get();

          case 3:
            res = _context4.sent;
            expect(res).to.eql([0, 1, 2, 3, 4]);

          case 5:
          case "end":
            return _context4.stop();
        }
      }
    }, _callee4);
  })));
  it('handles empty response',
  /*#__PURE__*/
  (0, _asyncToGenerator2.default)(
  /*#__PURE__*/
  _regenerator.default.mark(function _callee5() {
    var fetcher, res;
    return _regenerator.default.wrap(function _callee5$(_context5) {
      while (1) {
        switch (_context5.prev = _context5.next) {
          case 0:
            fetcher = new _ExhaustivePaginatedFetcher.default({
              SuperClass: FakeFetcher,
              perPage: '10',
              total: '0',
              onePage: []
            });
            _context5.next = 3;
            return fetcher.get();

          case 3:
            res = _context5.sent;
            expect(res).to.eql([]);

          case 5:
          case "end":
            return _context5.stop();
        }
      }
    }, _callee5);
  })));
});