var Collector = require('../promise-collector');
var results = require('promise-results');

describe('Promise Results', function() {
  var PizzaShop;
  beforeEach(function() {
    PizzaShop = new Collector();
    PizzaShop.order = function (requested) {
      return new Promise(function (resolve, reject) {
        PizzaShop.receive(requested, function (actual) {
          var expected = 'hot ' + requested;
          return (expected === actual) ? resolve(actual) : reject(new Error(actual + ' does not match ' + expected));
        });
      });
    };
    PizzaShop.cook = function (pizza) {
      PizzaShop.promise(pizza, Promise.resolve('hot ' + pizza));
    };
    PizzaShop.undercook = function (pizza) {
      PizzaShop.promise(pizza, Promise.reject('cold ' + pizza));
    };
  });
  afterEach(function() {
    PizzaShop = null;
  });

  it('collects', function () {
    return PizzaShop.collect(function() {
      PizzaShop.cook('pepperoni');
      PizzaShop.cook('hawaiian');
      PizzaShop.cook('supreme');
    }).should.eventually.eql({
      pepperoni: 'hot pepperoni',
      hawaiian: 'hot hawaiian',
      supreme: 'hot supreme'
    });
  });

  it('delivers', function () {
    var orders = Promise.all([
      PizzaShop.order('pepperoni'),
      PizzaShop.order('hawaiian'),
      PizzaShop.order('supreme')
    ]);
    return PizzaShop.collect(function() {
      PizzaShop.cook('pepperoni');
      PizzaShop.cook('hawaiian');
      PizzaShop.cook('supreme');
    }).then(function (pizzas) {
      var results = PizzaShop.deliver(pizzas);
      return Object.keys(results).length;
    })
    .should.eventually.eql(3);
  });

  it('understands failure', function () {
    var orders = Promise.all([
      PizzaShop.order('pepperoni'),
      PizzaShop.order('hawaiian')
    ]);
    PizzaShop.collect(function() {
      PizzaShop.cook('pepperoni');
      PizzaShop.undercook('hawaiian');
    }).then(function(pizzas) {
      PizzaShop.deliver(pizzas);
    });
    return orders.should.be.rejected;
  });

  it('delivers failures', function () {
    var orders = results([
      PizzaShop.order('pepperoni'),
      PizzaShop.order('hawaiian'),
      PizzaShop.order('supreme')
    ]);
    return PizzaShop.collect(function() {
      PizzaShop.cook('pepperoni');
      PizzaShop.undercook('hawaiian');
      PizzaShop.undercook('supreme');
    })
    .then(function (pizzas) {
      var results = PizzaShop.deliver(pizzas);
      return Object.keys(results).length;
    })
    .should.eventually.eql(3);
  });

});


@@ -199,19 +199,30 @@
 llector(
+'PizzaShop'
 );%0A
-
     Pizz
@@ -856,17 +856,17 @@
 %0A%0A  it('
-c
+C
 ollects'
@@ -1168,25 +1168,25 @@
  %7D);%0A%0A  it('
-d
+D
 elivers', fu
@@ -1656,17 +1656,17 @@
 %0A%0A  it('
-u
+U
 nderstan
@@ -2033,17 +2033,17 @@
 %0A%0A  it('
-d
+D
 elivers 
@@ -2485,32 +2485,32 @@
 .length;%0A    %7D)%0A
-
     .should.even
@@ -2531,13 +2531,523 @@
 %0A  %7D);%0A%0A
+%0A  it('Makes appropriate payloads', function () %7B%0A    return PizzaShop.collect(function() %7B%0A      PizzaShop.cook('pepperoni');%0A      PizzaShop.undercook('hawaiian');%0A      PizzaShop.undercook('supreme');%0A    %7D)%0A    .then(function (pizzas) %7B%0A      var js = PizzaShop.toPayload(pizzas);%0A      var json = js.replace('PizzaShop=','');%0A      return JSON.parse(json);%0A    %7D)%0A    .should.eventually.eql(%7B%0A      pepperoni: 'hot pepperoni',%0A      hawaiian: 'cold hawaiian',%0A      supreme: 'cold supreme'%0A    %7D);%0A  %7D);%0A%0A
 %7D);%0A%0A

Add coverage for payload