summaryrefslogtreecommitdiff
path: root/addons/web/static/tests/services/notification_service_tests.js
blob: 45b4de51b1001fa64cb3070faa400479025e6ce9 (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
odoo.define('web.notification_tests', function (require) {
"use strict";

var AbstractView = require('web.AbstractView');
var Notification = require('web.Notification');
var NotificationService = require('web.NotificationService');

var testUtils = require('web.test_utils');
var createView = testUtils.createView;

var waitCloseNotification = function () {
    return new Promise(function (resolve) {
        setTimeout(resolve, 1);
    });
}

QUnit.module('Services', {
    beforeEach: function () {
        // We need to use a delay above 0 ms because otherwise the notification will close right after it opens
        // before we can perform any test.
        testUtils.mock.patch(Notification, {
            _autoCloseDelay: 1,
            _animation: false,
        });
        this.viewParams = {
            View: AbstractView,
            arch: '<fake/>',
            data: {
            fake_model: {
                    fields: {},
                    record: [],
                },
            },
            model: 'fake_model',
            services: {
                notification: NotificationService,
            },
        };
    },
    afterEach: function () {
        // The Notification Service has a side effect: it adds a div inside
        // document.body.  We could implement a cleanup mechanism for services,
        // but this seems a little overkill since services are not supposed to
        // be destroyed anyway.
        $('.o_notification_manager').remove();
        testUtils.mock.unpatch(Notification);
    }
}, function () {
    QUnit.module('Notification');

    QUnit.test('Display a warning notification', async function (assert) {
        assert.expect(4);

        var view = await createView(this.viewParams);
        view.call('notification', 'notify', {
            title: 'a',
            message: 'b',
        });
        await testUtils.nextMicrotaskTick();
        var $notification = $('body .o_notification_manager .o_notification');
        assert.strictEqual($notification.html().trim().replace(/\s+/g, ' '),
            "<div class=\"toast-header\"> <span class=\"fa fa-2x mr-3 fa-lightbulb-o o_notification_icon\" role=\"img\" aria-label=\"Notification undefined\" title=\"Notification undefined\"></span> <div class=\"d-flex align-items-center mr-auto font-weight-bold o_notification_title\">a</div> <button type=\"button\" class=\"close o_notification_close\" data-dismiss=\"toast\" aria-label=\"Close\"> <span class=\"d-inline\" aria-hidden=\"true\">×</span> </button> </div> <div class=\"toast-body\"> <div class=\"mr-auto o_notification_content\">b</div> </div>",
            "should display notification");
        assert.containsOnce($notification, '.o_notification_close');
        await waitCloseNotification();
        assert.strictEqual($notification.is(':hidden'), true, "should hide the notification");
        assert.strictEqual($('body .o_notification_manager .o_notification').length, 0, "should destroy the notification");
        view.destroy();
    });

    QUnit.test('Display a danger notification', async function (assert) {
        assert.expect(1);

        var view = await createView(this.viewParams);
        view.call('notification', 'notify', {
            title: 'a',
            message: 'b',
            type: 'danger'
        });
        await testUtils.nextMicrotaskTick();
        var $notification = $('body .o_notification_manager .o_notification');
        assert.strictEqual($notification.html().trim().replace(/\s+/g, ' '),
            "<div class=\"toast-header\"> <span class=\"fa fa-2x mr-3 fa-exclamation o_notification_icon\" role=\"img\" aria-label=\"Notification undefined\" title=\"Notification undefined\"></span> <div class=\"d-flex align-items-center mr-auto font-weight-bold o_notification_title\">a</div> <button type=\"button\" class=\"close o_notification_close\" data-dismiss=\"toast\" aria-label=\"Close\"> <span class=\"d-inline\" aria-hidden=\"true\">×</span> </button> </div> <div class=\"toast-body\"> <div class=\"mr-auto o_notification_content\">b</div> </div>",
            "should display notification");
        view.destroy();
    });

    QUnit.test('Display a sticky notification', async function (assert) {
        assert.expect(3);

        var view = await createView(this.viewParams);
        view.call('notification', 'notify', {
            title: 'a',
            message: 'b',
            sticky: true,
        });
        await testUtils.nextTick();
        var $notification = $('body .o_notification_manager .o_notification');
        assert.containsOnce($notification, '.o_notification_close', "should display the close button in notification");

        assert.strictEqual($notification.is(':hidden'), false, "should not hide the notification automatically");
        await testUtils.dom.click($notification.find('.o_notification_close'));
        assert.strictEqual($('body .o_notification_manager .o_notification').length,
            0, "should destroy the notification");
        view.destroy();
    });

    QUnit.test('Display a notification without title', async function (assert) {
        assert.expect(3);

        const view = await createView(this.viewParams);
        view.call('notification', 'notify', {
            title: false,
            message: 'b',
            sticky: true,
        });
        await testUtils.nextTick();
        const $notification = $('body .o_notification_manager .o_notification');
        assert.containsNone($notification, '.toast-header .o_notification_title');
        assert.containsNone($notification, '.o_notification_icon');
        assert.containsOnce($notification, '.toast-body .o_notification_close');

        view.destroy();
    });

    // FIXME skip because the feature is unused and do not understand why the test even worked before
    QUnit.skip('Display a simple notification with onClose callback when automatically close', async function (assert) {
        assert.expect(2);

        var close = 0;
        var view = await createView(this.viewParams);
        view.call('notification', 'notify', {
            title: 'a',
            message: 'b',
            onClose: function () {
                close++;
            }
        });
        await testUtils.nextMicrotaskTick();
        view.destroy();
        assert.strictEqual(close, 0, "should wait to call onClose method once");
        await testUtils.nextTick();
        assert.strictEqual(close, 1, "should call onClose method once");
    });

    QUnit.test('Display a sticky notification with onClose callback', async function (assert) {
        assert.expect(2);

        testUtils.mock.unpatch(Notification);
        testUtils.mock.patch(Notification, {
            _autoCloseDelay: 2500,
            _animation: false,
        });
        var view = await createView(this.viewParams);

        var close = 0;
        view.call('notification', 'notify', {
            title: 'a',
            message: 'b',
            sticky: true,
            onClose: function () {
                close++;
            }
        });
        await testUtils.nextMicrotaskTick();
        assert.strictEqual(close, 0, "should wait to call onClose method once");
        testUtils.dom.click($('body .o_notification_manager .o_notification .o_notification_close'));
        assert.strictEqual(close, 1, "should call onClose method once");
        view.destroy();
    });

    QUnit.test('Display a question', async function (assert) {
        assert.expect(8);

        var view = await createView(this.viewParams);
        function notification (inc) {
            return {
                title: 'a' + inc,
                message: 'b' + inc,
                buttons: [
                    {
                        text: 'accept' + inc,
                        primary: true,
                        click: function () {
                            assert.step('accept' + inc);
                        },
                    },
                    {
                        text: 'refuse' + inc,
                        click: function () {
                            assert.step('refuse' + inc);
                        },
                    }
                ],
                onClose: function () {
                    assert.step('close' + inc);
                }
            };
        };
        view.call('notification', 'notify', notification(0));
        view.call('notification', 'notify', notification(1));
        view.call('notification', 'notify', notification(2));
        await testUtils.nextTick();

        var $notification = $('body .o_notification_manager .o_notification');
        assert.containsOnce($notification.eq(0), '.o_notification_close',
            "should display the close button in notification");
        assert.strictEqual($notification.html().trim().replace(/\s+/g, ' '),
            "<div class=\"toast-header\"> <span class=\"fa fa-2x mr-3 fa-question-circle-o o_notification_icon\" role=\"img\" aria-label=\"Notification undefined\" title=\"Notification undefined\"></span> <div class=\"d-flex align-items-center mr-auto font-weight-bold o_notification_title\">a0</div> <button type=\"button\" class=\"close o_notification_close\" data-dismiss=\"toast\" aria-label=\"Close\"> <span class=\"d-inline\" aria-hidden=\"true\">×</span> </button> </div> <div class=\"toast-body\"> <div class=\"mr-auto o_notification_content\">b0</div> <div class=\"mt-2 o_notification_buttons\"> <button type=\"button\" class=\"btn btn-sm btn-primary\"> <span>accept0</span> </button><button type=\"button\" class=\"btn btn-sm btn-secondary\"> <span>refuse0</span> </button> </div> </div>",
            "should display notification");

        testUtils.dom.click($notification.find('.o_notification_buttons button:contains(accept0)'));
        testUtils.dom.click($notification.find('.o_notification_buttons button:contains(refuse1)'));
        testUtils.dom.click($notification.eq(2).find('.o_notification_close'));

        assert.strictEqual($notification.is(':hidden'), true, "should hide the notification");
        assert.strictEqual($('body .o_notification_manager .o_notification').length,
            0, "should destroy the notification");
        assert.verifySteps(['accept0', 'refuse1', 'close2']);
        view.destroy();
    });

    QUnit.test('call close notification service', async function (assert) {
        assert.expect(2);

        testUtils.mock.unpatch(Notification);
        testUtils.mock.patch(Notification, {
            _autoCloseDelay: 2500,
            _animation: false,
        });
        var view = await createView(this.viewParams);

        var close = 0;
        var notificationId0 = view.call('notification', 'notify', {
            title: 'a',
            message: 'b',
            onClose: function () {
                close++;
            }
        });
        var notificationId1 = view.call('notification', 'notify', {
            title: 'a',
            message: 'b',
            sticky: true,
            onClose: function () {
                close++;
            }
        });
        await testUtils.nextTick();

        view.call('notification', 'close', notificationId0);
        view.call('notification', 'close', notificationId1);
        await testUtils.nextTick();

        assert.strictEqual($('body .o_notification_manager .o_notification').length, 0, "should destroy the notifications");
        assert.strictEqual(close, 2, "should call onClose method twice");
        view.destroy();
    });

    QUnit.test('Display a custom notification', async function (assert) {
        assert.expect(3);

        var Custom = Notification.extend({
            init: function (parent, params) {
                this._super.apply(this, arguments);
                assert.ok(params.customParams, 'instantiate custom notification');
            },
            start: function () {
                var self = this;
                return this._super().then(function () {
                    self.$el.html('Custom');
                });
            },
        });

        var view = await createView(this.viewParams);
        view.call('notification', 'notify', {
            Notification: Custom,
            customParams: true,
        });
        await testUtils.nextMicrotaskTick();
        assert.containsOnce($('body'), '.o_notification_manager .o_notification:contains(Custom)',
            "should display the notification");
        view.destroy();
        assert.containsNone($('body'), '.o_notification_manager .o_notification',
            "should destroy the notification");
    });

});});