"use strict";

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

var _throttle = require("./throttle");

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

(0, _tape.default)('throttle with leading', t => {
  let count = 0;
  const start = Date.now();

  const callee = () => {
    count++;
    const timePassed = Date.now() - start;

    if (count === 1) {
      t.ok(timePassed < 100, 'no time passed');
    }

    if (count === 2) {
      t.ok(timePassed > 450 && timePassed < 700, 'roughly 500ms passed. actual: ' + timePassed);
    }

    if (count === 3) {
      t.ok(timePassed > 950 && timePassed < 1300, 'roughly 1000ms passed. actual: ' + timePassed);
    }

    if (count > 3) {
      t.fail();
    }
  };

  const throttled = (0, _throttle.throttle)(callee, 500, true); // once to test immediate invocation

  throttled();
  let throttleCalls = 0;
  const int = setInterval(() => {
    throttled();
    throttleCalls++;
  }, 20); // call once to make up for the fact that interval
  // waits before starting

  throttled();
  setTimeout(() => {
    clearInterval(int);
    setTimeout(() => {
      t.ok(throttleCalls > 30, 'throttled function should have been called a lot');
      t.end();
    }, 1000);
  }, 1000);
});
(0, _tape.default)('throttle with leading calls with latest args once we stop calling', t => {
  let count = 0;
  const start = Date.now();

  const callee = callNumber => {
    count++;
    const timePassed = Date.now() - start;

    if (count === 1) {
      t.ok(timePassed < 50, 'no time passed');
    }

    if (count === 2) {
      t.ok(timePassed > 450 && timePassed < 600, 'roughly 500ms passed. actual: ' + timePassed);
    }

    if (count === 3) {
      t.ok(callNumber > 20 && callNumber < 30, 'gets latest argument');
      t.ok(timePassed > 900 && timePassed < 1100, 'roughly 1000ms passed. actual: ' + timePassed);
    }

    if (count > 3) {
      t.fail();
    }
  };

  const throttled = (0, _throttle.throttle)(callee, 500, true); // helper to keep track of calls

  let throttleCalls = 0;

  const callIt = () => throttled(++throttleCalls); // once to test immediate invocation


  callIt();
  const int = setInterval(callIt, 20); // call once to make up for the fact that interval
  // waits before starting

  callIt();
  setTimeout(() => {
    clearInterval(int);
    setTimeout(() => {
      t.ok(throttleCalls > 30, 'throttled function should have been called a lot');
      t.end();
    }, 1000);
  }, 1000);
});
(0, _tape.default)('throttle without leading', t => {
  let count = 0;
  const start = Date.now();

  const callee = () => {
    count++;
    const timePassed = Date.now() - start;

    if (count === 1) {
      t.ok(timePassed => 500 && timePassed < 600, 'roughly 500ms passed for first call. actual was: ' + timePassed);
    }

    if (count === 2) {
      t.ok(timePassed => 1000 && timePassed < 1100, 'roughly 1000ms passed for second call');
    }

    if (count > 3) {
      t.fail();
    }
  };

  const throttled = (0, _throttle.throttle)(callee, 500); // once to test immediate invocation

  throttled();
  let throttleCalls = 0;
  const int = setInterval(() => {
    throttled();
    throttleCalls++;
  }, 20); // call once to make up for the fact that interval
  // waits before starting

  throttled();
  setTimeout(() => {
    clearInterval(int);
    setTimeout(() => {
      t.ok(throttleCalls > 30, 'throttled function should have been called a lot');
      t.end();
    }, 1000);
  }, 1000);
});