var Immutable = require("../../seamless-immutable.js");
var JSC       = require("jscheck");
var TestUtils = require("../TestUtils.js");
var assert    = require("chai").assert;
var _         = require("lodash")
var check     = TestUtils.check;

module.exports = function() {
  describe("#flatMap", function() {
    // Sanity check to make sure our QuickCheck logic isn't off the rails.
    it("passes a basic sanity check on canned input", function() {
      var expected = Immutable(["foo", "foo2", "bar", "bar2", "baz", "baz2"]);
      var actual   = Immutable(["foo", "bar", "baz"]).flatMap(function(elem) {
        return [elem, elem + "2"];
      });

      assert.deepEqual(actual, expected);
    });
  });
};
var Immutable = require("../../seamless-immutable.js");
var JSC       = require("jscheck");
var TestUtils = require("../TestUtils.js");
var assert    = require("chai").assert;
var _         = require("lodash")
var check     = TestUtils.check;

module.exports = function() {
  describe("#flatMap", function() {
    // Sanity check to make sure our QuickCheck logic isn't off the rails.
    it("passes a basic sanity check on canned input", function() {
      var expected = Immutable(["foo", "foo2", "bar", "bar2", "baz", "baz2"]);
      var actual   = Immutable(["foo", "bar", "baz"]).flatMap(function(elem) {
        return [elem, elem + "2"];
      });

      assert.deepEqual(actual, expected);
    });

    it("works the same way as map when the iterator function returns non-arrays", function() {
      check(100, [JSC.array()], function(array) {
        var returnValues = array.map(function() { return JSC.any()(); });
        var iterator = function(value, index) { return returnValues[index]; };
        var expected = _.map(array, iterator);
        var actual   = Immutable(array).flatMap(iterator);

        assert.deepEqual(actual, expected);
      });
    });

    it("works the same way as map followed by flatten when the iterator function returns arrays", function() {
      check(100, [JSC.array()], function(array) {
        var returnValues = array.map(function() { return [JSC.any()()]; });
        var iterator = function(value, index) { return returnValues[index]; };
        var expected = _.flatten(_.map(array, iterator));
        var actual   = Immutable(array).flatMap(iterator);

        assert.deepEqual(actual, expected);
      });
    });

    it("results in an empty array if the iterator function returns empty array", function() {
      var expected = Immutable([]);
      var iterator = function() { return [] };

      check(100, [JSC.array()], function(array) {
        var actual = Immutable(array).flatMap(iterator);

        assert.deepEqual(actual, expected);
      });
    });
  });
};
Add more flatMap tests.