// Unit test for a.state (plugin)
// We handle bug and test to check they are not coming back

module('plugin/state.js');

// In this unit test, we check 2 children, with same parent element,
// and same hashtag, are both loaded not only one
test('a.state.dualchildren', function() {
    stop();
    expect(4);

    a.state.clear();

    var se = strictEqual,
        st = start;

    var tree = {
        id : 'ok',
        children : [
            {
                hash : 'unittest-dualchildren',
                load : function(chain) {
                    se(1, 1, 'Test loading first child');
                    chain.next();
                },
                unload : function(chain) {
                    se(1, 1, 'Test unloading first child');
                    chain.next();
                }
            },
            {
                hash : 'unittest-dualchildren',
                load : function(chain) {
                    se(1, 1, 'Test loading second child');
                    chain.next();
                },
                unload : function(chain) {
                    se(1, 1, 'Test unloading second child');
                    chain.next();
                }
            }
        ]
    };

    a.state.add(tree);

    // Now starting to proceed loader
    setTimeout(function() {
        window.location.href = '#unittest-dualchildren';
    }, 200);

    // Old browser will need a little wait...
    setTimeout(function() {
        window.location.href = '#tmp';
    }, 600);

    // Old browser will need a little wait...
    setTimeout(function() {
        a.state.clear();
        window.location.href = '#';
        st();
    }, 1000);
});



// Bug : passing data threw preLoad, load and postLoad
test('a.state.parameter-passthrew', function() {
    stop();
    expect(3);
    a.state.clear();

    var se = strictEqual,
        st = start;

    var test = {
        id : 'testloadpassthrew',
        data : {
            objId : '{{memory : test_objid}}'
        },
        converter : function(data) {
            data.plop = data.objId;
        },
        preLoad : function(chain) {
            this.data['data2'] = 'ok';
            chain.next();
        },
        postLoad : function(chain) {
            se(this.data['objId'], 'hello from data', 'test data pass');
            se(this.data['plop'], 'hello from data', 'test data pass');
            se(this.data['data2'], 'ok', 'test from postload');

            st();
            a.state.clear();
            chain.next();
        }
    };
    a.state.add(test);

    // Place a value and try result
    a.storage.memory.set('test_objid', 'hello from data');
    a.state.load('testloadpassthrew');
});


// Bug : too long request, may make the system putting
// content of #a while #b is already loading...
test('a.state.request-abort', function() {
    stop();
    expect(1);
    a.state.clear();

    var se = strictEqual,
        st = start;

    var b = {
        id : 'child-b',
        hash : 'request-abort-b',
        preLoad : function(chain) {
            se(true, true, 'Arrive on time');
            chain.next();
        }
    };

    var c = {
        id : 'child-c',
        hash : 'request-abort-c',
        preLoad : function(chain) {
            a.timer.once(chain.error, null, 500);
        },
        load : function(chain) {
            se(false, true, 'should be cancelled');
            chain.next();
        }
    };

    a.state.add(c);
    a.state.add(b);

    // Now starting to proceed loader
    setTimeout(function() {
        window.location.href = '#request-abort-c';
    }, 200);

    // Old browser will need a little wait...
    setTimeout(function() {
        window.location.href = '#request-abort-b';
    }, 600);

    // Old browser will need a little wait...
    setTimeout(function() {
        a.state.clear();
        window.location.href = '#';
        st();
    }, 1000);
});
@@ -1288,234 +1288,89 @@
     
-setTimeout(function() %7B%0A        window.location.href = '#unittest-dualchildren';%0A    %7D, 200);%0A%0A    // Old browser will need a little wait...%0A    setTimeout(function() %7B%0A        window.location.href = '#
+chain('unittest-dualchildren', function() %7B%0A        hashtag('
 tmp'
+)
 ;%0A    %7D
-, 600
 );%0A%0A    
 // O
@@ -1369,65 +1369,21 @@
     
-// Old browser will need a little wait...%0A    setTimeout(
+chain('tmp', 
 func
@@ -1469,28 +1469,66 @@
 t();%0A    %7D, 
-1000
+200);%0A%0A    hashtag('unittest-dualchildren'
 );%0A%7D);%0A%0A%0A%0A//

Switch one test to new chain system.