/* global chai, $, sinon */

describe('Async Download Modal', function() {
    var url = 'test_url',
        expect = chai.expect;

    describe('#AsyncDownloader.isDone', function() {
        var downloader = null,
            modal = $('#modal'),
            download_poll_id = '12345';

        beforeEach(function() {
            downloader = new AsyncDownloader(modal, url);
            downloader.download_poll_id = download_poll_id;
        });

        var test_done = [
            {input: null, expected: false},
            {input: undefined, expected: false},
            {input: '', expected: false},
            {input: 'progress', expected: false},
            {input: 'progress ready_' + download_poll_id, expected: true},
            {input: 'progress error_' + download_poll_id, expected: true}

        ];

        test_done.forEach(function(test) {
            it('should return ' + test.expected + ' for input "' + test.input + '"', function() {
                expect(downloader.isDone(test.input)).to.be.equal(test.expected);
            });
        });
    });

    describe('#AsyncDownloader', function() {
        var downloader = null,
            modal = $('#download-zip-modal-test'),
            ajax_stub;

        beforeEach(function() {
            ajax_stub = sinon.stub($, 'ajax');
            downloader = new AsyncDownloader(modal, url);
            downloader.POLL_FREQUENCY = 0;
        });

        afterEach(function() {
          ajax_stub.restore();
        });

        it('should only make one download request', function(done) {
            modal.modal({show: true});
            expect($.ajax.callCount).to.equal(1, 'Only expecting 1 ajax call');
            done();
        });

        var tests_state = [
            {state: 'ready_'},
            {state: 'error_'}
        ];

        tests_state.forEach(function(test) {
            it('should poll until ' + test.state, function(done) {
                var pollUrl = 'ajax/temp/123',
                    downloadId = '123';
                ajax_stub.onFirstCall(0).yieldsTo("success", {
                    download_id: downloadId,
                    download_url: pollUrl
                });
                ajax_stub.onSecondCall().yieldsTo("success", 'html progress content');
                ajax_stub.onThirdCall().yieldsTo("success", 'html read content ' + test.state + downloadId);
                modal.modal({show: true});
                setTimeout(function () {
                    expect($.ajax.callCount).to.equal(3, 'Expecting 3 ajax calls');
                    expect(ajax_stub.firstCall.args[0].url).to.equal(url);
                    expect(ajax_stub.secondCall.args[0].url).to.equal(pollUrl);
                    expect(ajax_stub.thirdCall.args[0].url).to.equal(pollUrl);
                    done();
                }, 5);
            });
        });
    });
});

@@ -96,40 +96,10 @@
 url'
-,%0A        expect = chai.expect
 ;%0A
+
 %0A   
@@ -946,22 +946,28 @@
         
-expect
+assert.equal
 (downloa
@@ -992,22 +992,10 @@
 put)
-).to.be.equal(
+, 
 test
@@ -1575,38 +1575,44 @@
 %7D);%0A            
-expect
+assert.equal
 ($.ajax.callCoun
@@ -1612,27 +1612,18 @@
 allCount
-).to.equal(
+, 
 1, 'Only
@@ -2451,22 +2451,28 @@
         
-expect
+assert.equal
 ($.ajax.
@@ -2480,27 +2480,18 @@
 allCount
-).to.equal(
+, 
 3, 'Expe
@@ -2524,38 +2524,44 @@
                 
-expect
+assert.equal
 (ajax_stub.first
@@ -2576,27 +2576,18 @@
 s%5B0%5D.url
-).to.equal(
+, 
 url);%0A  
@@ -2596,38 +2596,44 @@
                 
-expect
+assert.equal
 (ajax_stub.secon
@@ -2645,35 +2645,26 @@
 .args%5B0%5D.url
-).to.equal(
+, 
 pollUrl);%0A  
@@ -2681,22 +2681,28 @@
         
-expect
+assert.equal
 (ajax_st
@@ -2729,19 +2729,10 @@
 .url
-).to.equal(
+, 
 poll

switch to assert