summaryrefslogtreecommitdiff
path: root/addons/web/static/tests/views/abstract_view_tests.js
blob: b2e0cee0dcdd5988553a6f639af0877a8e30630b (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
odoo.define('web.abstract_view_tests', function (require) {
"use strict";

var AbstractView = require('web.AbstractView');
var ajax = require('web.ajax');
var testUtils = require('web.test_utils');

var createActionManager = testUtils.createActionManager;
var createView = testUtils.createView;

QUnit.module('Views', {
    beforeEach: function () {
        this.data = {
            fake_model: {
                fields: {},
                record: [],
            },
            foo: {
                fields: {
                    foo: {string: "Foo", type: "char"},
                    bar: {string: "Bar", type: "boolean"},
                },
                records: [
                    {id: 1, bar: true, foo: "yop"},
                    {id: 2, bar: true, foo: "blip"},
                ]
            },
        };
    },
}, function () {

    QUnit.module('AbstractView');

    QUnit.test('lazy loading of js libs (in parallel)', async function (assert) {
        var done = assert.async();
        assert.expect(6);

        var prom = testUtils.makeTestPromise();
        var loadJS = ajax.loadJS;
        ajax.loadJS = function (url) {
            assert.step(url);
            return prom.then(function () {
                assert.step(url + ' loaded');
            });
        };

        var View = AbstractView.extend({
            jsLibs: [['a', 'b']],
        });
        createView({
            View: View,
            arch: '<fake/>',
            data: this.data,
            model: 'fake_model',
        }).then(function (view) {
            assert.verifySteps(['a loaded', 'b loaded'],
                "should wait for both libs to be loaded");
            ajax.loadJS = loadJS;
            view.destroy();
            done();
        });

        await testUtils.nextTick();
        assert.verifySteps(['a', 'b'], "both libs should be loaded in parallel");
        prom.resolve();
    });

    QUnit.test('lazy loading of js libs (sequentially)', async function (assert) {
        var done = assert.async();
        assert.expect(10);

        var proms = {
            a:  testUtils.makeTestPromise(),
            b:  testUtils.makeTestPromise(),
            c:  testUtils.makeTestPromise(),
        };
        var loadJS = ajax.loadJS;
        ajax.loadJS = function (url) {
            assert.step(url);
            return proms[url].then(function () {
                assert.step(url + ' loaded');
            });
        };

        var View = AbstractView.extend({
            jsLibs: [
                ['a', 'b'],
                'c',
            ],
        });
        createView({
            View: View,
            arch: '<fake/>',
            data: this.data,
            model: 'fake_model',
        }).then(function (view) {
            assert.verifySteps(['c loaded'], "should wait for all libs to be loaded");
            ajax.loadJS = loadJS;
            view.destroy();
            done();
        });
        await testUtils.nextTick();
        assert.verifySteps(['a', 'b'], "libs 'a' and 'b' should be loaded in parallel");
        await proms.b.resolve();
        await testUtils.nextTick();
        assert.verifySteps(['b loaded'], "should wait for 'a' and 'b' to be loaded before loading 'c'");
        await proms.a.resolve();
        await testUtils.nextTick();
        assert.verifySteps(['a loaded', 'c'], "should load 'c' when 'a' and 'b' are loaded");
        await proms.c.resolve();
    });

    QUnit.test('group_by from context can be a string, instead of a list of strings', async function (assert) {
        assert.expect(1);

        var actionManager = await createActionManager({
            actions: [{
                id: 1,
                name: 'Foo',
                res_model: 'foo',
                type: 'ir.actions.act_window',
                views: [[false, 'list']],
                context: {
                    group_by: 'bar',
                },
            }],
            archs: {
                'foo,false,list': '<tree><field name="foo"/><field name="bar"/></tree>',
                'foo,false,search': '<search></search>',
            },
            data: this.data,
            mockRPC: function (route, args) {
                if (args.method === 'web_read_group') {
                    assert.deepEqual(args.kwargs.groupby, ['bar']);
                }
                return this._super.apply(this, arguments);
            },
        });

        await actionManager.doAction(1);

        actionManager.destroy();
    });

});
});