
var Batch = require('../');

describe('Batch', function(){
  var batch;

  beforeEach(function(){
    batch = new Batch;
  })

  describe('#end(callback)', function(){
    describe('when no functions are queued', function(){
      it('should invoke the callback', function(done){
        batch.end(done);
      })
    })

    it('construct an array of results in order', function(done){
      batch.push(function(fn){
        setTimeout(function(){
          fn(null, 'foo');
        }, 100);
      });
      
      batch.push(function(fn){
        setTimeout(function(){
          fn(null, 'bar');
        }, 50);
      });

      batch.end(function(err, res){
        if (err) return done(err);
        res.should.eql(['foo', 'bar']);
        done();
      })
    })

    describe('when several functions are queued', function(){
      it('should invoke the callback', function(done){
        batch.push(function(fn){
          process.nextTick(fn);
        })
        
        batch.push(function(fn){
          process.nextTick(fn);
        })

        batch.end(done);
      })
    })
        
    describe('when a queued function is completed', function(){
      it('should emit "progress" events', function(done){
          
        batch.push(function(fn){
          fn(null, 'foo');
        });
            
        batch.push(function(fn){
          fn(null, 'bar');
        });

        batch.push(function(fn){
          fn(null, 'baz');
        });

        var pending = 3;
        batch.on('progress', function(e){
          switch (e.index) {
            case 0:
              e.value.should.equal('foo');
              e.percent.should.be.a('number');
              e.total.should.be.a('number');
              e.complete.should.be.a('number');
              e.pending.should.be.a('number');
              e.duration.should.be.a('number');
              break;
            case 1:
              e.value.should.equal('bar');
              e.percent.should.be.a('number');
              break;
            case 2:
              e.value.should.equal('baz');
              e.percent.should.be.a('number');
              break;
          }

          --pending || done();
        })
          
        batch.end(function(err, res){
          if (err) return done(err);
        })
      })
    })
    
    describe('when several errors occur', function(){
      it('should invoke the callback with the first error', function(done){
        batch.push(function(fn){
          fn(new Error('fail one'));
        })
        
        batch.push(function(fn){
          fn(new Error('fail two'));
        })

        batch.end(function(err){
          err.message.should.equal('fail one');
          done();
        });
      })
    })
    
    describe('when .throwUp(false) is in effect', function(){
      it('errors should pile up', function(done){
        batch.push(function(fn){
          fn(null, 'foo');
        });

        batch.push(function(fn){
          fn(new Error('fail one'));
        });
            
        batch.push(function(fn){
          fn(null, 'bar');
        });
        
        batch.push(function(fn){
          fn(new Error('fail two'));
        });

        batch.push(function(fn){
          fn(null, 'baz');
        });

        batch.throwUp(false);

        batch.end(function(err, res){
          err.should.be.an.instanceOf(Array);
          err[0].should.equal(null);
          err[1].message.should.equal('fail one');
          err[2].should.equal(null);
          err[3].message.should.equal('fail two');
          err[4].should.equal(null);

          res.should.eql(['foo', undefined, 'bar', undefined, 'baz']);

          done();
        });
      })
    })
  })
})
@@ -497,23 +497,17 @@
     %7D);%0A
-      
 %0A
+
       ba
@@ -947,33 +947,25 @@
 %0A        %7D)%0A
-        
 %0A
+
         batc
@@ -1066,33 +1066,25 @@
   %7D)%0A    %7D)%0A
-        
 %0A
+
     describe
@@ -1193,27 +1193,17 @@
 (done)%7B%0A
-          
 %0A
+
         
@@ -1262,37 +1262,25 @@
         %7D);%0A
-            
 %0A
+
         batc
@@ -2145,27 +2145,17 @@
      %7D)%0A
-          
 %0A
+
         
@@ -2240,37 +2240,33 @@
       %7D)%0A    %7D)%0A
-    
 %0A
+
     describe('wh
@@ -2460,25 +2460,17 @@
      %7D)%0A
-        
 %0A
+
         
@@ -2670,21 +2670,17 @@
 %0A    %7D)%0A
-    
 %0A
+
     desc
@@ -2942,21 +2942,9 @@
 %7D);%0A
-            
 %0A
+
     
@@ -3015,17 +3015,9 @@
 %7D);%0A
-        
 %0A
+
     
@@ -3183,26 +3183,25 @@
  batch.throw
-Up
+s
 (false);%0A%0A  

Change .throwsUp to .throws in test case