summaryrefslogtreecommitdiff
path: root/addons/mail/static/src/components/discuss/tests/discuss_domain_tests.js
blob: 2643120746772ecdf905c6a9514f41fc93251eac (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
odoo.define('mail/static/src/components/discuss/tests/discuss_domain_tests.js', function (require) {
'use strict';

const {
    afterEach,
    afterNextRender,
    beforeEach,
    start,
} = require('mail/static/src/utils/test_utils.js');

QUnit.module('mail', {}, function () {
QUnit.module('components', {}, function () {
QUnit.module('discuss', {}, function () {
QUnit.module('discuss_domain_tests.js', {
    beforeEach() {
        beforeEach(this);

        this.start = async params => {
            const { afterEvent, env, widget } = await start(Object.assign({}, params, {
                autoOpenDiscuss: true,
                data: this.data,
                hasDiscuss: true,
            }));
            this.afterEvent = afterEvent;
            this.env = env;
            this.widget = widget;
        };
    },
    afterEach() {
        afterEach(this);
    },
});

QUnit.test('discuss should filter messages based on given domain', async function (assert) {
    assert.expect(2);

    this.data['mail.message'].records.push({
        body: "test",
        needaction: true,
        needaction_partner_ids: [this.data.currentPartnerId],
    }, {
        body: "not empty",
        needaction: true,
        needaction_partner_ids: [this.data.currentPartnerId],
    });
    await this.start();
    assert.containsN(
        document.body,
        '.o_Message',
        2,
        "should have 2 messages in Inbox initially"
    );

    await this.afterEvent({
        eventName: 'o-thread-view-hint-processed',
        func: () => {
            // simulate control panel search
            this.env.messaging.discuss.update({
                stringifiedDomain: JSON.stringify([['body', 'ilike', 'test']]),
            });
        },
        message: "should wait until search filter is applied",
        predicate: ({ hint, threadViewer }) => {
            return (
                hint.type === 'messages-loaded' &&
                hint.data.fetchedMessages.length === 1 &&
                threadViewer.thread.model === 'mail.box' &&
                threadViewer.thread.id === 'inbox'
            );
        },
    });
    assert.containsOnce(
        document.body,
        '.o_Message',
        "should only have the 1 message containing 'test' remaining after doing a search"
    );
});

QUnit.test('discuss should keep filter domain on changing thread', async function (assert) {
    assert.expect(3);

    this.data['mail.channel'].records.push({ id: 20 });
    this.data['mail.message'].records.push({
        body: "test",
        channel_ids: [20],
    }, {
        body: "not empty",
        channel_ids: [20],
    });
    await this.start();
    const channel = this.env.models['mail.thread'].findFromIdentifyingData({
        id: 20,
        model: 'mail.channel',
    });
    assert.containsNone(
        document.body,
        '.o_Message',
        "should have no message in Inbox initially"
    );

    await this.afterEvent({
        eventName: 'o-thread-view-hint-processed',
        func: () => {
            // simulate control panel search
            this.env.messaging.discuss.update({
                stringifiedDomain: JSON.stringify([['body', 'ilike', 'test']]),
            });
        },
        message: "should wait until search filter is applied",
        predicate: ({ hint, threadViewer }) => {
            return (
                hint.type === 'messages-loaded' &&
                threadViewer.thread.model === 'mail.box' &&
                threadViewer.thread.id === 'inbox'
            );
        },
    });
    assert.containsNone(
        document.body,
        '.o_Message',
        "should have still no message in Inbox after doing a search"
    );

    await this.afterEvent({
        eventName: 'o-thread-view-hint-processed',
        func: () => {
            document.querySelector(`
                .o_DiscussSidebar_item[data-thread-local-id="${channel.localId}"]
            `).click();
        },
        message: "should wait until channel 20 is loaded after clicking on it",
        predicate: ({ hint, threadViewer }) => {
            return (
                hint.type === 'messages-loaded' &&
                threadViewer.thread.model === 'mail.channel' &&
                threadViewer.thread.id === 20
            );
        },
    });
    assert.containsOnce(
        document.body,
        '.o_Message',
        "should only have the 1 message containing 'test' in channel 20 (due to the domain still applied on changing thread)"
    );
});

QUnit.test('discuss should refresh filtered thread on receiving new message', async function (assert) {
    assert.expect(2);

    this.data['mail.channel'].records.push({ id: 20 });
    await this.start();
    const channel = this.env.models['mail.thread'].findFromIdentifyingData({
        id: 20,
        model: 'mail.channel',
    });
    await this.afterEvent({
        eventName: 'o-thread-view-hint-processed',
        func: () => {
            document.querySelector(`
                .o_DiscussSidebar_item[data-thread-local-id="${channel.localId}"]
            `).click();
        },
        message: "should wait until channel 20 is loaded after clicking on it",
        predicate: ({ hint, threadViewer }) => {
            return (
                hint.type === 'messages-loaded' &&
                threadViewer.thread.model === 'mail.channel' &&
                threadViewer.thread.id === 20
            );
        },
    });
    await this.afterEvent({
        eventName: 'o-thread-view-hint-processed',
        func: () => {
            // simulate control panel search
            this.env.messaging.discuss.update({
                stringifiedDomain: JSON.stringify([['body', 'ilike', 'test']]),
            });
        },
        message: "should wait until search filter is applied",
        predicate: ({ hint, threadViewer }) => {
            return (
                hint.type === 'messages-loaded' &&
                threadViewer.thread.model === 'mail.channel' &&
                threadViewer.thread.id === 20
            );
        },
    });
    assert.containsNone(
        document.body,
        '.o_Message',
        "should have initially no message in channel 20 matching the search 'test'"
    );

    // simulate receiving a message
    await this.afterEvent({
        eventName: 'o-thread-view-hint-processed',
        func: () => this.env.services.rpc({
            route: '/mail/chat_post',
            params: {
                message_content: "test",
                uuid: channel.uuid,
            },
        }),
        message: "should wait until channel 20 refreshed its filtered message list",
        predicate: data => {
            return (
                data.threadViewer.thread.model === 'mail.channel' &&
                data.threadViewer.thread.id === 20 &&
                data.hint.type === 'messages-loaded'
            );
        },
    });
    assert.containsOnce(
        document.body,
        '.o_Message',
        "should only have the 1 message containing 'test' in channel 20 after just receiving it"
    );
});

QUnit.test('discuss should refresh filtered thread on changing thread', async function (assert) {
    assert.expect(4);

    this.data['mail.channel'].records.push({ id: 20 }, { id: 21 });
    await this.start();
    const channel20 = this.env.models['mail.thread'].findFromIdentifyingData({
        id: 20,
        model: 'mail.channel',
    });
    const channel21 = this.env.models['mail.thread'].findFromIdentifyingData({
        id: 21,
        model: 'mail.channel',
    });
    await this.afterEvent({
        eventName: 'o-thread-view-hint-processed',
        func: () => {
            document.querySelector(`
                .o_DiscussSidebar_item[data-thread-local-id="${channel20.localId}"]
            `).click();
        },
        message: "should wait until channel 20 is loaded after clicking on it",
        predicate: ({ hint, threadViewer }) => {
            return (
                hint.type === 'messages-loaded' &&
                threadViewer.thread.model === 'mail.channel' &&
                threadViewer.thread.id === 20
            );
        },
    });
    await this.afterEvent({
        eventName: 'o-thread-view-hint-processed',
        func: () => {
            // simulate control panel search
            this.env.messaging.discuss.update({
                stringifiedDomain: JSON.stringify([['body', 'ilike', 'test']]),
            });
        },
        message: "should wait until search filter is applied",
        predicate: ({ hint, threadViewer }) => {
            return (
                hint.type === 'messages-loaded' &&
                threadViewer.thread.model === 'mail.channel' &&
                threadViewer.thread.id === 20
            );
        },
    });
    assert.containsNone(
        document.body,
        '.o_Message',
        "should have initially no message in channel 20 matching the search 'test'"
    );

    await this.afterEvent({
        eventName: 'o-thread-view-hint-processed',
        func: () => {
            document.querySelector(`
                .o_DiscussSidebar_item[data-thread-local-id="${channel21.localId}"]
            `).click();
        },
        message: "should wait until channel 21 is loaded after clicking on it",
        predicate: ({ hint, threadViewer }) => {
            return (
                hint.type === 'messages-loaded' &&
                threadViewer.thread.model === 'mail.channel' &&
                threadViewer.thread.id === 21
            );
        },
    });
    assert.containsNone(
        document.body,
        '.o_Message',
        "should have no message in channel 21 matching the search 'test'"
    );
    // simulate receiving a message on channel 20 while channel 21 is displayed
    await this.env.services.rpc({
        route: '/mail/chat_post',
        params: {
            message_content: "test",
            uuid: channel20.uuid,
        },
    });
    assert.containsNone(
        document.body,
        '.o_Message',
        "should still have no message in channel 21 matching the search 'test' after receiving a message on channel 20"
    );

    await this.afterEvent({
        eventName: 'o-thread-view-hint-processed',
        func: () => {
            document.querySelector(`
                .o_DiscussSidebar_item[data-thread-local-id="${channel20.localId}"]
            `).click();
        },
        message: "should wait until channel 20 is loaded with the new message after clicking on it",
        predicate: ({ hint, threadViewer }) => {
            return (
                hint.type === 'messages-loaded' &&
                threadViewer.thread.model === 'mail.channel' &&
                threadViewer.thread.id === 20 &&
                threadViewer.threadCache.fetchedMessages.length === 1
            );
        },
    });
    assert.containsOnce(
        document.body,
        '.o_Message',
        "should now have the 1 message containing 'test' in channel 20 when displaying it, after having received the message while the channel was not visible"
    );
});

QUnit.test('select all and unselect all buttons should work on filtered thread', async function (assert) {
    assert.expect(4);

    this.data['mail.channel'].records.push({
        id: 20,
        is_moderator: true,
        moderation: true,
        name: "general",
    });
    this.data['mail.message'].records.push({
        body: "<p>test</p>",
        model: 'mail.channel',
        moderation_status: 'pending_moderation',
        res_id: 20,
    });
    await this.start();
    await this.afterEvent({
        eventName: 'o-thread-view-hint-processed',
        func: () => {
            document.querySelector(`
                .o_DiscussSidebar_item[data-thread-local-id="${this.env.messaging.moderation.localId}"]
            `).click();
        },
        message: "should wait until moderation box is loaded after clicking on it",
        predicate: ({ hint, threadViewer }) => {
            return (
                hint.type === 'messages-loaded' &&
                threadViewer.thread.model === 'mail.box' &&
                threadViewer.thread.id === 'moderation'
            );
        },
    });
    await this.afterEvent({
        eventName: 'o-thread-view-hint-processed',
        func: () => {
            // simulate control panel search
            this.env.messaging.discuss.update({
                stringifiedDomain: JSON.stringify([['body', 'ilike', 'test']]),
            });
        },
        message: "should wait until search filter is applied",
        predicate: ({ hint, threadViewer }) => {
            return (
                hint.type === 'messages-loaded' &&
                threadViewer.thread.model === 'mail.box' &&
                threadViewer.thread.id === 'moderation'
            );
        },
    });
    assert.containsOnce(
        document.body,
        '.o_Message',
        "should only have the 1 message containing 'test' in moderation box"
    );
    assert.notOk(
        document.querySelector('.o_Message_checkbox').checked,
        "the moderation checkbox should not be checked initially"
    );

    await afterNextRender(() => document.querySelector('.o_widget_Discuss_controlPanelButtonSelectAll').click());
    assert.ok(
        document.querySelector('.o_Message_checkbox').checked,
        "the moderation checkbox should be checked after clicking on 'select all'"
    );

    await afterNextRender(() => document.querySelector('.o_widget_Discuss_controlPanelButtonUnselectAll').click());
    assert.notOk(
        document.querySelector('.o_Message_checkbox').checked,
        "the moderation checkbox should be unchecked after clicking on 'unselect all'"
    );
});

});
});
});

});