summaryrefslogtreecommitdiff
path: root/addons/web/static/tests/control_panel/control_panel_model_extension_tests.js
blob: 01ab0be43fcb96b77562b62e83589cddb6ef2c12 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
odoo.define("web/static/tests/control_panel/control_panel_model_extension_tests.js", function (require) {
    "use strict";

    const ActionModel = require("web/static/src/js/views/action_model.js");
    const makeTestEnvironment = require('web.test_env');

    function createModel(params = {}) {
        const archs = (params.arch && { search: params.arch, }) || {};
        const { ControlPanel: controlPanelInfo, } = ActionModel.extractArchInfo(archs);
        const extensions = {
            ControlPanel: {
                context: params.context,
                archNodes: controlPanelInfo.children,
                dynamicFilters: params.dynamicFilters,
                favoriteFilters: params.favoriteFilters,
                env: makeTestEnvironment(),
                fields: params.fields,
            },
        };
        const model = new ActionModel(extensions);
        return model;
    }
    function sanitizeFilters(model) {
        const cpme = model.extensions[0].find(
            (ext) => ext.constructor.name === "ControlPanelModelExtension"
        );
        const filters = Object.values(cpme.state.filters);
        return filters.map(filter => {
            const copy = Object.assign({}, filter);
            delete copy.groupId;
            delete copy.groupNumber;
            delete copy.id;
            return copy;
        });
    }

    QUnit.module('ControlPanelModelExtension', {
        beforeEach() {
            this.fields = {
                display_name: { string: "Displayed name", type: 'char' },
                foo: { string: "Foo", type: "char", default: "My little Foo Value", store: true, sortable: true },
                date_field: { string: "Date", type: "date", store: true, sortable: true },
                float_field: { string: "Float", type: "float" },
                bar: { string: "Bar", type: "many2one", relation: 'partner' },
            };
        }
    }, function () {
        QUnit.module('Arch parsing');

        QUnit.test('empty arch', async function (assert) {
            assert.expect(1);
            const model = createModel();
            assert.deepEqual(sanitizeFilters(model), []);
        });

        QUnit.test('one field tag', async function (assert) {
            assert.expect(1);
            const arch = `
                <search>
                    <field name="bar"/>
                </search>`;
            const fields = this.fields;
            const model = createModel({ arch, fields, });
            assert.deepEqual(sanitizeFilters(model), [
                {
                    description: "Bar",
                    fieldName: "bar",
                    fieldType: "many2one",
                    type: "field"
                },
            ]);
        });

        QUnit.test('one separator tag', async function (assert) {
            assert.expect(1);
            const arch = `
                <search>
                    <separator/>
                </search>`;
            const fields = this.fields;
            const model = createModel({ arch, fields, });
            assert.deepEqual(sanitizeFilters(model), []);
        });

        QUnit.test('one separator tag and one field tag', async function (assert) {
            assert.expect(1);
            const arch = `
                <search>
                    <separator/>
                    <field name="bar"/>
                </search>`;
            const fields = this.fields;
            const model = createModel({ arch, fields, });
            assert.deepEqual(sanitizeFilters(model), [
                {
                    description: "Bar",
                    fieldName: "bar",
                    fieldType: "many2one",
                    type: "field"
                },
            ]);
        });

        QUnit.test('one filter tag', async function (assert) {
            assert.expect(1);
            const arch = `
                <search>
                    <filter name="filter" string="Hello" domain="[]"/>
                </search>`;
            const fields = this.fields;
            const model = createModel({ arch, fields, });
            assert.deepEqual(sanitizeFilters(model), [
                {
                    description: "Hello",
                    domain: "[]",
                    type: "filter",
                },
            ]);
        });

        QUnit.test('one filter tag with date attribute', async function (assert) {
            assert.expect(1);
            const arch = `
                <search>
                    <filter name="date_filter" string="Date" date="date_field"/>
                </search>`;
            const fields = this.fields;
            const model = createModel({ arch, fields, });
            const dateFilterId = Object.values(model.get('filters'))[0].id;
            assert.deepEqual(sanitizeFilters(model), [
                {
                    defaultOptionId: "this_month",
                    description: "Date",
                    fieldName: "date_field",
                    fieldType: "date",
                    isDateFilter: true,
                    hasOptions: true,
                    type: "filter"
                  },
                  {
                    comparisonOptionId: "previous_period",
                    dateFilterId,
                    description: "Date: Previous Period",
                    type: "comparison"
                  },
                  {
                    comparisonOptionId: "previous_year",
                    dateFilterId,
                    description: "Date: Previous Year",
                    type: "comparison"
                  }
            ]);
        });

        QUnit.test('one groupBy tag', async function (assert) {
            assert.expect(1);
            const arch = `
                <search>
                    <filter name="groupby" string="Hi" context="{ 'group_by': 'date_field:day'}"/>
                </search>`;
            const fields = this.fields;
            const model = createModel({ arch, fields, });
            assert.deepEqual(sanitizeFilters(model), [
                {
                    defaultOptionId: "day",
                    description: "Hi",
                    fieldName: "date_field",
                    fieldType: "date",
                    hasOptions: true,
                    type: "groupBy",
                },
            ]);
        });

        QUnit.test('two filter tags', async function (assert) {
            assert.expect(1);
            const arch = `
                <search>
                    <filter name="filter_1" string="Hello One" domain="[]"/>
                    <filter name="filter_2" string="Hello Two" domain="[('bar', '=', 3)]"/>
                </search>`;
            const fields = this.fields;
            const model = createModel({ arch, fields, });
            assert.deepEqual(sanitizeFilters(model), [
                {
                    description: "Hello One",
                    domain: "[]",
                    type: "filter",
                },
                {
                    description: "Hello Two",
                    domain: "[('bar', '=', 3)]",
                    type: "filter",
                },
            ]);
        });

        QUnit.test('two filter tags separated by a separator', async function (assert) {
            assert.expect(1);
            const arch = `
                <search>
                    <filter name="filter_1" string="Hello One" domain="[]"/>
                    <separator/>
                    <filter name="filter_2" string="Hello Two" domain="[('bar', '=', 3)]"/>
                </search>`;
            const fields = this.fields;
            const model = createModel({ arch, fields, });
            assert.deepEqual(sanitizeFilters(model), [
                {
                    description: "Hello One",
                    domain: "[]",
                    type: "filter",
                },
                {
                    description: "Hello Two",
                    domain: "[('bar', '=', 3)]",
                    type: "filter",
                },
            ]);
        });

        QUnit.test('one filter tag and one field', async function (assert) {
            assert.expect(1);
            const arch = `
                <search>
                    <filter name="filter" string="Hello" domain="[]"/>
                    <field name="bar"/>
                </search>`;
            const fields = this.fields;
            const model = createModel({ arch, fields, });
            assert.deepEqual(sanitizeFilters(model), [
                {
                    description: "Hello",
                    domain: "[]",
                    type: "filter",
                },
                {
                    description: "Bar",
                    fieldName: "bar",
                    fieldType: "many2one",
                    type: "field",
                },
            ]);
        });

        QUnit.test('two field tags', async function (assert) {
            assert.expect(1);
            const arch = `
                <search>
                    <field name="foo"/>
                    <field name="bar"/>
                </search>`;
            const fields = this.fields;
            const model = createModel({ arch, fields, });
            assert.deepEqual(sanitizeFilters(model), [
                {
                    description: "Foo",
                    fieldName: "foo",
                    fieldType: "char",
                    type: "field"
                },
                {
                    description: "Bar",
                    fieldName: "bar",
                    fieldType: "many2one",
                    type: "field"
                },
            ]);
        });

        QUnit.module('Preparing initial state');

        QUnit.test('process favorite filters', async function (assert) {
            assert.expect(1);
            const favoriteFilters = [{
                user_id: [2, "Mitchell Admin"],
                name: 'Sorted filter',
                id: 5,
                context: {
                    group_by: ['foo', 'bar']
                },
                sort: '["foo", "-bar"]',
                domain: "[('user_id', '=', uid)]",
            }];

            const model = createModel({ favoriteFilters });
            assert.deepEqual(sanitizeFilters(model), [
                {
                    context: {},
                    description: "Sorted filter",
                    domain: "[('user_id', '=', uid)]",
                    groupBys: ['foo', 'bar'],
                    orderedBy: [
                        {
                            asc: true,
                            name: "foo"
                        },
                        {
                            asc: false,
                            name: "bar"
                        }
                    ],
                    removable: true,
                    serverSideId: 5,
                    type: "favorite",
                    userId: 2
                },
            ]);

        });

        QUnit.test('process dynamic filters', async function (assert) {
            assert.expect(1);
            const dynamicFilters = [{
                description: 'Quick search',
                domain: [['id', 'in', [1, 3, 4]]]
            }];

            const model = createModel({ dynamicFilters });
            assert.deepEqual(sanitizeFilters(model), [
                {
                    description: 'Quick search',
                    domain: "[[\"id\",\"in\",[1,3,4]]]",
                    isDefault: true,
                    type: 'filter'
                },
            ]);

        });

        QUnit.test('falsy search defaults are not activated', async function (assert) {
            assert.expect(1);

            const context = {
                search_default_filter: false,
                search_default_bar: 0,
                search_default_groupby: 2,
            };
            const arch = `
                <search>
                    <filter name="filter" string="Hello" domain="[]"/>
                    <filter name="groupby" string="Goodbye" context="{'group_by': 'foo'}"/>
                    <field name="bar"/>
                </search>`;
            const fields = this.fields;
            const model = createModel({ arch, fields, context });
            // only the truthy filter 'groupby' has isDefault true
            assert.deepEqual(sanitizeFilters(model), [
                {
                    description: 'Hello',
                    domain: "[]",
                    type: 'filter',
                },
                {
                    description: 'Bar',
                    fieldName: 'bar',
                    fieldType: 'many2one',
                    type: 'field',
                },
                {
                    defaultRank: 2,
                    description: 'Goodbye',
                    fieldName: 'foo',
                    fieldType: 'char',
                    isDefault: true,
                    type: 'groupBy',
                },
            ]);

        });

        QUnit.test('search defaults on X2M fields', async function (assert) {
            assert.expect(1);

            const context = {
                search_default_otom: [1, 2],
                search_default_mtom: [1, 2]
            };
            const fields = this.fields;
            fields.otom = { string: "O2M", type: "one2many", relation: 'partner' };
            fields.mtom = { string: "M2M", type: "many2many", relation: 'partner' };
            const arch = `
                <search>
                    <field name="otom"/>
                    <field name="mtom"/>
                </search>`;
            const model = createModel({ arch, fields, context });
            assert.deepEqual(sanitizeFilters(model), [
                {
                    "defaultAutocompleteValue": {
                      "label": [1, 2],
                      "operator": "ilike",
                      "value": [1, 2]
                    },
                    "defaultRank": -10,
                    "description": "O2M",
                    "fieldName": "otom",
                    "fieldType": "one2many",
                    "isDefault": true,
                    "type": "field"
                },
                {
                    "defaultAutocompleteValue": {
                      "label": [1, 2],
                      "operator": "ilike",
                      "value": [1, 2]
                    },
                    "defaultRank": -10,
                    "description": "M2M",
                    "fieldName": "mtom",
                    "fieldType": "many2many",
                    "isDefault": true,
                    "type": "field"
                }
            ]);

        });

    });
});