summaryrefslogtreecommitdiff
path: root/addons/web/static/tests/qweb_tests.js
diff options
context:
space:
mode:
authorChristophe Matthieu <chm@odoo.com>2018-07-31 01:04:30 +0200
committerChristophe Matthieu <chm@odoo.com>2018-08-13 17:09:28 +0200
commit8f7fdb0d99bb99f122b7a5be6795ebb174c27bc6 (patch)
treeb064e6f84076fd97d9f312c8b69822e49bbe7445 /addons/web/static/tests/qweb_tests.js
parent61eef73b52c078daaae37f8d57e93830a2e53410 (diff)
[FIX] web: fix JavaScript qweb and activate tests
Fix the issue of the t-esc display the content before the tag. Activate qweb qunit test (same test for the JavaScript and python) Use the same behavior as python: keep attributes order, and display content as default value when the variable is falsy.
Diffstat (limited to 'addons/web/static/tests/qweb_tests.js')
-rw-r--r--addons/web/static/tests/qweb_tests.js97
1 files changed, 97 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 00000000000..6e40dd1b1cb
--- /dev/null
+++ b/addons/web/static/tests/qweb_tests.js
@@ -0,0 +1,97 @@
+odoo.define('web.qweb', 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 def = $.Deferred();
+ var qweb = new window.QWeb2.Engine();
+
+ qweb.add_template(qwebPath + template, function (error, doc) {
+ if (error) {
+ return $.Deferred().reject(error);
+ }
+ def.resolve({
+ qweb: qweb,
+ doc: doc
+ });
+ });
+
+ def.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),
+ template);
+ } catch (error) {
+ assert.notOk(error.stack || error, 'Rendering error');
+ }
+ }
+ done();
+ });
+ return def;
+}
+
+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';}})
+ .always(function () {
+ window.QWeb2.WORD_REPLACEMENT = WORD_REPLACEMENT;
+ });
+ });
+ QUnit.test('Template inheritance', function (assert) {
+ loadTest(assert, 'qweb-test-extend.xml');
+ });
+});
+});