summaryrefslogtreecommitdiff
path: root/addons/web/static/tests/qweb_tests.js
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
committerstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
commit3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch)
treea44932296ef4a9b71d5f010906253d8c53727726 /addons/web/static/tests/qweb_tests.js
parent0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff)
initial commit 2
Diffstat (limited to 'addons/web/static/tests/qweb_tests.js')
-rw-r--r--addons/web/static/tests/qweb_tests.js100
1 files changed, 100 insertions, 0 deletions
diff --git a/addons/web/static/tests/qweb_tests.js b/addons/web/static/tests/qweb_tests.js
new file mode 100644
index 00000000..fda72e23
--- /dev/null
+++ b/addons/web/static/tests/qweb_tests.js
@@ -0,0 +1,100 @@
+odoo.define('web.qweb_tests', function (require) {
+"use strict";
+
+var qwebPath = '/web/static/lib/qweb/';
+
+function trim(s) {
+ return s.replace(/(^\s+|\s+$)/g, '');
+}
+
+/**
+ * Loads the template file, and executes all the test template in a
+ * qunit module $title
+ */
+function loadTest(assert, template, context) {
+ var done = assert.async();
+ assert.expect(1);
+
+ var qweb = new window.QWeb2.Engine();
+ var prom = new Promise(function (resolve, reject) {
+ qweb.add_template(qwebPath + template, function (error, doc) {
+ if (error) {
+ return reject(error);
+ }
+ resolve({
+ qweb: qweb,
+ doc: doc
+ });
+ });
+ });
+
+ prom.then(function (r) {
+ var qweb = r.qweb;
+ var doc = r.doc;
+ assert.expect(doc.querySelectorAll('result').length);
+
+ var templates = qweb.templates;
+ for (var template in templates) {
+ try {
+ if (!templates.hasOwnProperty(template)) {
+ continue;
+ }
+ // ignore templates whose name starts with _, they're
+ // helpers/internal
+ if (/^_/.test(template)) {
+ continue;
+ }
+
+ var params = doc.querySelector('params#' + template);
+ var args = params ? JSON.parse(params.textContent) : (context ? _.clone(context) : {});
+
+ var results = doc.querySelector('result#' + template);
+
+ assert.equal(
+ trim(qweb.render(template, args)),
+ trim(results.textContent.replace(new RegExp(String.fromCharCode(13), 'g'), '')),
+ template);
+ } catch (error) {
+ assert.notOk(error.stack || error, 'Rendering error');
+ }
+ }
+ done();
+ });
+ return prom;
+}
+
+QUnit.module('QWeb', {}, function () {
+ QUnit.test('Output', function (assert) {
+ loadTest(assert, 'qweb-test-output.xml');
+ });
+ QUnit.test('Context-setting', function (assert) {
+ loadTest(assert, 'qweb-test-set.xml');
+ });
+ QUnit.test('Conditionals', function (assert) {
+ loadTest(assert, 'qweb-test-conditionals.xml');
+ });
+ QUnit.test('Attributes manipulation', function (assert) {
+ loadTest(assert, 'qweb-test-attributes.xml');
+ });
+ QUnit.test('Template calling (to the faraway pages)', function (assert) {
+ loadTest(assert, 'qweb-test-call.xml', {True: true});
+ });
+ QUnit.test('Foreach', function (assert) {
+ loadTest(assert, 'qweb-test-foreach.xml');
+ });
+ QUnit.test('Global', function (assert) {
+ // test use python syntax
+ var WORD_REPLACEMENT = window.QWeb2.WORD_REPLACEMENT;
+ window.QWeb2.WORD_REPLACEMENT = _.extend({not: '!', None: 'undefined'}, WORD_REPLACEMENT);
+ loadTest(assert, 'qweb-test-global.xml', {bool: function (v) { return !!v ? 'True' : 'False';}})
+ .then(function () {
+ window.QWeb2.WORD_REPLACEMENT = WORD_REPLACEMENT;
+ }, function () {
+ window.QWeb2.WORD_REPLACEMENT = WORD_REPLACEMENT;
+ });
+ });
+ QUnit.test('Template inheritance', function (assert) {
+ loadTest(assert, 'qweb-test-extend.xml');
+ });
+});
+});