"use strict";

var _qunitjs = require("qunitjs");

var QUnit = _interopRequireWildcard(_qunitjs);

var _ramda = require("ramda");

var _rx = require("rx");

var Rx = _interopRequireWildcard(_rx);

var _utils = require("../src/utils");

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

var $ = Rx.Observable;

QUnit.module("utils");

QUnit.test("makeFunctionDecorator :: ({ before, after, name }) : o after, x before, o name", function exec_test(assert) {

  function tapEventSource(eventName) {
    return function (result) {
      return result.tap(function (_) {
        testArr.push("value: " + _);
        return testArr;
      });
    };
  }

  function tapStreamOutput(eventName) {
    return (0, _utils.makeFunctionDecorator)({ after: tapEventSource(eventName), name: 'tapStreamOutput' });
  }

  var decoratorFn = tapStreamOutput('testEv');
  var testArr = [];
  var fnToDecorate = function testFn(param) {
    return $.just(param);
  };

  assert.ok(typeof decoratorFn === 'function', "makeFunctionDecorator returns a function");
  var args = ['params'];
  var result = decoratorFn(args, 'testFn', fnToDecorate);
  result.subscribe(function (value) {
    assert.deepEqual("" + args[0], value, "the decorated function is executed!");
  });
  assert.deepEqual(testArr, ["value: " + args[0]], "the (after) decorating function is executed on the result of the decorated function!");
});

QUnit.test("makeFunctionDecorator :: ({ before, after, name }) : x after, o before, o name", function exec_test(assert) {

  var log = (0, _ramda.curry)(function log(specs, args, fnToDecorateName, fnToDecorate) {
    specs.forEach(function (x, i) {
      testArr.push(x + " : " + [args, fnToDecorateName, fnToDecorate][i]);
    });
  });
  var specs = ['args', 'fnToDecorateName', 'fnToDecorate'];

  var decoratorFn = (0, _utils.makeFunctionDecorator)({ before: log(specs), name: 'log' });
  var testArr = [];
  var fnToDecorate = function testFn(param) {
    return $.just(param);
  };

  assert.ok(typeof decoratorFn === 'function', "makeFunctionDecorator returns a function");
  var args = ['params'];
  var result = decoratorFn(args, 'testFn', fnToDecorate);
  result.subscribe(function (value) {
    assert.deepEqual("" + args[0], value, "the decorated function is executed!");
  });
  assert.deepEqual(testArr, ["args : params", "fnToDecorateName : testFn", "fnToDecorate : function testFn(param) {\n    return $.just(param);\n  }"], "the (after) decorating function is executed on the result of the decorated function!");
});

QUnit.test("decorateWithOne:: decoratingFnSpecs, fnToDecorate", function exec_test(assert) {
  var log = (0, _ramda.curry)(function log(specs, args, fnToDecorateName, fnToDecorate) {
    specs.forEach(function (x, i) {
      testArr.push(x + " : " + [args, fnToDecorateName, fnToDecorate][i]);
    });
  });
  var specs = ['param'];
  var eventName = 'testEv';

  function tapEventSource(eventName) {
    return function (result, fnToDecorateName, fnToDecorate) {
      return result.tap(function (_) {
        testArr.push("value: " + _);
      });
    };
  }

  function tapStreamOutput(eventName) {
    return (0, _utils.makeFunctionDecorator)({ after: tapEventSource(eventName), name: 'tapStreamOutput' });
  }

  var testArr = [];
  var fnToDecorate = function testFn(param) {
    return $.just(param + '!');
  };
  var decoratedFn = (0, _utils.decorateWithOne)({
    before: log(specs),
    after: tapEventSource(eventName)
  }, fnToDecorate);

  assert.ok(typeof decoratedFn === 'function', "decorateWithOne returns a function");
  var result = decoratedFn('paramTest');
  result.subscribe(function (value) {
    assert.deepEqual("paramTest!", value, "the decorated function is executed!");
  });
  assert.deepEqual(testArr, ["param : paramTest", "value: paramTest!"], "the decorated function executes normally together with its decoration");
});

QUnit.test("decorateWith:: [decoratingFnSpecs], fnToDecorate", function exec_test(assert) {
  var log = (0, _ramda.curry)(function log(specs, args, fnToDecorateName, fnToDecorate) {
    specs.forEach(function (x, i) {
      testArr.push(x + " : " + [args, fnToDecorateName, fnToDecorate][i]);
    });
  });
  var specs = ['param'];
  var eventName = 'testEv';

  function tapEventSource(eventName) {
    return function (result) {
      return result.tap(function (_) {
        testArr.push("value: " + _);
      });
    };
  }

  function tapStreamOutput(eventName) {
    return (0, _utils.makeFunctionDecorator)({ after: tapEventSource(eventName), name: 'tapStreamOutput' });
  }

  var testArr = [];
  var fnToDecorate = function testFn(param) {
    return $.just(param + '!');
  };
  var decoratedFn = (0, _utils.decorateWith)([{ before: log(specs) }, { after: tapEventSource(eventName) }], fnToDecorate);

  assert.ok(typeof decoratedFn === 'function', "decorateWithOne returns a function");
  var result = decoratedFn('paramTest');
  result.subscribe(function (value) {
    assert.deepEqual("paramTest!", value, "the decorated function is executed!");
  });
  assert.deepEqual(testArr, ["param : paramTest", "value: paramTest!"], "the decorated function executes normally together with its decoration");
});

//# sourceMappingURL=utils.specs.js.map