"use strict";

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

var _reduxBundler = require("redux-bundler");

var _getRecentActionsBundle = _interopRequireDefault(require("./get-recent-actions-bundle"));

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

//@ts-check

/* eslint-disable no-undef */
const getStringOfLength = length => {
  let buff = '';

  while (length--) {
    buff += 'a';
  }

  return buff;
};

const getStore = opts => (0, _reduxBundler.composeBundlesRaw)((0, _getRecentActionsBundle.default)(opts), {
  name: 'stubs',
  selectStubs: state => state.stubs,
  reducer: (state = {}) => state
})();

(0, _tape.default)('getRecentActionsBundle keeps right actions actions', async t => {
  const store = getStore({
    recentActionsToTrack: 3
  });
  let count = 0;

  while (count++ < 20) {
    store.dispatch({
      type: 'SOME_ACTION',
      payload: count
    });
  }

  let otherCount = 0;

  while (otherCount++ < 50) {
    store.dispatch({
      type: 'APP_IDLE'
    });
  }

  t.deepEqual(store.getRecentActions(), [{
    type: 'SOME_ACTION',
    payload: 18
  }, {
    type: 'SOME_ACTION',
    payload: 19
  }, {
    type: 'SOME_ACTION',
    payload: 20
  }], 'only keeps last 3');
  t.end();
});
(0, _tape.default)('getRecentActionsBundle trims to size so we never end up with too many', async t => {
  const store = getStore({
    recentActionsToTrack: 5
  });
  let count = 0;

  while (count++ < 20) {
    store.dispatch({
      type: 'SOME_ACTION',
      payload: count === 19 ? getStringOfLength(50000) : count
    });
  }

  let otherCount = 0;

  while (otherCount++ < 50) {
    store.dispatch({
      type: 'APP_IDLE'
    });
  }

  t.deepEqual(store.getRecentActions(), [{
    type: 'SOME_ACTION',
    payload: 16
  }, {
    type: 'SOME_ACTION',
    payload: 17
  }, {
    type: 'SOME_ACTION',
    payload: 18
  }, {
    type: 'SOME_ACTION',
    payload: '_LONG_'
  }, {
    type: 'SOME_ACTION',
    payload: 20
  }], 'trims size to fit');
  t.end();
});
(0, _tape.default)('getRecentActionsBundle does not error when not serializable', async t => {
  const store = getStore({
    maxByteSize: 75
  });
  let count = 0;
  const aPayload = {
    blah: 'hi'
  };

  while (count++ < 20) {
    aPayload.payload = aPayload;
    store.dispatch({
      type: 'SOME_ACTION',
      payload: aPayload
    });
  }

  t.deepEqual(store.getRecentActions(), 'error serializing recent actions', 'returns error message instead of throwing error if cannot serialize');
  t.end();
});
(0, _tape.default)('getRecentActionsBundle does not require options', t => {
  t.doesNotThrow(() => (0, _getRecentActionsBundle.default)(undefined));
  t.end();
});
(0, _tape.default)('getRecentActionsBundle trims longest payloads', t => {
  const store = getStore({
    maxByteSize: 500
  });
  store.dispatch({
    type: 'SOME_ACTION',
    payload: getStringOfLength(200)
  });
  store.dispatch({
    type: 'SOME_ACTION',
    payload: getStringOfLength(40)
  });
  store.dispatch({
    type: 'SOME_ACTION',
    payload: getStringOfLength(200)
  });
  t.deepEqual(store.getRecentActions(), [{
    type: 'SOME_ACTION',
    payload: '_LONG_'
  }, {
    type: 'SOME_ACTION',
    payload: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
  }, {
    type: 'SOME_ACTION',
    payload: '_LONG_'
  }], 'trims size to fit, so we only ed up with 2');
  t.end();
});