From 1e3c76f41d9b74a25f8f2449efcd791e36f5d744 Mon Sep 17 00:00:00 2001
From: Xavier Morel <xmo@odoo.com>
Date: Mon, 2 Oct 2017 16:09:13 +0200
Subject: [PATCH] [IMP] web: add web tests to regular JS suite

Remove old tests page as it was obviously unmaintained and did not run
at all...
---
 addons/web/static/lib/qweb/qweb-test.js.html | 73 -------------------
 addons/web/static/lib/qweb/tests.js          | 77 ++++++++++++++++++++
 addons/web/views/webclient_templates.xml     |  3 +
 3 files changed, 80 insertions(+), 73 deletions(-)
 delete mode 100644 addons/web/static/lib/qweb/qweb-test.js.html
 create mode 100644 addons/web/static/lib/qweb/tests.js

diff --git a/addons/web/static/lib/qweb/qweb-test.js.html b/addons/web/static/lib/qweb/qweb-test.js.html
deleted file mode 100644
index 0ee5f27c3bdb..000000000000
--- a/addons/web/static/lib/qweb/qweb-test.js.html
+++ /dev/null
@@ -1,73 +0,0 @@
-<!doctype html>
-<html>
-<head>
-    <script src="/web/static/lib/jquery/jquery.js"></script>
-    <link rel="stylesheet" href="/web/static/lib/qunit/qunit.css" type="text/css" media="screen"/>
-    <script type="text/javascript" src="/web/static/lib/qunit/qunit.js"></script>
-
-    <script type="text/javascript" src="qweb2.js"></script>
-
-    <script>
-        QWeb = new QWeb2.Engine();
-        function trim(s) {
-            return s.replace(/(^\s+|\s+$)/g, '');
-        }
-        function render(template, context) {
-            return trim(QWeb.render(template, context)).toLowerCase();
-        }
-
-        /**
-         * Loads the template file, and executes all the test template in a
-         * qunit module $title
-         */
-        function test(title, template) {
-            QUnit.module(title, {
-                setup: function () {
-                    var self = this;
-                    this.qweb = new QWeb2.Engine();
-                    QUnit.stop();
-                    this.qweb.add_template(template, function (_, doc) {
-                        self.doc = doc;
-                        QUnit.start();
-                    })
-                }
-            });
-            QUnit.test('autotest', function (assert) {
-                var templates = this.qweb.templates;
-                for (var template in templates) {
-                    if (!templates.hasOwnProperty(template)) { continue; }
-                    // ignore templates whose name starts with _, they're
-                    // helpers/internal
-                    if (/^_/.test(template)) { continue; }
-
-                    var params = this.doc.querySelector('params#' + template);
-                    var args = params ? JSON.parse(params.textContent) : {};
-
-                    var results = this.doc.querySelector('result#' + template);
-                    assert.equal(
-                        trim(this.qweb.render(template, args)),
-                        trim(results.textContent),
-                        template);
-                }
-            });
-        }
-        $(document).ready(function() {
-            test("Output", 'qweb-test-output.xml');
-            test("Context-setting", 'qweb-test-set.xml');
-            test("Conditionals", 'qweb-test-conditionals.xml');
-            test("Attributes manipulation", 'qweb-test-attributes.xml');
-            test("Template calling (to the faraway pages)",
-                 'qweb-test-call.xml');
-            test("Foreach", 'qweb-test-foreach.xml');
-            test("Global", 'qweb-test-global.xml');
-
-            test('Template inheritance', 'qweb-test-extend.xml');
-        });
-    </script>
-
-</head>
-<body>
-  <div id="qunit"></div>
-  <div id="qunit-fixture"></div>
-</body>
-</html>
diff --git a/addons/web/static/lib/qweb/tests.js b/addons/web/static/lib/qweb/tests.js
new file mode 100644
index 000000000000..5b961acd427b
--- /dev/null
+++ b/addons/web/static/lib/qweb/tests.js
@@ -0,0 +1,77 @@
+odoo.define('qweb.tests', function () {
+'use strict';
+
+function trim(s) {
+    return s.replace(/(^\s+|\s+$)/g, '');
+}
+
+/**
+ * Generates a QUnit.module hook object loading the specified test file
+ * (from /web/static/lib/qweb) and setting ``this.qweb`` (the qweb
+ * instance for this module) and ``this.doc`` (the loaded XML file).
+ *
+ * Note that test files mix template elements <t t-name>, params elements
+ * <params> and result elements <result>. A result and an optional params
+ * object are linked to the corresponding template via the ``id``
+ * attribute (result and params have the template name as id).
+ */
+function hooks(testfile) {
+    var template = '/web/static/lib/qweb/' + testfile;
+    return {
+        before: function () {
+            var self = this;
+            this.qweb = new QWeb2.Engine();
+            var p = $.Deferred();
+            this.qweb.add_template(template, function (_, doc) {
+                self.doc = doc;
+                p.resolve(doc);
+            });
+            return p.promise();
+        }
+    }
+}
+// can't get generative QUnit.test (e.g. QUnit.test in a for(;;)
+// or Array#forEach() loop) to work, so each test file has a single test,
+// that seems to work
+function runtest() {
+    QUnit.test('', function (assert) {
+    var templates = this.qweb.templates;
+    assert.expect(_.reduce(templates, function (acc, _, k) {
+        return acc + (/^_/.test(k) ? 0 : 1);
+    }, 0));
+    for (var template in templates) {
+        if (!templates.hasOwnProperty(template)) { continue; }
+        // ignore templates whose name starts with _, they're
+        // helpers/internal
+        if (/^_/.test(template)) { continue; }
+
+        var params = this.doc.querySelector('params#' + template);
+        var args = params ? JSON.parse(params.textContent) : {};
+
+        var results = this.doc.querySelector('result#' + template);
+        assert.equal(
+            trim(this.qweb.render(template, args)),
+            trim(results.textContent),
+            template);
+        }
+    });
+}
+
+var TEMPLATES = [
+    ["Output", 'qweb-test-output.xml'],
+    ["Context-setting", 'qweb-test-set.xml'],
+    ["Conditionals", 'qweb-test-conditionals.xml'],
+    ["Attributes manipulation", 'qweb-test-attributes.xml'],
+    ["Template calling (to the faraway pages)", 'qweb-test-call.xml'],
+    ["Foreach", 'qweb-test-foreach.xml'],
+    ["Global", 'qweb-test-global.xml'],
+    ['Template inheritance', 'qweb-test-extend.xml']
+];
+
+QUnit.module('qweb', {}, function () {
+    TEMPLATES.forEach(function (it) {
+        QUnit.module(it[0], hooks(it[1]), runtest);
+    })
+});
+
+});
diff --git a/addons/web/views/webclient_templates.xml b/addons/web/views/webclient_templates.xml
index 4f69ed286b33..605003443d07 100644
--- a/addons/web/views/webclient_templates.xml
+++ b/addons/web/views/webclient_templates.xml
@@ -448,6 +448,9 @@
                         display: inherit;
                     }
                 </style>
+
+                <script type="text/javascript" src="/web/static/lib/qweb/tests.js"></script>
+
                 <script type="text/javascript" src="/web/static/tests/helpers/test_utils.js"></script>
                 <script type="text/javascript" src="/web/static/tests/helpers/mock_server.js"></script>
 
-- 
GitLab