/* global describe, it */
'use strict';

delete require.cache[require.resolve('..')];
var batch = require('..');
var assert = require('power-assert');
var async = require('async');

describe('glob-batch', function () {
    it('should support domains `on(\'error\', ...)` without callback', function (done) {
        var domain = require('domain').create();
        domain.on('error', function (err) {
            assert.ok(err);
            done();
        });
        var receiver = domain.bind(batch({ timeout: 10 }, function () {
            throw new Error('Bang!');
        }));
        receiver('one');
    });

    it('should support domains `on(\'error\', ...)` with callback', function (done) {
        var domain = require('domain').create();
        domain.on('error', function (err) {
            assert.ok(err);
            done();
        });
        var receiver = domain.bind(batch({ timeout: 10 }, function (events, async) {
            async(new Error('Bang!'));
        }));
        receiver('one');
    });

    it('should support domains `on(\'error\', ...)` with callback, but with throw', function (done) {
        var domain = require('domain').create();
        domain.on('error', function (err) {
            assert.ok(err);
            done();
        });
        var receiver = domain.bind(batch({ timeout: 10 }, function (events, async) {
            throw new Error('Bang!');
        }));
        receiver('one');
    });

    it('should batch sync calls to array', function (done) {
        var receiver = batch({ timeout: 10 }, function (events) {
            assert.equal(events.length, 2);
            done();
        });
        receiver('one');
        receiver('two');
    });

    it('should batch async calls to array', function (done) {
        var receiver = batch({ timeout: 10 }, function (events) {
            assert.equal(events.length, 2);
            done();
        });
        receiver('one');
        setTimeout(receiver.bind(null, 'two'), 5);
    });

    it('should flush, if we exceed timeout', function (done) {
        var iterator = async.iterator([
            function (events) { assert.equal(events.length, 1); },
            function () { done(); }
        ]);

        var receiver = batch({ timeout: 5 }, function (events) {
            iterator = iterator(events);
        });

        receiver('one');
        setTimeout(receiver.bind(null, 'two'), 10);
    });

    it('should flush, if we exceed limit', function (done) {
        var iterator = async.iterator([
            function (events) { assert.equal(events.length, 2); },
            function (events) {
                assert.equal(events.length, 1);
                done();
            }
        ]);
        var receiver = batch({ timeout: 10, limit: 2 }, function (events) {
            iterator = iterator(events);
        });
        receiver('one');
        receiver('two');
        receiver('three');
    });

    it('should support done callback function', function (done) {
        var iterator = async.iterator([
            function (events, cb) {
                receiver('two');
                setTimeout(function () {
                    cb();
                    receiver('three');
                }, 15);
            },
            function (events) {
                assert.equal(events.length, 2);
                done();
            }
        ]);
        var receiver = batch({ timeout: 10 }, function (events, cb) {
            iterator = iterator(events, cb);
        });
        receiver('one');
    });

    it('should support debounce option', function (done) {
        var iterator = async.iterator([
            function (events, cb) {
                receiver('two');
                setTimeout(function () {
                    cb();
                    setTimeout(receiver.bind(null, 'three'), 15);
                }, 15);
            },
            function (events) {
                assert.equal(events.length, 2);
                done();
            }
        ]);
        var receiver = batch({ timeout: 10, debounce: 10 }, function (events, cb) {
            iterator = iterator(events, cb);
        });
        receiver('one');
    });

    it('should throw, if we provide invalid callback', function () {
        assert.throws(batch, /Provided callback is not a function/);
        assert.throws(batch.bind(null, 'string'), /Provided callback is not a function/);
    });

});

@@ -175,16 +175,68 @@
 ync');%0A%0A
+var defaultTimeout = 10,%0A    defaultDebounce = 10;%0A%0A
 describe
@@ -260,24 +260,182 @@
 nction () %7B%0A
+    it('should swap callback, if options omitted', function (done) %7B%0A        var receiver = batch(function () %7B done(); %7D);%0A        receiver('one');%0A    %7D);%0A%0A
     it('shou
@@ -708,34 +708,457 @@
 atch(%7B timeout: 
-10
+defaultTimeout %7D, function () %7B%0A            throw new Error('Bang!');%0A        %7D));%0A        receiver('one');%0A    %7D);%0A%0A    it('should support domains %60on(%5C'error%5C', ...)%60 without callback', function (done) %7B%0A        var domain = require('domain').create();%0A        domain.on('error', function (err) %7B%0A            assert.ok(err);%0A            done();%0A        %7D);%0A        var receiver = domain.bind(batch(%7B timeout: defaultTimeout
  %7D, function () 
@@ -1527,34 +1527,46 @@
 atch(%7B timeout: 
-10
+defaultTimeout
  %7D, function (ev
@@ -1965,34 +1965,46 @@
 atch(%7B timeout: 
-10
+defaultTimeout
  %7D, function (ev
@@ -2196,34 +2196,46 @@
 atch(%7B timeout: 
-10
+defaultTimeout
  %7D, function (ev
@@ -2471,34 +2471,46 @@
 atch(%7B timeout: 
-10
+defaultTimeout
  %7D, function (ev
@@ -3088,18 +3088,30 @@
 'two'), 
-10
+defaultTimeout
 );%0A    %7D
@@ -3452,18 +3452,30 @@
 imeout: 
-10
+defaultTimeout
 , limit:
@@ -4129,18 +4129,30 @@
 imeout: 
-10
+defaultTimeout
  %7D, func
@@ -4776,24 +4776,49 @@
 ut: 
-10, debounce: 10
+defaultTimeout, debounce: defaultDebounce
  %7D, 

Test on constructor