"use strict";

var _tape = _interopRequireDefault(require("tape"));

var _promiseTimeout = require("./promise-timeout");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

//@ts-check
const getPromise = (wait, shouldResolve = false, resolvedValue) => new Promise((resolve, reject) => {
  setTimeout(() => {
    shouldResolve ? resolve(resolvedValue) : reject();
  }, wait);
});

(0, _tape.default)('promiseTimeout times out and resolves without error', t => {
  const slowPromise = getPromise(1000);
  const now = Date.now();
  (0, _promiseTimeout.promiseTimeout)({
    promise: slowPromise,
    shouldReject: false,
    timeout: 200
  }).then(() => {
    const duration = Date.now() - now;
    t.ok(duration >= 200 && duration <= 300, 'should resolve without error sooner than 1 second');
    t.end();
  }).catch(() => {
    t.fail();
  });
});
(0, _tape.default)('promiseTimeout lets original promise through if within timeout', t => {
  const originalPromise = getPromise(200, true, 'something');
  const now = Date.now();
  (0, _promiseTimeout.promiseTimeout)({
    promise: originalPromise,
    shouldReject: false,
    timeout: 500
  }).then(result => {
    const duration = Date.now() - now;
    t.ok(duration >= 200 && duration <= 300, 'should resolve without error with expected value');
    t.equal(result, 'something');
    t.end();
  }).catch(() => {
    t.fail();
  });
});