"use strict";

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

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

var _chai = require("chai");

var _withDynamoPagination = _interopRequireDefault(require("../withDynamoPagination"));

describe('withDynamoPagination', function () {
  var Klazz = function Klazz() {
    (0, _classCallCheck2.default)(this, Klazz);
  };

  var PaginatedKlazz = (0, _withDynamoPagination.default)(Klazz);
  it('exports a function', function () {
    (0, _chai.expect)(_withDynamoPagination.default).to.be.a('function');
  });
  it('returns a function', function () {
    (0, _chai.expect)(PaginatedKlazz).to.be.a('function');
  });
  it('returns a function that is bound to the correct prototype chains', function () {
    (0, _chai.expect)(Object.getPrototypeOf(PaginatedKlazz)).to.eq(Klazz);
    (0, _chai.expect)(Object.getPrototypeOf(PaginatedKlazz.prototype)).to.eq(Klazz.prototype);
  });
  it('assigns a default "pass thru" asyncResponseHandler instance method', function () {
    var instance = new PaginatedKlazz();
    var double = {};
    (0, _chai.expect)(instance.asyncResponseHandler(double)).to.eq(double);
  });
  context('paginatedGet', function () {
    it('is an available method', function () {
      var instance = new PaginatedKlazz();
      (0, _chai.expect)(instance.paginatedGet).to.be.a('function');
    });
    it('returns a promise', function () {
      var responseDouble = {
        json: function json() {
          return Promise.resolve({});
        },
        headers: {
          get: function get() {}
        }
      };
      var instance = new PaginatedKlazz();

      instance.get = function () {
        return Promise.resolve(responseDouble);
      };

      instance.dispatch = function () {};

      (0, _chai.expect)(instance.paginatedGet('lol', {
        actionCreator: function actionCreator() {}
      })).to.be.a('promise');
    });
    it('captures async errors for ancillary work and rejects appropriately', function (done) {
      var responseDouble = {
        json: function json() {
          return Promise.resolve({});
        },
        headers: {
          get: function get() {}
        }
      };
      var instance = new PaginatedKlazz();

      instance.get = function () {
        return Promise.resolve(responseDouble);
      };

      instance.dispatch = sinon.stub().throws(new Error('oops!'));
      instance.paginatedGet('zomg', {
        actionCreator: function actionCreator() {}
      }).catch(function (err) {
        (0, _chai.expect)(instance.dispatch.callCount).to.eq(1);
        (0, _chai.expect)(err.message).to.eq('oops!');
        done();
      });
    });
    it('fetches so long as pagination headers are available, resolves with responses', function (done) {
      var get = sinon.stub();
      get.withArgs('X-Last-Evaluated-Hash-Key').onFirstCall().returns('lol').onSecondCall().returns('wut');
      get.withArgs('X-Last-Evaluated-Range-Key').onFirstCall().returns('lol').onSecondCall().returns('wut');
      var responseDouble = {
        json: function json() {
          return Promise.resolve({});
        },
        headers: {
          get: get
        }
      };
      var instance = new PaginatedKlazz();

      instance.get = function () {
        return Promise.resolve(responseDouble);
      };

      instance.dispatch = sinon.stub();
      instance.paginatedGet('zomg', {
        actionCreator: function actionCreator() {}
      }).then(function (result) {
        (0, _chai.expect)(result).to.eql([responseDouble, responseDouble, responseDouble]);
        (0, _chai.expect)(get.callCount).to.eq(6);
        (0, _chai.expect)(instance.dispatch.callCount).to.eq(3);
        done();
      });
    });
  });
});