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

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

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

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

QUnit.test('sidebar find shows channels matching search term', async function (assert) {
    assert.expect(3);

    this.data['mail.channel'].records.push({
        channel_partner_ids: [],
        channel_type: 'channel',
        id: 20,
        members: [],
        name: 'test',
        public: 'public',
    });
    const searchReadDef = makeDeferred();
    await this.start({
        async mockRPC(route, args) {
            const res = await this._super(...arguments);
            if (args.method === 'search_read') {
                searchReadDef.resolve();
            }
            return res;
        },
    });
    await afterNextRender(() =>
        document.querySelector(`.o_DiscussSidebar_groupHeaderItemAdd`).click()
    );
    document.querySelector(`.o_DiscussSidebar_itemNew`).focus();
    document.execCommand('insertText', false, "test");
    document.querySelector(`.o_DiscussSidebar_itemNew`)
        .dispatchEvent(new window.KeyboardEvent('keydown'));
    document.querySelector(`.o_DiscussSidebar_itemNew`)
        .dispatchEvent(new window.KeyboardEvent('keyup'));

    await searchReadDef;
    await nextAnimationFrame(); // ensures search_read rpc is rendered.
    const results = document.querySelectorAll('.ui-autocomplete .ui-menu-item a');
    assert.ok(
        results,
        "should have autocomplete suggestion after typing on 'find or create channel' input"
    );
    assert.strictEqual(
        results.length,
        // When searching for a single existing channel, the results list will have at least 3 lines:
        // One for the existing channel itself
        // One for creating a public channel with the search term
        // One for creating a private channel with the search term
        3
    );
    assert.strictEqual(
        results[0].textContent,
        "test",
        "autocomplete suggestion should target the channel matching search term"
    );
});

QUnit.test('sidebar find shows channels matching search term even when user is member', async function (assert) {
    assert.expect(3);

    this.data['mail.channel'].records.push({
        channel_partner_ids: [this.data.currentPartnerId],
        channel_type: 'channel',
        id: 20,
        members: [this.data.currentPartnerId],
        name: 'test',
        public: 'public',
    });
    const searchReadDef = makeDeferred();
    await this.start({
        async mockRPC(route, args) {
            const res = await this._super(...arguments);
            if (args.method === 'search_read') {
                searchReadDef.resolve();
            }
            return res;
        },
    });
    await afterNextRender(() =>
        document.querySelector(`.o_DiscussSidebar_groupHeaderItemAdd`).click()
    );
    document.querySelector(`.o_DiscussSidebar_itemNew`).focus();
    document.execCommand('insertText', false, "test");
    document.querySelector(`.o_DiscussSidebar_itemNew`)
        .dispatchEvent(new window.KeyboardEvent('keydown'));
    document.querySelector(`.o_DiscussSidebar_itemNew`)
        .dispatchEvent(new window.KeyboardEvent('keyup'));

    await searchReadDef;
    await nextAnimationFrame();
    const results = document.querySelectorAll('.ui-autocomplete .ui-menu-item a');
    assert.ok(
        results,
        "should have autocomplete suggestion after typing on 'find or create channel' input"
    );
    assert.strictEqual(
        results.length,
        // When searching for a single existing channel, the results list will have at least 3 lines:
        // One for the existing channel itself
        // One for creating a public channel with the search term
        // One for creating a private channel with the search term
        3
    );
    assert.strictEqual(
        results[0].textContent,
        "test",
        "autocomplete suggestion should target the channel matching search term even if user is member"
    );
});

QUnit.test('sidebar channels should be ordered case insensitive alphabetically', async function (assert) {
    assert.expect(1);

    this.data['mail.channel'].records.push(
        { id: 19, name: "Xyz" },
        { id: 20, name: "abc" },
        { id: 21, name: "Abc" },
        { id: 22, name: "Xyz" }
    );
    await this.start();
    const results = document.querySelectorAll('.o_DiscussSidebar_groupChannel .o_DiscussSidebarItem_name');
    assert.deepEqual(
        [results[0].textContent, results[1].textContent, results[2].textContent, results[3].textContent],
        ["abc", "Abc", "Xyz", "Xyz"],
        "Channel name should be in case insensitive alphabetical order"
    );
});

});
});
});

});