'use strict';

describe('qorlate', function () {

    function isPromise(candidate) {
        expect(typeof candidate.then).toBe('function');
        expect(typeof candidate.catch).toBe('function');
        expect(typeof candidate.finally).toBe('function');
    }

    var $rs,
        $t,
        ql8,
        id = 'test',
        idFn = function () { return id; };

    beforeEach(function () {
        module('jlikness.qorlate');
    });

    beforeEach(inject(function($rootScope, $timeout, qorlate) {
        $rs = $rootScope;
        $t = $timeout;
        ql8 = qorlate;
    }));

    it('should return a correlation result with a promise', function () {
        var correlation = ql8();
        expect(correlation).not.toBeNull();
        expect(correlation.id).not.toBeNull();
        expect(correlation.promise).not.toBeNull();
        isPromise(correlation.promise);
        $t.flush();
    });

    it('should return a unique correlation id with each call', function () {
        var correlation1 = ql8(), correlation2 = ql8();
        expect(correlation1.id).not.toBeNull();
        expect(correlation2.id).not.toBeNull();
        expect(correlation1.id).not.toBe(correlation2.id);
        $t.flush();
    });

    it('should use id passed in configuration', function () {
        var correlation = ql8({ id: id });
        expect(correlation.id).toBe(id);
        $t.flush();
    });

    it('should use result of function for id passed in configuration', function () {
        var correlation = ql8({ id: idFn });
        expect(correlation.id).toBe(id);
        $t.flush();
    });

    it('should provide a default timeout value of 5000', function () {
        expect(ql8.defaultTimeout).toBe(5000);
    });

    it('should reject the correlation if it is not resolved in the timeout period', function () {
        var resolved = false,
            rejected = false,
            correlation = ql8();

        correlation.promise.then(function success () {
            resolved = true;
        }, function error () {
            rejected = true;
        });

        $t.flush();

        expect(resolved).toBe(false);
        expect(rejected).toBe(true);
    });

    describe('immediate', function () {

        it('should immediately return a promise.', function () {
            var promise = ql8.immediate();
            expect(promise).not.toBeNull();
            isPromise(promise);
        });

        it('should immediately resolve the promise', function () {
            var resolved = false,
                rejected = false,
                promise = ql8.immediate(id);
            promise.then(function (data) {
                resolved = true;
                expect(data).toBe(id);
            }, function () {
                rejected = true;
            });

            $rs.$digest();

            expect(resolved).toBe(true);
            expect(rejected).toBe(false);
        });

        it('should immediately reject the promise', function () {
            var resolved = false,
                rejected = false,
                promise = ql8.immediate(id, true);

            promise.then(function (data) {
                resolved = true;
                expect(data).toBe(id);
            }, function () {
                rejected = true;
            });

            $rs.$digest();

            expect(resolved).toBe(false);
            expect(rejected).toBe(true);
        });
    });

    it('should use timeout passed in configuration', function () {

    });

    it('should use result of function for timeout passed in configuration', function () {

    });

    it('should cancel the timeout if it is passed as null', function () {

    });

    it('should cancel the timeout if it is passed as a value less than zero', function () {

    });
});
@@ -356,17 +356,16 @@
 turn id;
- 
 %7D;%0A%0A    
@@ -3465,32 +3465,39 @@
 configuration', 
+inject(
 function () %7B%0A%0A 
@@ -3486,35 +3486,148 @@
 t(function (
-) %7B%0A
+$browser) %7B%0A        ql8(%7B timeout: 50 %7D);%0A        expect($browser.deferredFns%5B0%5D.time).toBe(50);%0A        $t.flush();
 %0A    %7D)
+)
 ;%0A%0A    it('s
@@ -3685,32 +3685,39 @@
 configuration', 
+inject(
 function () %7B%0A%0A 
@@ -3706,35 +3706,173 @@
 t(function (
-) %7B%0A
+$browser) %7B%0A        ql8(%7B timeout: function () %7B return 100; %7D%7D);%0A        expect($browser.deferredFns%5B0%5D.time).toBe(100);%0A        $t.flush();
 %0A    %7D)
+)
 ;%0A%0A    it('s
@@ -3918,24 +3918,31 @@
 d as null', 
+inject(
 function () 
@@ -3939,27 +3939,122 @@
 nction (
-) %7B%0A
+$browser) %7B%0A        ql8( %7B timeout: null %7D );%0A        expect($browser.deferredFns.length).toBe(0);
 %0A    %7D)
+)
 ;%0A%0A    i
@@ -4126,16 +4126,23 @@
  zero', 
+inject(
 function
@@ -4147,19 +4147,111 @@
 on (
-) %7B%0A
+$browser) %7B%0A        ql8( %7B timeout: -1 %7D);%0A        expect($browser.deferredFns.length).toBe(0);
 %0A    %7D)
+)
 ;%0A%7D)

Add timeout tests