web/test/jstests/test_ajax.js
changeset 5658 7b9553a9db65
child 5743 4a0600664f85
equal deleted inserted replaced
5655:ef903fff826d 5658:7b9553a9db65
       
     1 $(document).ready(function() {
       
     2 
       
     3     function jsSources() {
       
     4         return $.map($('head script[src]'), function(script) {
       
     5             return script.getAttribute('src');
       
     6         });
       
     7     }
       
     8 
       
     9     test('test simple h1 inclusion (ajax_url0.html)', function() {
       
    10         expect(3);
       
    11         equals(jQuery('#main').children().length, 0);
       
    12         stop();
       
    13         jQuery('#main').loadxhtml('/../ajax_url0.html', {
       
    14             callback: function() {
       
    15                 equals(jQuery('#main').children().length, 1);
       
    16                 equals(jQuery('#main h1').html(), 'Hello');
       
    17                 start();
       
    18             }
       
    19         });
       
    20     });
       
    21 
       
    22     test('test simple html head inclusion (ajax_url1.html)', function() {
       
    23         expect(6);
       
    24         var scriptsIncluded = jsSources();
       
    25         equals(jQuery.inArray('http://foo.js', scriptsIncluded), - 1);
       
    26         stop();
       
    27         jQuery('#main').loadxhtml('/../ajax_url1.html', {
       
    28             callback: function() {
       
    29                 var origLength = scriptsIncluded.length;
       
    30                 scriptsIncluded = jsSources();
       
    31                 // check that foo.js has been inserted in <head>
       
    32                 equals(scriptsIncluded.length, origLength + 1);
       
    33                 equals(scriptsIncluded[origLength].indexOf('http://foo.js'), 0);
       
    34                 // check that <div class="ajaxHtmlHead"> has been removed
       
    35                 equals(jQuery('#main').children().length, 1);
       
    36                 equals(jQuery('div.ajaxHtmlHead').length, 0);
       
    37                 equals(jQuery('#main h1').html(), 'Hello');
       
    38                 start();
       
    39             }
       
    40         });
       
    41     });
       
    42 
       
    43     test('test addCallback', function() {
       
    44         expect(3);
       
    45         equals(jQuery('#main').children().length, 0);
       
    46         stop();
       
    47         var d = jQuery('#main').loadxhtml('/../ajax_url0.html');
       
    48         d.addCallback(function() {
       
    49             equals(jQuery('#main').children().length, 1);
       
    50             equals(jQuery('#main h1').html(), 'Hello');
       
    51             start();
       
    52         });
       
    53     });
       
    54 
       
    55     test('test callback after synchronous request', function() {
       
    56         expect(1);
       
    57         var deferred = new Deferred();
       
    58         var result = jQuery.ajax({
       
    59             url: './ajax_url0.html',
       
    60             async: false,
       
    61             beforeSend: function(xhr) {
       
    62                 deferred._req = xhr;
       
    63             },
       
    64             success: function(data, status) {
       
    65                 deferred.success(data);
       
    66             }
       
    67         });
       
    68         stop();
       
    69         deferred.addCallback(function() {
       
    70             // add an assertion to ensure the callback is executed
       
    71             ok(true, "callback is executed");
       
    72             start();
       
    73         });
       
    74     });
       
    75 
       
    76     test('test addCallback with parameters', function() {
       
    77         expect(3);
       
    78         equals(jQuery('#main').children().length, 0);
       
    79         stop();
       
    80         var d = jQuery('#main').loadxhtml('/../ajax_url0.html');
       
    81         d.addCallback(function(data, req, arg1, arg2) {
       
    82             equals(arg1, 'Hello');
       
    83             equals(arg2, 'world');
       
    84             start();
       
    85         },
       
    86         'Hello', 'world');
       
    87     });
       
    88 
       
    89     test('test callback after synchronous request with parameters', function() {
       
    90         var deferred = new Deferred();
       
    91         var result = jQuery.ajax({
       
    92             url: './ajax_url0.html',
       
    93             async: false,
       
    94             beforeSend: function(xhr) {
       
    95                 deferred._req = xhr;
       
    96             },
       
    97             success: function(data, status) {
       
    98                 deferred.success(data);
       
    99             }
       
   100         });
       
   101         deferred.addCallback(function(data, req, arg1, arg2) {
       
   102             // add an assertion to ensure the callback is executed
       
   103             ok(true, "callback is executed");
       
   104             equals(arg1, 'Hello');
       
   105             equals(arg2, 'world');
       
   106         },
       
   107         'Hello', 'world');
       
   108     });
       
   109 
       
   110     test('test addErrback', function() {
       
   111         expect(1);
       
   112         stop();
       
   113         var d = jQuery('#main').loadxhtml('/../ajax_url0.html');
       
   114         d.addCallback(function() {
       
   115             // throw an exception to start errback chain
       
   116             throw new Error();
       
   117         });
       
   118         d.addErrback(function() {
       
   119             ok(true, "errback is executed");
       
   120             start();
       
   121         });
       
   122     });
       
   123 
       
   124     test('test callback / errback execution order', function() {
       
   125         expect(4);
       
   126         var counter = 0;
       
   127         stop();
       
   128         var d = jQuery('#main').loadxhtml('/../ajax_url0.html', {
       
   129             callback: function() {
       
   130                 equals(++counter, 1); // should be executed first
       
   131                 start();
       
   132             }
       
   133         });
       
   134         d.addCallback(function() {
       
   135             equals(++counter, 2); // should be executed and break callback chain
       
   136             throw new Error();
       
   137         });
       
   138         d.addCallback(function() {
       
   139             // should not be executed since second callback raised an error
       
   140             ok(false, "callback is executed");
       
   141         });
       
   142         d.addErrback(function() {
       
   143             // should be executed after the second callback
       
   144             equals(++counter, 3);
       
   145         });
       
   146         d.addErrback(function() {
       
   147             // should be executed after the first errback
       
   148             equals(++counter, 4);
       
   149         });
       
   150     });
       
   151 
       
   152     test('test already included resources are ignored (ajax_url2.html)', function() {
       
   153         expect(10);
       
   154         var scriptsIncluded = jsSources();
       
   155         equals(jQuery.inArray('http://foo.js', scriptsIncluded), - 1);
       
   156         equals(jQuery('head link').length, 1);
       
   157         equals(jQuery('head link').attr('href'), 'qunit.css');
       
   158         stop();
       
   159         jQuery('#main').loadxhtml('/../ajax_url1.html', {
       
   160             callback: function() {
       
   161                 var origLength = scriptsIncluded.length;
       
   162                 scriptsIncluded = jsSources();
       
   163                 // check that foo.js has been inserted in <head>
       
   164                 equals(scriptsIncluded.length, origLength + 1);
       
   165                 equals(scriptsIncluded[origLength].indexOf('http://foo.js'), 0);
       
   166                 // check that <div class="ajaxHtmlHead"> has been removed
       
   167                 equals(jQuery('#main').children().length, 1);
       
   168                 equals(jQuery('div.ajaxHtmlHead').length, 0);
       
   169                 equals(jQuery('#main h1').html(), 'Hello');
       
   170                 // qunit.css is not added twice
       
   171                 equals(jQuery('head link').length, 1);
       
   172                 equals(jQuery('head link').attr('href'), 'qunit.css');
       
   173                 start();
       
   174             }
       
   175         });
       
   176     });
       
   177 
       
   178     test('test synchronous request loadRemote', function() {
       
   179         var res = loadRemote('/../ajaxresult.json', {},
       
   180         'GET', true);
       
   181         same(res, ['foo', 'bar']);
       
   182     });
       
   183 
       
   184     test('test event on CubicWeb', function() {
       
   185         expect(1);
       
   186         stop();
       
   187         var events = null;
       
   188         jQuery(CubicWeb).bind('server-response', function() {
       
   189             // check that server-response event on CubicWeb is triggered
       
   190             events = 'CubicWeb';
       
   191         });
       
   192         jQuery('#main').loadxhtml('/../ajax_url0.html', {
       
   193             callback: function() {
       
   194                 equals(events, 'CubicWeb');
       
   195                 start();
       
   196             }
       
   197         });
       
   198     });
       
   199 
       
   200     test('test event on node', function() {
       
   201         expect(3);
       
   202         stop();
       
   203         var nodes = [];
       
   204         jQuery('#main').bind('server-response', function() {
       
   205             nodes.push('node');
       
   206         });
       
   207         jQuery(CubicWeb).bind('server-response', function() {
       
   208             nodes.push('CubicWeb');
       
   209         });
       
   210         jQuery('#main').loadxhtml('/../ajax_url0.html', {
       
   211             callback: function() {
       
   212                 equals(nodes.length, 2);
       
   213                 // check that server-response event on CubicWeb is triggered
       
   214                 // only once and event server-response on node is triggered
       
   215                 equals(nodes[0], 'CubicWeb');
       
   216                 equals(nodes[1], 'node');
       
   217                 start();
       
   218             }
       
   219         });
       
   220     });
       
   221 });
       
   222