All files / src test-utils.js

100% Statements 6/6
100% Branches 0/0
100% Functions 3/3
100% Lines 6/6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47                  1x 23x                                                   1x 3x     23x 23x          
/**
 * Testing utils
 *
 * @namespace TestUtils
 *
 */
 
import { path } from 'ramda';
 
export const resumeIteratorAndCheckOutput = (t, iterator) => (into, outof) => {
  let value;
  if (!iterator) {
    throw 'iterator is undefined';
  }
 
  try {
    value = iterator.next(into).value;
    t.deepEqual(value, outof);
  } catch (e) {
    t.deepEqual(e.message, outof.error.message);
  }
};
 
/**
 * checks generator scenario
 *
 * this is equivalent to:
 * asserting that two iterators are equal, when given the same input
 *
 * @param {NodeTap} t node-tap instance
 * @param {Iterator} iter
 * @param {[]} scenario Scenario data
 * @returns {void}
 *
 * @memberof TestUtils
 */
export const checkGeneratorScenario = (t, iter, scenario) => {
  const next = resumeIteratorAndCheckOutput(t, iter);
 
  for (let i = 0; i < scenario.length - 1; i += 2) {
    const input = scenario[i];
    const output = scenario[i + 1];
    next(input, output);
  }
};