'use strict';

var _assert = require('assert');

var assert = _interopRequireWildcard(_assert);

var _src = require('../src');

var _mockDOM = require('../src/utils/mockDOM');

var _most = require('most');

var $ = _interopRequireWildcard(_most);

var _mostSubject = require('most-subject');

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; } }

function plan(n) {
  return function _done(done) {
    if (--n === 0) {
      done();
    }
  };
}

describe("When inputs are simulating regular stream behaviour", function () {
  it('emits the inputs in increasing order of (i,j), where : \n   - i is the (row) index of the source in the input array\n   - j is the (column) index of the emitted input value in the source diagram\n   - (2,1) < (1,2)', function (done) {
    var assertAsync = plan(3);

    function analyzeTestResults(actual, expected, message) {
      assert.deepEqual(actual, expected, message);
      assertAsync(done);
    }

    var inputs = [{ a: { diagram: 'xy|', values: { x: 'a-0', y: 'a-1' } } }, { b: { diagram: 'xyz|', values: { x: 'b-0', y: 'b-1', z: 'b-2' } } }];

    /** @type ExpectedTestResults */
    var expected = {
      m: {
        outputs: ['m-a-0', 'm-b-0', 'm-a-1', 'm-b-1', 'm-b-2'],
        successMessage: 'sink m produces the expected values',
        analyzeTestResults: analyzeTestResults,
        transformFn: undefined
      },
      n: {
        outputs: ['t-n-a-0', 't-n-a-1'],
        successMessage: 'sink n produces the expected values',
        analyzeTestResults: analyzeTestResults,
        transformFn: function transformFn(x) {
          return 't-' + x;
        }
      },
      o: {
        outputs: ['o-b-0', 'o-b-1', 'o-b-2'],
        successMessage: 'sink o produces the expected values',
        analyzeTestResults: analyzeTestResults,
        transformFn: undefined
      }
    };

    var testFn = function testFn(sources) {
      return {
        m: $.merge(sources.a, sources.b).map(function (x) {
          return 'm-' + x;
        }),
        n: sources.a.map(function (x) {
          return 'n-' + x;
        }),
        o: sources.b.delay(3).map(function (x) {
          return 'o-' + x;
        })
      };
    };

    (0, _src.runTestScenario)(inputs, expected, testFn, {
      tickDuration: 10,
      waitForFinishDelay: 30
    });
  });
});

describe("When inputs are simulating an object", function () {
  it('constructs the object according to the mock handler, and emits the' + ' input values through that', function (done) {
    var assertAsync = plan(3);

    function analyzeTestResults(actual, expected, message) {
      assert.deepEqual(actual, expected, message);
      assertAsync(done);
    }

    function noop() {}

    function makeDummyClickEvent(value) {
      return {
        preventDefault: noop,
        tap: function tap(x) {
          return console.log(x);
        },
        target: {
          value: value
        }
      };
    }

    function makeDummyHoverEvent(value) {
      return {
        value: value
      };
    }

    var inputs = [{
      'DOM!input@click': {
        diagram: 'xy|', values: {
          x: makeDummyClickEvent('a-0'), y: makeDummyClickEvent('a-1')
        }
      }
    }, {
      'DOM!a@hover': {
        diagram: 'xyz|', values: {
          x: makeDummyHoverEvent('a-0'),
          y: makeDummyHoverEvent('a-1'),
          z: makeDummyHoverEvent('a-2')
        }
      }
    }, { b: { diagram: 'xyz|', values: { x: 'b-0', y: 'b-1', z: 'b-2' } } }];

    var testFn = function testFn(sources) {
      return {
        m: sources.DOM.select('input').events('click').tap(function (ev) {
          return ev.preventDefault();
        }).map(function (x) {
          return 'm-' + x.target.value;
        }),
        n: sources.DOM.select('a').events('hover').map(function (x) {
          return 'n-' + x.value;
        }),
        o: sources.b.delay(3).map(function (x) {
          return 'o-' + x;
        })
      };
    };

    /** @type ExpectedTestResults */
    var expected = {
      m: {
        outputs: ['m-a-0', 'm-a-1'],
        successMessage: 'sink m produces the expected values',
        analyzeTestResults: analyzeTestResults,
        transformFn: undefined
      },
      n: {
        outputs: ['t-n-a-0', 't-n-a-1', 't-n-a-2'],
        successMessage: 'sink n produces the expected values',
        analyzeTestResults: analyzeTestResults,
        transformFn: function transformFn(x) {
          return 't-' + x;
        }
      },
      o: {
        outputs: ['o-b-0', 'o-b-1', 'o-b-2'],
        successMessage: 'sink o produces the expected values',
        analyzeTestResults: analyzeTestResults,
        transformFn: undefined
      }
    };

    (0, _src.runTestScenario)(inputs, expected, testFn, {
      tickDuration: 10,
      waitForFinishDelay: 30,
      mocks: {
        DOM: _mockDOM.makeMockDOMSource
      },
      sourceFactory: {
        DOM: function DOM() {
          return (0, _mostSubject.hold)(1, (0, _mostSubject.sync)());
        }
      },
      errorHandler: function errorHandler(err) {
        done(err);
      }
    });
  });
});

