web/test/jstests/test_ajax.js
changeset 10429 98ea2b865210
parent 10428 c961a301a90b
child 10430 2b1ea3e8e080
equal deleted inserted replaced
10428:c961a301a90b 10429:98ea2b865210
    20         return $.map($('head script[src]'), function(script) {
    20         return $.map($('head script[src]'), function(script) {
    21             return script.getAttribute('src');
    21             return script.getAttribute('src');
    22         });
    22         });
    23     }
    23     }
    24 
    24 
    25     QUnit.test('test simple h1 inclusion (ajax_url0.html)', function() {
    25     QUnit.test('test simple h1 inclusion (ajax_url0.html)', function (assert) {
    26         expect(3);
    26         assert.expect(3);
    27         equal(jQuery('#qunit-fixture').children().length, 0);
    27         assert.equal(jQuery('#qunit-fixture').children().length, 0);
    28         stop();
    28         stop();
    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                     equal(jQuery('#qunit-fixture').children().length, 1);
    32                     assert.equal(jQuery('#qunit-fixture').children().length, 1);
    33                     equal(jQuery('#qunit-fixture h1').html(), 'Hello');
    33                     assert.equal(jQuery('#qunit-fixture h1').html(), 'Hello');
    34                 } finally {
    34                 } finally {
    35                     start();
    35                     start();
    36                 };
    36                 };
    37             }
    37             }
    38         );
    38         );
    39     });
    39     });
    40 
    40 
    41     QUnit.test('test simple html head inclusion (ajax_url1.html)', function() {
    41     QUnit.test('test simple html head inclusion (ajax_url1.html)', function (assert) {
    42         expect(6);
    42         assert.expect(6);
    43         var scriptsIncluded = jsSources();
    43         var scriptsIncluded = jsSources();
    44         equal(jQuery.inArray('http://foo.js', scriptsIncluded), - 1);
    44         assert.equal(jQuery.inArray('http://foo.js', scriptsIncluded), - 1);
    45         stop();
    45         stop();
    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();
    51                     // check that foo.js has been prepended to <head>
    51                     // check that foo.js has been prepended to <head>
    52                     equal(scriptsIncluded.length, origLength + 1);
    52                     assert.equal(scriptsIncluded.length, origLength + 1);
    53                     equal(scriptsIncluded.indexOf('http://foo.js'), 0);
    53                     assert.equal(scriptsIncluded.indexOf('http://foo.js'), 0);
    54                     // check that <div class="ajaxHtmlHead"> has been removed
    54                     // check that <div class="ajaxHtmlHead"> has been removed
    55                     equal(jQuery('#qunit-fixture').children().length, 1);
    55                     assert.equal(jQuery('#qunit-fixture').children().length, 1);
    56                     equal(jQuery('div.ajaxHtmlHead').length, 0);
    56                     assert.equal(jQuery('div.ajaxHtmlHead').length, 0);
    57                     equal(jQuery('#qunit-fixture h1').html(), 'Hello');
    57                     assert.equal(jQuery('#qunit-fixture h1').html(), 'Hello');
    58                 } finally {
    58                 } finally {
    59                     start();
    59                     start();
    60                 };
    60                 };
    61             }
    61             }
    62         );
    62         );
    63     });
    63     });
    64 
    64 
    65     QUnit.test('test addCallback', function() {
    65     QUnit.test('test addCallback', function (assert) {
    66         expect(3);
    66         assert.expect(3);
    67         equal(jQuery('#qunit-fixture').children().length, 0);
    67         assert.equal(jQuery('#qunit-fixture').children().length, 0);
    68         stop();
    68         stop();
    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                 equal(jQuery('#qunit-fixture').children().length, 1);
    72                 assert.equal(jQuery('#qunit-fixture').children().length, 1);
    73                 equal(jQuery('#qunit-fixture h1').html(), 'Hello');
    73                 assert.equal(jQuery('#qunit-fixture h1').html(), 'Hello');
    74             } finally {
    74             } finally {
    75                 start();
    75                 start();
    76             };
    76             };
    77         });
    77         });
    78     });
    78     });
    79 
    79 
    80     QUnit.test('test callback after synchronous request', function() {
    80     QUnit.test('test callback after synchronous request', function (assert) {
    81         expect(1);
    81         assert.expect(1);
    82         var deferred = new Deferred();
    82         var deferred = new Deferred();
    83         var result = jQuery.ajax({
    83         var result = jQuery.ajax({
    84             url: BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url0.html',
    84             url: BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url0.html',
    85             async: false,
    85             async: false,
    86             beforeSend: function(xhr) {
    86             beforeSend: function(xhr) {
    92         });
    92         });
    93         stop();
    93         stop();
    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                 ok(true, "callback is executed");
    97                 assert.ok(true, "callback is executed");
    98             } finally {
    98             } finally {
    99                 start();
    99                 start();
   100             };
   100             };
   101         });
   101         });
   102     });
   102     });
   103 
   103 
   104     QUnit.test('test addCallback with parameters', function() {
   104     QUnit.test('test addCallback with parameters', function (assert) {
   105         expect(3);
   105         assert.expect(3);
   106         equal(jQuery('#qunit-fixture').children().length, 0);
   106         assert.equal(jQuery('#qunit-fixture').children().length, 0);
   107         stop();
   107         stop();
   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                 equal(arg1, 'Hello');
   111                 assert.equal(arg1, 'Hello');
   112                 equal(arg2, 'world');
   112                 assert.equal(arg2, 'world');
   113             } finally {
   113             } finally {
   114                 start();
   114                 start();
   115             };
   115             };
   116         },
   116         },
   117         'Hello', 'world');
   117         'Hello', 'world');
   118     });
   118     });
   119 
   119 
   120     QUnit.test('test callback after synchronous request with parameters', function() {
   120     QUnit.test('test callback after synchronous request with parameters', function (assert) {
   121         expect(3);
   121         assert.expect(3);
   122         var deferred = new Deferred();
   122         var deferred = new Deferred();
   123         deferred.addCallback(function(data, req, arg1, arg2) {
   123         deferred.addCallback(function(data, req, arg1, arg2) {
   124             // add an assertion to ensure the callback is executed
   124             // add an assertion to ensure the callback is executed
   125             try {
   125             try {
   126                 ok(true, "callback is executed");
   126                 assert.ok(true, "callback is executed");
   127                 equal(arg1, 'Hello');
   127                 assert.equal(arg1, 'Hello');
   128                 equal(arg2, 'world');
   128                 assert.equal(arg2, 'world');
   129             } finally {
   129             } finally {
   130                 start();
   130                 start();
   131             };
   131             };
   132         },
   132         },
   133         'Hello', 'world');
   133         'Hello', 'world');
   150                 deferred.success(data);
   150                 deferred.success(data);
   151             }
   151             }
   152         });
   152         });
   153     });
   153     });
   154 
   154 
   155   QUnit.test('test addErrback', function() {
   155   QUnit.test('test addErrback', function (assert) {
   156         expect(1);
   156         assert.expect(1);
   157         stop();
   157         stop();
   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             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                 ok(true, "errback is executed");
   165                 assert.ok(true, "errback is executed");
   166             } finally {
   166             } finally {
   167                 start();
   167                 start();
   168             };
   168             };
   169         });
   169         });
   170     });
   170     });
   171 
   171 
   172     QUnit.test('test callback execution order', function() {
   172     QUnit.test('test callback execution order', function (assert) {
   173         expect(3);
   173         assert.expect(3);
   174         var counter = 0;
   174         var counter = 0;
   175         stop();
   175         stop();
   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                 try {
   179                     equal(++counter, 1); // should be executed first
   179                     assert.equal(++counter, 1); // should be executed first
   180                 } finally {
   180                 } finally {
   181                     start();
   181                     start();
   182                 };
   182                 };
   183             }
   183             }
   184         );
   184         );
   185         d.addCallback(function() {
   185         d.addCallback(function() {
   186             equal(++counter, 2);
   186             assert.equal(++counter, 2);
   187         });
   187         });
   188         d.addCallback(function() {
   188         d.addCallback(function() {
   189             equal(++counter, 3);
   189             assert.equal(++counter, 3);
   190         });
   190         });
   191     });
   191     });
   192 
   192 
   193     QUnit.test('test already included resources are ignored (ajax_url1.html)', function() {
   193     QUnit.test('test already included resources are ignored (ajax_url1.html)', function (assert) {
   194         expect(10);
   194         assert.expect(10);
   195         var scriptsIncluded = jsSources();
   195         var scriptsIncluded = jsSources();
   196         // NOTE:
   196         // NOTE:
   197         equal(jQuery.inArray('http://foo.js', scriptsIncluded), -1);
   197         assert.equal(jQuery.inArray('http://foo.js', scriptsIncluded), -1);
   198         equal(jQuery('head link').length, 1);
   198         assert.equal(jQuery('head link').length, 1);
   199         /* use endswith because in pytest context we have an absolute path */
   199         /* use endswith because in pytest context we have an absolute path */
   200         ok(jQuery('head link').attr('href').endswith('/qunit.css'), 'qunit.css is loaded');
   200         assert.ok(jQuery('head link').attr('href').endswith('/qunit.css'), 'qunit.css is loaded');
   201         stop();
   201         stop();
   202         jQuery('#qunit-fixture').loadxhtml(BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url1.html')
   202         jQuery('#qunit-fixture').loadxhtml(BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url1.html')
   203         .addCallback(function() {
   203         .addCallback(function() {
   204                 var origLength = scriptsIncluded.length;
   204                 var origLength = scriptsIncluded.length;
   205                 scriptsIncluded = jsSources();
   205                 scriptsIncluded = jsSources();
   206                 try {
   206                 try {
   207                     // check that foo.js has been inserted in <head>
   207                     // check that foo.js has been inserted in <head>
   208                     equal(scriptsIncluded.length, origLength + 1);
   208                     assert.equal(scriptsIncluded.length, origLength + 1);
   209                     equal(scriptsIncluded.indexOf('http://foo.js'), 0);
   209                     assert.equal(scriptsIncluded.indexOf('http://foo.js'), 0);
   210                     // check that <div class="ajaxHtmlHead"> has been removed
   210                     // check that <div class="ajaxHtmlHead"> has been removed
   211                     equal(jQuery('#qunit-fixture').children().length, 1);
   211                     assert.equal(jQuery('#qunit-fixture').children().length, 1);
   212                     equal(jQuery('div.ajaxHtmlHead').length, 0);
   212                     assert.equal(jQuery('div.ajaxHtmlHead').length, 0);
   213                     equal(jQuery('#qunit-fixture h1').html(), 'Hello');
   213                     assert.equal(jQuery('#qunit-fixture h1').html(), 'Hello');
   214                     // qunit.css is not added twice
   214                     // qunit.css is not added twice
   215                     equal(jQuery('head link').length, 1);
   215                     assert.equal(jQuery('head link').length, 1);
   216                     /* use endswith because in pytest context we have an absolute path */
   216                     /* use endswith because in pytest context we have an absolute path */
   217                     ok(jQuery('head link').attr('href').endswith('/qunit.css'), 'qunit.css is loaded');
   217                     assert.ok(jQuery('head link').attr('href').endswith('/qunit.css'), 'qunit.css is loaded');
   218                 } finally {
   218                 } finally {
   219                     start();
   219                     start();
   220                 }
   220                 }
   221             }
   221             }
   222         );
   222         );
   223     });
   223     });
   224 
   224 
   225     QUnit.test('test synchronous request loadRemote', function() {
   225     QUnit.test('test synchronous request loadRemote', function (assert) {
   226         var res = loadRemote(BASE_URL + 'cwsoftwareroot/web/test/jstests/ajaxresult.json', {},
   226         var res = loadRemote(BASE_URL + 'cwsoftwareroot/web/test/jstests/ajaxresult.json', {},
   227         'GET', true);
   227         'GET', true);
   228         deepEqual(res, ['foo', 'bar']);
   228         assert.deepEqual(res, ['foo', 'bar']);
   229     });
   229     });
   230 
   230 
   231     QUnit.test('test event on CubicWeb', function() {
   231     QUnit.test('test event on CubicWeb', function (assert) {
   232         expect(1);
   232         assert.expect(1);
   233         stop();
   233         stop();
   234         var events = null;
   234         var events = null;
   235         jQuery(CubicWeb).bind('server-response', function() {
   235         jQuery(CubicWeb).bind('server-response', function() {
   236             // check that server-response event on CubicWeb is triggered
   236             // check that server-response event on CubicWeb is triggered
   237             events = 'CubicWeb';
   237             events = 'CubicWeb';
   238         });
   238         });
   239         jQuery('#qunit-fixture').loadxhtml(BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url0.html')
   239         jQuery('#qunit-fixture').loadxhtml(BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url0.html')
   240         .addCallback(function() {
   240         .addCallback(function() {
   241                 try {
   241                 try {
   242                     equal(events, 'CubicWeb');
   242                     assert.equal(events, 'CubicWeb');
   243                 } finally {
   243                 } finally {
   244                     start();
   244                     start();
   245                 };
   245                 };
   246             }
   246             }
   247         );
   247         );
   248     });
   248     });
   249 
   249 
   250     QUnit.test('test event on node', function() {
   250     QUnit.test('test event on node', function (assert) {
   251         expect(3);
   251         assert.expect(3);
   252         stop();
   252         stop();
   253         var nodes = [];
   253         var nodes = [];
   254         jQuery('#qunit-fixture').bind('server-response', function() {
   254         jQuery('#qunit-fixture').bind('server-response', function() {
   255             nodes.push('node');
   255             nodes.push('node');
   256         });
   256         });
   258             nodes.push('CubicWeb');
   258             nodes.push('CubicWeb');
   259         });
   259         });
   260         jQuery('#qunit-fixture').loadxhtml(BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url0.html')
   260         jQuery('#qunit-fixture').loadxhtml(BASE_URL + 'cwsoftwareroot/web/test/jstests/ajax_url0.html')
   261         .addCallback(function() {
   261         .addCallback(function() {
   262                 try {
   262                 try {
   263                     equal(nodes.length, 2);
   263                     assert.equal(nodes.length, 2);
   264                     // check that server-response event on CubicWeb is triggered
   264                     // check that server-response event on CubicWeb is triggered
   265                     // only once and event server-response on node is triggered
   265                     // only once and event server-response on node is triggered
   266                     equal(nodes[0], 'CubicWeb');
   266                     assert.equal(nodes[0], 'CubicWeb');
   267                     equal(nodes[1], 'node');
   267                     assert.equal(nodes[1], 'node');
   268                 } finally {
   268                 } finally {
   269                     start();
   269                     start();
   270                 };
   270                 };
   271             }
   271             }
   272         );
   272         );