[test] fix test_ajax js tests. We have to mock some of our js functions / variables (e.g. _ or pageid) in the test environment
authorAdrien Di Mascio <Adrien.DiMascio@logilab.fr>
Thu, 08 Jul 2010 19:18:56 +0200
changeset 5950 f84dba9b8eca
parent 5949 2a273c896a38
child 5951 6026582ae4f1
[test] fix test_ajax js tests. We have to mock some of our js functions / variables (e.g. _ or pageid) in the test environment
devtools/data/cwmock.js
devtools/qunit.py
web/test/jstests/test_ajax.html
web/test/jstests/test_ajax.js
web/test/jstests/test_htmlhelpers.html
web/test/jstests/test_utils.html
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/devtools/data/cwmock.js	Thu Jul 08 19:18:56 2010 +0200
@@ -0,0 +1,11 @@
+/*
+ * cubicweb js mock for unit tests
+ * This module defines variables and functions used in quite a few places
+ * in cw js framework that can't be used or guessed without a real CW server
+ */
+
+var pageid = 'my-page-id';
+
+function _(message) {
+    return message;
+}
--- a/devtools/qunit.py	Thu Jul 08 18:59:42 2010 +0200
+++ b/devtools/qunit.py	Thu Jul 08 19:18:56 2010 +0200
@@ -273,6 +273,7 @@
     <!-- JS lib used as testing framework -->
     <link rel="stylesheet" type="text/css" media="all" href="%(web_test)s/qunit.css" />
     <script src="%(web_data)s/jquery.js" type="text/javascript"></script>
+    <script src="%(web_test)s/cwmock.js" type="text/javascript"></script>
     <script src="%(web_test)s/qunit.js" type="text/javascript"></script>'''
     % data]
     if server_data is not None:
--- a/web/test/jstests/test_ajax.html	Thu Jul 08 18:59:42 2010 +0200
+++ b/web/test/jstests/test_ajax.html	Thu Jul 08 19:18:56 2010 +0200
@@ -9,9 +9,10 @@
     <script src="../../data/cubicweb.htmlhelpers.js" type="text/javascript"></script>
     <script src="../../data/cubicweb.ajax.js" type="text/javascript"></script>
     <!-- qunit files -->
-    <script type="text/javascript" src="qunit.js"></script>
-    <link rel="stylesheet" type="text/css" media="all" href="qunit.css" />
+    <script type="text/javascript" src="../../../devtools/data/qunit.js"></script>
+    <link rel="stylesheet" type="text/css" media="all" href="../../../devtools/data/qunit.css" />
     <!-- test suite -->
+    <script src="cwmock.js" type="text/javascript"></script>
     <script src="test_ajax.js" type="text/javascript"></script>
   </head>
   <body>
--- a/web/test/jstests/test_ajax.js	Thu Jul 08 18:59:42 2010 +0200
+++ b/web/test/jstests/test_ajax.js	Thu Jul 08 19:18:56 2010 +0200
@@ -1,5 +1,16 @@
 $(document).ready(function() {
 
+    module("ajax", {
+        setup: function() {
+          this.scriptsLength = $('head script[src]').length-1;
+	  this.cssLength = $('head link[rel=stylesheet]').length-1;
+        },
+        teardown: function() {
+          $('head script[src]:gt(' + this.scriptsLength + ')').remove();
+          $('head link[rel=stylesheet]:gt(' + this.cssLength + ')').remove();
+        }
+      });
+
     function jsSources() {
         return $.map($('head script[src]'), function(script) {
             return script.getAttribute('src');
@@ -161,18 +172,21 @@
             callback: function() {
                 var origLength = scriptsIncluded.length;
                 scriptsIncluded = jsSources();
-                // check that foo.js has been inserted in <head>
-                equals(scriptsIncluded.length, origLength + 1);
-                equals(scriptsIncluded[origLength].indexOf('http://foo.js'), 0);
-                // check that <div class="ajaxHtmlHead"> has been removed
-                equals(jQuery('#main').children().length, 1);
-                equals(jQuery('div.ajaxHtmlHead').length, 0);
-                equals(jQuery('#main h1').html(), 'Hello');
-                // qunit.css is not added twice
-                equals(jQuery('head link').length, 1);
-                /* use endswith because in pytest context we have an absolute path */
-                ok(jQuery('head link').attr('href').endswith('/qunit.css'));
-                start();
+		try {
+                    // check that foo.js has been inserted in <head>
+                    equals(scriptsIncluded.length, origLength + 1);
+                    equals(scriptsIncluded[origLength].indexOf('http://foo.js'), 0);
+                    // check that <div class="ajaxHtmlHead"> has been removed
+                    equals(jQuery('#main').children().length, 1);
+                    equals(jQuery('div.ajaxHtmlHead').length, 0);
+                    equals(jQuery('#main h1').html(), 'Hello');
+                    // qunit.css is not added twice
+                    equals(jQuery('head link').length, 1);
+                    /* use endswith because in pytest context we have an absolute path */
+                    ok(jQuery('head link').attr('href').endswith('/qunit.css'));
+                } finally {
+                    start();
+		}
             }
         });
     });
--- a/web/test/jstests/test_htmlhelpers.html	Thu Jul 08 18:59:42 2010 +0200
+++ b/web/test/jstests/test_htmlhelpers.html	Thu Jul 08 19:18:56 2010 +0200
@@ -7,6 +7,7 @@
     <script src="../../data/cubicweb.htmlhelpers.js" type="text/javascript"></script>
     <script type="text/javascript" src="qunit.js"></script>
     <link rel="stylesheet" type="text/css" media="all" href="qunit.css" />
+    <script src="cwmock.js" type="text/javascript"></script>
     <script src="test_htmlhelpers.js" type="text/javascript"></script>
   </head>
   <body>
--- a/web/test/jstests/test_utils.html	Thu Jul 08 18:59:42 2010 +0200
+++ b/web/test/jstests/test_utils.html	Thu Jul 08 19:18:56 2010 +0200
@@ -8,6 +8,7 @@
     <script src="utils.js" type="text/javascript"></script>
     <script type="text/javascript" src="qunit.js"></script>
     <link rel="stylesheet" type="text/css" media="all" href="qunit.css" />
+    <script src="cwmock.js" type="text/javascript"></script>
     <script src="test_utils.js" type="text/javascript"></script>
   </head>
   <body>