"use strict";

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

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

var _waitFor = require("./wait-for");

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

(0, _tape.default)('waitFor helper works when condition is met', t => {
  const store = (0, _reduxBundler.composeBundlesRaw)({
    name: 'test',
    reducer: (state = false, {
      type
    }) => {
      if (type === 'ON') {
        return true;
      }

      return state;
    },
    selectTestState: state => state.test
  })();
  t.equal(store.selectTestState(), false, 'should start out false');
  (0, _waitFor.waitFor)(store, 'selectTestState').then(() => {
    t.deepEqual(store.subscriptions.watchedSelectors, {}, 'should be empty');
    t.ok('waited for condition');
    t.end();
  });
  t.deepEqual(store.subscriptions.watchedSelectors, {
    selectTestState: 1
  }, 'store should have a watcher for that value');
  store.dispatch({
    type: 'ON'
  });
});
(0, _tape.default)('waitFor helper works when condition is already met at beginning', t => {
  const store = (0, _reduxBundler.composeBundlesRaw)({
    name: 'test',
    reducer: (state = false, {
      type
    }) => {
      if (type === 'ON') {
        return true;
      }

      return state;
    },
    selectTestState: state => state.test
  })(); // set to true

  store.dispatch({
    type: 'ON'
  });
  t.equal(store.selectTestState(), true, 'should start out true');
  (0, _waitFor.waitFor)(store, 'selectTestState').then(() => {
    t.deepEqual(store.subscriptions.watchedSelectors, {}, 'no subscriptions should have been created');
    t.ok('waited for condition');
    t.end();
  });
});
(0, _tape.default)('waitFor helper works with timeout', t => {
  t.plan(3);
  const store = (0, _reduxBundler.composeBundlesRaw)({
    name: 'test',
    reducer: (state = false, {
      type
    }) => {
      if (type === 'ON') {
        return true;
      }

      return state;
    },
    selectTestState: state => state.test
  })();
  t.equal(store.selectTestState(), false, 'should start out false');
  (0, _waitFor.waitFor)(store, 'selectTestState', {
    timeout: 100
  }).then(() => {
    t.fail('should not ever resolve');
  }).catch(err => {
    t.deepEqual(store.subscriptions.watchedSelectors, {}, 'subscriptions should have been cleared');
    t.equal(err.message, 'TIMED_OUT', 'should have timed out');
  }); // set it to true 100ms later, it should not do anything to the above

  setTimeout(() => {
    store.dispatch({
      type: 'ON'
    });
    t.end();
  }, 200);
});