Skip to content
Snippets Groups Projects
Commit 1e3c76f4 authored by Xavier Morel's avatar Xavier Morel
Browse files

[IMP] web: add web tests to regular JS suite

Remove old tests page as it was obviously unmaintained and did not run
at all...
parent 6a291815
No related branches found
No related tags found
No related merge requests found
<!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>
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);
})
});
});
...@@ -448,6 +448,9 @@ ...@@ -448,6 +448,9 @@
display: inherit; display: inherit;
} }
</style> </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/test_utils.js"></script>
<script type="text/javascript" src="/web/static/tests/helpers/mock_server.js"></script> <script type="text/javascript" src="/web/static/tests/helpers/mock_server.js"></script>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment