web/test/jstests/test_ajax.js
changeset 10430 2b1ea3e8e080
parent 10429 98ea2b865210
child 10431 7628a14d2209
equal deleted inserted replaced
10429:98ea2b865210 10430:2b1ea3e8e080
    23     }
    23     }
    24 
    24 
    25     QUnit.test('test simple h1 inclusion (ajax_url0.html)', function (assert) {
    25     QUnit.test('test simple h1 inclusion (ajax_url0.html)', function (assert) {
    26         assert.expect(3);
    26         assert.expect(3);
    27         assert.equal(jQuery('#qunit-fixture').children().length, 0);
    27         assert.equal(jQuery('#qunit-fixture').children().length, 0);
    28         stop();
    28         var done = assert.async();
    29         jQuery('#qunit-fixture').loadxhtml(BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url0.html')
    29         jQuery('#qunit-fixture').loadxhtml(BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url0.html')
    30         .addCallback(function() {
    30         .addCallback(function() {
    31                 try {
    31                 try {
    32                     assert.equal(jQuery('#qunit-fixture').children().length, 1);
    32                     assert.equal(jQuery('#qunit-fixture').children().length, 1);
    33                     assert.equal(jQuery('#qunit-fixture h1').html(), 'Hello');
    33                     assert.equal(jQuery('#qunit-fixture h1').html(), 'Hello');
    34                 } finally {
    34                 } finally {
    35                     start();
    35                     done();
    36                 };
    36                 };
    37             }
    37             }
    38         );
    38         );
    39     });
    39     });
    40 
    40 
    41     QUnit.test('test simple html head inclusion (ajax_url1.html)', function (assert) {
    41     QUnit.test('test simple html head inclusion (ajax_url1.html)', function (assert) {
    42         assert.expect(6);
    42         assert.expect(6);
    43         var scriptsIncluded = jsSources();
    43         var scriptsIncluded = jsSources();
    44         assert.equal(jQuery.inArray('http://foo.js', scriptsIncluded), - 1);
    44         assert.equal(jQuery.inArray('http://foo.js', scriptsIncluded), - 1);
    45         stop();
    45         var done = assert.async();
    46         jQuery('#qunit-fixture').loadxhtml(BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url1.html')
    46         jQuery('#qunit-fixture').loadxhtml(BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url1.html')
    47         .addCallback(function() {
    47         .addCallback(function() {
    48                 try {
    48                 try {
    49                     var origLength = scriptsIncluded.length;
    49                     var origLength = scriptsIncluded.length;
    50                     scriptsIncluded = jsSources();
    50                     scriptsIncluded = jsSources();
    54                     // check that <div class="ajaxHtmlHead"> has been removed
    54                     // check that <div class="ajaxHtmlHead"> has been removed
    55                     assert.equal(jQuery('#qunit-fixture').children().length, 1);
    55                     assert.equal(jQuery('#qunit-fixture').children().length, 1);
    56                     assert.equal(jQuery('div.ajaxHtmlHead').length, 0);
    56                     assert.equal(jQuery('div.ajaxHtmlHead').length, 0);
    57                     assert.equal(jQuery('#qunit-fixture h1').html(), 'Hello');
    57                     assert.equal(jQuery('#qunit-fixture h1').html(), 'Hello');
    58                 } finally {
    58                 } finally {
    59                     start();
    59                     done();
    60                 };
    60                 };
    61             }
    61             }
    62         );
    62         );
    63     });
    63     });
    64 
    64 
    65     QUnit.test('test addCallback', function (assert) {
    65     QUnit.test('test addCallback', function (assert) {
    66         assert.expect(3);
    66         assert.expect(3);
    67         assert.equal(jQuery('#qunit-fixture').children().length, 0);
    67         assert.equal(jQuery('#qunit-fixture').children().length, 0);
    68         stop();
    68         var done = assert.async();
    69         var d = jQuery('#qunit-fixture').loadxhtml(BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url0.html');
    69         var d = jQuery('#qunit-fixture').loadxhtml(BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url0.html');
    70         d.addCallback(function() {
    70         d.addCallback(function() {
    71             try {
    71             try {
    72                 assert.equal(jQuery('#qunit-fixture').children().length, 1);
    72                 assert.equal(jQuery('#qunit-fixture').children().length, 1);
    73                 assert.equal(jQuery('#qunit-fixture h1').html(), 'Hello');
    73                 assert.equal(jQuery('#qunit-fixture h1').html(), 'Hello');
    74             } finally {
    74             } finally {
    75                 start();
    75                 done();
    76             };
    76             };
    77         });
    77         });
    78     });
    78     });
    79 
    79 
    80     QUnit.test('test callback after synchronous request', function (assert) {
    80     QUnit.test('test callback after synchronous request', function (assert) {
    88             },
    88             },
    89             success: function(data, status) {
    89             success: function(data, status) {
    90                 deferred.success(data);
    90                 deferred.success(data);
    91             }
    91             }
    92         });
    92         });
    93         stop();
    93         var done = assert.async();
    94         deferred.addCallback(function() {
    94         deferred.addCallback(function() {
    95             try {
    95             try {
    96                 // add an assertion to ensure the callback is executed
    96                 // add an assertion to ensure the callback is executed
    97                 assert.ok(true, "callback is executed");
    97                 assert.ok(true, "callback is executed");
    98             } finally {
    98             } finally {
    99                 start();
    99                 done();
   100             };
   100             };
   101         });
   101         });
   102     });
   102     });
   103 
   103 
   104     QUnit.test('test addCallback with parameters', function (assert) {
   104     QUnit.test('test addCallback with parameters', function (assert) {
   105         assert.expect(3);
   105         assert.expect(3);
   106         assert.equal(jQuery('#qunit-fixture').children().length, 0);
   106         assert.equal(jQuery('#qunit-fixture').children().length, 0);
   107         stop();
   107         var done = assert.async();
   108         var d = jQuery('#qunit-fixture').loadxhtml(BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url0.html');
   108         var d = jQuery('#qunit-fixture').loadxhtml(BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url0.html');
   109         d.addCallback(function(data, req, arg1, arg2) {
   109         d.addCallback(function(data, req, arg1, arg2) {
   110             try {
   110             try {
   111                 assert.equal(arg1, 'Hello');
   111                 assert.equal(arg1, 'Hello');
   112                 assert.equal(arg2, 'world');
   112                 assert.equal(arg2, 'world');
   113             } finally {
   113             } finally {
   114                 start();
   114                 done();
   115             };
   115             };
   116         },
   116         },
   117         'Hello', 'world');
   117         'Hello', 'world');
   118     });
   118     });
   119 
   119 
   125             try {
   125             try {
   126                 assert.ok(true, "callback is executed");
   126                 assert.ok(true, "callback is executed");
   127                 assert.equal(arg1, 'Hello');
   127                 assert.equal(arg1, 'Hello');
   128                 assert.equal(arg2, 'world');
   128                 assert.equal(arg2, 'world');
   129             } finally {
   129             } finally {
   130                 start();
   130                 done();
   131             };
   131             };
   132         },
   132         },
   133         'Hello', 'world');
   133         'Hello', 'world');
   134         deferred.addErrback(function() {
   134         deferred.addErrback(function() {
   135             // throw an exception to start errback chain
   135             // throw an exception to start errback chain
   136             try {
   136             try {
   137                 throw this._error;
   137                 throw this._error;
   138             } finally {
   138             } finally {
   139                 start();
   139                 done();
   140             };
   140             };
   141         });
   141         });
   142         stop();
   142         var done = assert.async();
   143         var result = jQuery.ajax({
   143         var result = jQuery.ajax({
   144             url: BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url0.html',
   144             url: BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url0.html',
   145             async: false,
   145             async: false,
   146             beforeSend: function(xhr) {
   146             beforeSend: function(xhr) {
   147                 deferred._req = xhr;
   147                 deferred._req = xhr;
   152         });
   152         });
   153     });
   153     });
   154 
   154 
   155   QUnit.test('test addErrback', function (assert) {
   155   QUnit.test('test addErrback', function (assert) {
   156         assert.expect(1);
   156         assert.expect(1);
   157         stop();
   157         var done = assert.async();
   158         var d = jQuery('#qunit-fixture').loadxhtml(BASE_URL + 'cwsoftwareroot/web/test/jstests/nonexistent.html');
   158         var d = jQuery('#qunit-fixture').loadxhtml(BASE_URL + 'cwsoftwareroot/web/test/jstests/nonexistent.html');
   159         d.addCallback(function() {
   159         d.addCallback(function() {
   160             // should not be executed
   160             // should not be executed
   161             assert.ok(false, "callback is executed");
   161             assert.ok(false, "callback is executed");
   162         });
   162         });
   163         d.addErrback(function() {
   163         d.addErrback(function() {
   164             try {
   164             try {
   165                 assert.ok(true, "errback is executed");
   165                 assert.ok(true, "errback is executed");
   166             } finally {
   166             } finally {
   167                 start();
   167                 done();
   168             };
   168             };
   169         });
   169         });
   170     });
   170     });
   171 
   171 
   172     QUnit.test('test callback execution order', function (assert) {
   172     QUnit.test('test callback execution order', function (assert) {
   173         assert.expect(3);
   173         assert.expect(3);
   174         var counter = 0;
   174         var counter = 0;
   175         stop();
   175         var done = assert.async();
   176         var d = jQuery('#qunit-fixture').loadxhtml(BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url0.html');
   176         var d = jQuery('#qunit-fixture').loadxhtml(BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url0.html');
   177         d.addCallback(function() {
   177         d.addCallback(function() {
   178                 try {
   178             assert.equal(++counter, 1); // should be executed first
   179                     assert.equal(++counter, 1); // should be executed first
   179         });
   180                 } finally {
       
   181                     start();
       
   182                 };
       
   183             }
       
   184         );
       
   185         d.addCallback(function() {
   180         d.addCallback(function() {
   186             assert.equal(++counter, 2);
   181             assert.equal(++counter, 2);
   187         });
   182         });
   188         d.addCallback(function() {
   183         d.addCallback(function() {
   189             assert.equal(++counter, 3);
   184             try {
       
   185                 assert.equal(++counter, 3);
       
   186             } finally {
       
   187                 done();
       
   188             }
   190         });
   189         });
   191     });
   190     });
   192 
   191 
   193     QUnit.test('test already included resources are ignored (ajax_url1.html)', function (assert) {
   192     QUnit.test('test already included resources are ignored (ajax_url1.html)', function (assert) {
   194         assert.expect(10);
   193         assert.expect(10);
   196         // NOTE:
   195         // NOTE:
   197         assert.equal(jQuery.inArray('http://foo.js', scriptsIncluded), -1);
   196         assert.equal(jQuery.inArray('http://foo.js', scriptsIncluded), -1);
   198         assert.equal(jQuery('head link').length, 1);
   197         assert.equal(jQuery('head link').length, 1);
   199         /* use endswith because in pytest context we have an absolute path */
   198         /* use endswith because in pytest context we have an absolute path */
   200         assert.ok(jQuery('head link').attr('href').endswith('/qunit.css'), 'qunit.css is loaded');
   199         assert.ok(jQuery('head link').attr('href').endswith('/qunit.css'), 'qunit.css is loaded');
   201         stop();
   200         var done = assert.async();
   202         jQuery('#qunit-fixture').loadxhtml(BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url1.html')
   201         jQuery('#qunit-fixture').loadxhtml(BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url1.html')
   203         .addCallback(function() {
   202         .addCallback(function() {
   204                 var origLength = scriptsIncluded.length;
   203                 var origLength = scriptsIncluded.length;
   205                 scriptsIncluded = jsSources();
   204                 scriptsIncluded = jsSources();
   206                 try {
   205                 try {
   214                     // qunit.css is not added twice
   213                     // qunit.css is not added twice
   215                     assert.equal(jQuery('head link').length, 1);
   214                     assert.equal(jQuery('head link').length, 1);
   216                     /* use endswith because in pytest context we have an absolute path */
   215                     /* use endswith because in pytest context we have an absolute path */
   217                     assert.ok(jQuery('head link').attr('href').endswith('/qunit.css'), 'qunit.css is loaded');
   216                     assert.ok(jQuery('head link').attr('href').endswith('/qunit.css'), 'qunit.css is loaded');
   218                 } finally {
   217                 } finally {
   219                     start();
   218                     done();
   220                 }
   219                 }
   221             }
   220             }
   222         );
   221         );
   223     });
   222     });
   224 
   223 
   228         assert.deepEqual(res, ['foo', 'bar']);
   227         assert.deepEqual(res, ['foo', 'bar']);
   229     });
   228     });
   230 
   229 
   231     QUnit.test('test event on CubicWeb', function (assert) {
   230     QUnit.test('test event on CubicWeb', function (assert) {
   232         assert.expect(1);
   231         assert.expect(1);
   233         stop();
   232         var done = assert.async();
   234         var events = null;
   233         var events = null;
   235         jQuery(CubicWeb).bind('server-response', function() {
   234         jQuery(CubicWeb).bind('server-response', function() {
   236             // check that server-response event on CubicWeb is triggered
   235             // check that server-response event on CubicWeb is triggered
   237             events = 'CubicWeb';
   236             events = 'CubicWeb';
   238         });
   237         });
   239         jQuery('#qunit-fixture').loadxhtml(BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url0.html')
   238         jQuery('#qunit-fixture').loadxhtml(BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url0.html')
   240         .addCallback(function() {
   239         .addCallback(function() {
   241                 try {
   240                 try {
   242                     assert.equal(events, 'CubicWeb');
   241                     assert.equal(events, 'CubicWeb');
   243                 } finally {
   242                 } finally {
   244                     start();
   243                     done();
   245                 };
   244                 };
   246             }
   245             }
   247         );
   246         );
   248     });
   247     });
   249 
   248 
   250     QUnit.test('test event on node', function (assert) {
   249     QUnit.test('test event on node', function (assert) {
   251         assert.expect(3);
   250         assert.expect(3);
   252         stop();
   251         var done = assert.async();
   253         var nodes = [];
   252         var nodes = [];
   254         jQuery('#qunit-fixture').bind('server-response', function() {
   253         jQuery('#qunit-fixture').bind('server-response', function() {
   255             nodes.push('node');
   254             nodes.push('node');
   256         });
   255         });
   257         jQuery(CubicWeb).bind('server-response', function() {
   256         jQuery(CubicWeb).bind('server-response', function() {
   264                     // check that server-response event on CubicWeb is triggered
   263                     // check that server-response event on CubicWeb is triggered
   265                     // only once and event server-response on node is triggered
   264                     // only once and event server-response on node is triggered
   266                     assert.equal(nodes[0], 'CubicWeb');
   265                     assert.equal(nodes[0], 'CubicWeb');
   267                     assert.equal(nodes[1], 'node');
   266                     assert.equal(nodes[1], 'node');
   268                 } finally {
   267                 } finally {
   269                     start();
   268                     done();
   270                 };
   269                 };
   271             }
   270             }
   272         );
   271         );
   273     });
   272     });
   274 });
   273 });