describe("When inputs are simulating an object, AND there is a mock" + " associated to that object", function () {
  it('constructs the object according to the mock handler, constructs the' + ' sources with the source factory and emits the input values through that', function (done) {
    var assertAsync = plan(3);

    function analyzeTestResults(actual, expected, message) {
      assert.deepEqual(actual, expected, message);
      assertAsync(done);
    }

    function noop() {}

    function makeDummyClickEvent(value) {
      return {
        preventDefault: noop,
        tap: function tap(x) {
          return console.log(x);
        },
        target: {
          value: value
        }
      };
    }

    function makeDummyHoverEvent(value) {
      return {
        value: value
      };
    }

    var inputs = [{
      'DOM!input@click': {
        diagram: 'xy|', values: {
          x: makeDummyClickEvent('a-0'), y: makeDummyClickEvent('a-1')
        }
      }
    }, {
      'DOM!a@hover': {
        diagram: '-xyz|', values: {
          x: makeDummyHoverEvent('a-0'),
          y: makeDummyHoverEvent('a-1'),
          z: makeDummyHoverEvent('a-2')
        }
      }
    }, { b: { diagram: 'xyz|', values: { x: 'b-0', y: 'b-1', z: 'b-2' } } }];

    var testFn = function testFn(sources) {
      var DOMclick = sources.DOM.select('input').events('click');
      var DOMhover = sources.DOM.select('a').events('hover');
      return {
        m: DOMclick.tap(function (ev) {
          return ev.preventDefault();
        }).map(function (x) {
          return 'm-' + x.target.value;
        }),
        n: DOMhover.map(function (x) {
          return 'n-' + x.value;
        }),
        o: DOMhover.combine(function (a, b) {
          return { x: a.value, y: b.target.value };
        }, DOMclick)
      };
    };

    /** @type ExpectedTestResults */
    var expected = {
      m: {
        outputs: ['m-a-0', 'm-a-1'],
        successMessage: 'sink m produces the expected values',
        analyzeTestResults: analyzeTestResults,
        transformFn: undefined
      },
      n: {
        outputs: ['t-n-a-0', 't-n-a-1', 't-n-a-2'],
        successMessage: 'sink n produces the expected values',
        analyzeTestResults: analyzeTestResults,
        transformFn: function transformFn(x) {
          return 't-' + x;
        }
      },
      o: {
        outputs: [{ "x": "a-0", "y": "a-1" }, { "x": "a-1", "y": "a-1" }, { "x": "a-2", "y": "a-1" }],
        successMessage: 'sink o produces the expected values',
        analyzeTestResults: analyzeTestResults,
        transformFn: undefined
      }
    };

    (0, _src.runTestScenario)(inputs, expected, testFn, {
      tickDuration: 10,
      waitForFinishDelay: 30,
      mocks: {
        DOM: _mockDOM.makeMockDOMSource
      },
      sourceFactory: {
        'DOM!input@click': function DOMInputClick() {
          return (0, _mostSubject.sync)();
        },
        'DOM!a@hover': function DOMAHover() {
          return (0, _mostSubject.sync)();
        }
      },
      errorHandler: function errorHandler(err) {
        done(err);
      }
    });
  });
});

// TODO : test errorHandler settings
// TODO : test error handling (basically all thrown exceptions should have a
// test)
// skipped for now, as being of lower priority, and subject to future changes
//# sourceMappingURL=runTestScenario.specs.js.map