summaryrefslogtreecommitdiff
path: root/addons/web/static/tests/qweb_tests.js
blob: fda72e2392b43bae2a9c4ffe27c2510b9669fcc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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');
    });
});
});