/
chatter_tests.js
2254 lines (2046 loc) · 87.5 KB
/
chatter_tests.js
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
odoo.define('mail.chatter_tests', function (require) {
"use strict";
var mailTestUtils = require('mail.testUtils');
var concurrency = require('web.concurrency');
var core = require('web.core');
var FormView = require('web.FormView');
var KanbanView = require('web.KanbanView');
var testUtils = require('web.test_utils');
var createAsyncView = testUtils.createAsyncView;
var createView = testUtils.createView;
var Activity = require('mail.Activity');
var _t = core._t;
QUnit.module('mail', {}, function () {
QUnit.module('Chatter', {
beforeEach: function () {
// patch _.debounce and _.throttle to be fast and synchronous
this.underscoreDebounce = _.debounce;
this.underscoreThrottle = _.throttle;
_.debounce = _.identity;
_.throttle = _.identity;
this.services = mailTestUtils.getMailServices();
this.data = {
partner: {
fields: {
display_name: { string: "Displayed name", type: "char" },
foo: {string: "Foo", type: "char", default: "My little Foo Value"},
message_follower_ids: {
string: "Followers",
type: "one2many",
relation: 'mail.followers',
relation_field: "res_id",
},
message_ids: {
string: "messages",
type: "one2many",
relation: 'mail.message',
relation_field: "res_id",
},
activity_ids: {
string: 'Activities',
type: 'one2many',
relation: 'mail.activity',
relation_field: 'res_id',
},
activity_state: {
string: 'State',
type: 'selection',
selection: [['overdue', 'Overdue'], ['today', 'Today'], ['planned', 'Planned']],
},
message_attachment_count: {
string: 'Attachment count',
type: 'integer',
},
},
records: [{
id: 2,
message_attachment_count: 3,
display_name: "first partner",
foo: "HELLO",
message_follower_ids: [],
message_ids: [],
activity_ids: [],
}]
},
'mail.activity': {
fields: {
activity_type_id: { string: "Activity type", type: "many2one", relation: "mail.activity.type" },
create_uid: { string: "Assigned to", type: "many2one", relation: 'partner' },
create_user_id: { string: "Creator", type: "many2one", relation: 'partner' },
display_name: { string: "Display name", type: "char" },
date_deadline: { string: "Due Date", type: "date" },
user_id: { string: "Assigned to", type: "many2one", relation: 'partner' },
state: {
string: 'State',
type: 'selection',
selection: [['overdue', 'Overdue'], ['today', 'Today'], ['planned', 'Planned']],
},
note : { string: "Note", type: "char" },
},
},
'mail.activity.type': {
fields: {
name: { string: "Name", type: "char" },
},
records: [
{ id: 1, name: "Type 1" },
{ id: 2, name: "Type 2" },
],
},
'mail.message': {
fields: {
attachment_ids: {
string: "Attachments",
type: 'many2many',
relation: 'ir.attachment',
default: [],
},
author_id: {
string: "Author",
relation: 'res.partner',
},
body: {
string: "Contents",
type: 'html',
},
date: {
string: "Date",
type: 'datetime',
},
is_note: {
string: "Note",
type: 'boolean',
},
is_discussion: {
string: "Discussion",
type: 'boolean',
},
is_notification: {
string: "Notification",
type: 'boolean',
},
is_starred: {
string: "Starred",
type: 'boolean',
},
model: {
string: "Related Document Model",
type: 'char',
},
res_id: {
string: "Related Document ID",
type: 'integer',
}
},
records: [],
},
'ir.attachment': {
fields:{
name:{type:'char', string:"attachment name", required:true},
res_model:{type:'char', string:"res model"},
res_id:{type:'integer', string:"res id"},
url:{type:'char', string:'url'},
type:{ type:'selection', selection:[['url',"URL"],['binary',"BINARY"]]},
mimetype:{type:'char', string:"mimetype"},
datas_fname:{type:'char', string:"filename"},
},
records:[
{id:1, name:"name1", type:'url', mimetype:'image/png', datas_fname:'filename.jpg',
res_id: 7, res_model: 'partner'},
{id:2, name:"name2", type:'binary', mimetype:"application/x-msdos-program",
datas_fname:"file2.txt", res_id: 7, res_model: 'partner'},
{id:3, name:"name2", type:'binary', mimetype:"application/x-msdos-program",
datas_fname:"file2.txt", res_id: 5, res_model: 'partner'},
],
},
};
},
afterEach: function () {
// unpatch _.debounce and _.throttle
_.debounce = this.underscoreDebounce;
_.throttle = this.underscoreThrottle;
}
});
QUnit.test('basic rendering', function (assert) {
assert.expect(9);
var count = 0;
var unwanted_read_count = 0;
var form = createView({
View: FormView,
model: 'partner',
data: this.data,
services: this.services,
arch: '<form string="Partners">' +
'<sheet>' +
'<field name="foo"/>' +
'</sheet>' +
'<div class="oe_chatter">' +
'<field name="message_follower_ids" widget="mail_followers"/>' +
'<field name="message_ids" widget="mail_thread"/>' +
'<field name="activity_ids" widget="mail_activity"/>' +
'</div>' +
'</form>',
mockRPC: function (route, args) {
if (route === '/web/dataset/call_kw/mail.followers/read') {
unwanted_read_count++;
}
if (route === '/mail/read_followers') {
count++;
return $.when({
followers: [],
subtypes: [],
});
}
return this._super(route, args);
},
res_id: 2,
});
assert.ok(form.$('.o_mail_activity').length, "there should be an activity widget");
assert.ok(form.$('.o_chatter_topbar .o_chatter_button_schedule_activity').length,
"there should be a 'Schedule an activity' button in the chatter's topbar");
assert.ok(form.$('.o_chatter_topbar .o_followers').length,
"there should be a followers widget, moved inside the chatter's topbar");
assert.ok(form.$('.o_chatter').length, "there should be a chatter widget");
assert.ok(form.$('.o_mail_thread').length, "there should be a mail thread");
assert.strictEqual(form.$('.o_chatter_button_attachment').length, 1, "should have one attachment button");
assert.ok(!form.$('.o_chatter_topbar .o_chatter_button_log_note').length,
"log note button should not be available");
form.$buttons.find('.o_form_button_edit').click();
assert.strictEqual(count, 0, "should have done no read_followers rpc as there are no followers");
assert.strictEqual(unwanted_read_count, 0, "followers should only be fetched with read_followers route");
form.destroy();
});
QUnit.test('Activity Done keep feedback on blur', function (assert) {
assert.expect(3);
var done = assert.async();
this.data['mail.activity'].records = [
{activity_type_id: 1, id: 1, user_id: 2, state: 'today', note: 'But I\'m talkin\' about Shaft'},
];
this.data.partner.records[0].activity_ids = [1];
var shownDef = $.Deferred();
var hiddenDef = $.Deferred();
testUtils.patch(Activity, {
_bindPopoverFocusout: function () {
this._super.apply(this, arguments);
shownDef.resolve();
},
});
var form = createView({
View: FormView,
model: 'partner',
data: this.data,
services: this.services,
res_id: 2,
arch:'<form string="Partners">' +
'<div class="oe_chatter">' +
'<field name="activity_ids" widget="mail_activity"/>' +
'</div>' +
'</form>',
});
// sanity checks
var $activityEl = form.$('.o_mail_activity[name=activity_ids]');
assert.strictEqual($activityEl.find('.o_thread_message').length, 1,
'There should be one activity');
assert.strictEqual($activityEl.find('.o_thread_message .o_thread_message_note').text().trim(),
'But I\'m talkin\' about Shaft', 'The activity should have the right note');
var $popoverEl = $activityEl.find('.o_thread_message_tools .o_mark_as_done');
$popoverEl.on('hidden.bs.popover', hiddenDef.resolve.bind(hiddenDef));
// open popover
$popoverEl.click();
shownDef.then(function () {
// write a feedback and focusout
var $feedbackPopover = $($popoverEl.data('bs.popover').tip);
$feedbackPopover.find('#activity_feedback').val('John Shaft').focusout();
hiddenDef.then(function () {
shownDef = $.Deferred();
// re-open popover
$popoverEl.click();
shownDef.then(function () {
var $feedbackPopover = $($popoverEl.data('bs.popover').tip);
assert.strictEqual($feedbackPopover.find('#activity_feedback').val(), 'John Shaft',
"feedback should have been kept");
form.destroy();
testUtils.unpatch(Activity);
done();
});
});
});
});
QUnit.test('attachmentBox basic rendering', function (assert) {
assert.expect(11);
this.data.partner.records.push({
id: 7,
display_name: "attachment_test",
});
var form = createView({
View: FormView,
model: 'partner',
data: this.data,
services: this.services,
arch: '<form string="Partners">' +
'<sheet>' +
'<field name="foo"/>' +
'</sheet>' +
'<div class="oe_chatter">' +
'</div>' +
'</form>',
res_id: 7,
});
var $button = form.$('.o_chatter_button_attachment');
assert.strictEqual($button.length, 1, "should have one attachment button");
$button.click();
assert.strictEqual(form.$('.o_mail_chatter_attachments').length, 1,
"attachment widget should exist after a first click on the button");
assert.strictEqual(form.$('.o_attachment_image').length, 1, "there should be an image preview");
assert.strictEqual(form.$('.o_attachments_previews').length, 1, "there should be a list of previews");
assert.strictEqual(form.$('.o_attachments_list').length, 1, "there should be a list of non previewable attachments");
assert.strictEqual(form.$('.o_attachment_title').text(), 'name1',
"the image name should be correct");
// since there are two elements "Download name2"; one "name" and the other "txt" as text content, the following test
// asserts both at the same time.
assert.strictEqual(form.$('a[title = "Download name2"]').text().trim(), 'name2txt',
"the attachment name should be correct");
assert.ok(form.$('.o_attachment_image').css('background-image').indexOf('/web/image/1/160x160/?crop=true') >= 0,
"the attachment image URL should be correct");
assert.strictEqual(form.$('.o_attachment_download').eq(0).attr('href'), '/web/content/1?download=true',
"the download URL of name1 must be correct");
assert.strictEqual(form.$('.o_attachment_download').eq(1).attr('href'), '/web/content/2?download=true',
"the download URL of name2 must be correct");
$button.click();
assert.strictEqual(form.$('.o_mail_chatter_attachments').length, 0,
"attachment widget should de destroyed after we reclick the button");
form.destroy();
});
QUnit.test('chatter in create mode', function (assert) {
assert.expect(9);
var form = createView({
View: FormView,
model: 'partner',
data: this.data,
services: this.services,
arch: '<form string="Partners">' +
'<sheet>' +
'<field name="foo"/>' +
'</sheet>' +
'<div class="oe_chatter">' +
'<field name="message_ids" widget="mail_thread"/>' +
'</div>' +
'</form>',
res_id: 2,
mockRPC: function (route, args) {
if (route === "/web/dataset/call_kw/partner/message_get_suggested_recipients") {
return $.when({2: []});
}
return this._super(route, args);
},
});
assert.strictEqual(form.$('.o_chatter').length, 1,
"chatter should be displayed");
// entering create mode
form.$buttons.find('.o_form_button_create').click();
assert.ok(form.$el.find('.o_form_view').hasClass('o_form_editable'),
"we should be in create mode");
assert.strictEqual(form.$('.o_chatter').length, 1,
"chatter should still be displayed in create mode");
// topbar buttons disabled in create mode (e.g. 'send message')
assert.strictEqual(form.$('.o_chatter_topbar button:not(:disabled)').length, 0,
"button should be disabled in create mode");
// chatter containing a single message with 'Creating a record...'
assert.strictEqual(form.$('.o_mail_thread').length, 1,
"there should be a mail thread");
assert.strictEqual(form.$('.o_thread_message').length, 1,
"there should be a single thread message");
assert.strictEqual(form.$('.o_thread_message_content').text().trim(),
"Creating a new record...",
"the content of the message should be 'Creating a new record...'");
// getting out of create mode by saving
form.$('.o_field_char').val('coucou').trigger('input');
form.$buttons.find('.o_form_button_save').click();
assert.strictEqual(form.$('.o_chatter').length, 1,
"chatter should still be displayed after saving from create mode");
// check if chatter buttons still work
form.$('.o_chatter_button_new_message').click();
assert.strictEqual(form.$('.o_thread_composer:visible').length, 1,
"chatter should be opened");
form.destroy();
});
QUnit.test('chatter rendering inside the sheet', function (assert) {
assert.expect(5);
var form = createView({
View: FormView,
model: 'partner',
data: this.data,
services: this.services,
arch: '<form string="Partners">' +
'<sheet>' +
'<field name="foo"/>' +
'<notebook>' +
'<page>' +
'<div class="oe_chatter">' +
'<field name="message_ids" widget="mail_thread"/>' +
'</div>' +
'</page>' +
'</notebook>' +
'</sheet>' +
'</form>',
res_id: 2,
mockRPC: function (route, args) {
if (route === "/web/dataset/call_kw/partner/message_get_suggested_recipients") {
return $.when({2: []});
}
return this._super(route, args);
},
});
assert.strictEqual(form.$('.o_chatter').length, 1,
"chatter should be displayed");
form.$buttons.find('.o_form_button_create').click();
assert.ok(form.$el.find('.o_form_view').hasClass('o_form_editable'),
"we should be in create mode");
assert.strictEqual(form.$('.o_chatter').length, 1,
"chatter should be displayed");
form.$('.o_field_char').val('coucou').trigger('input');
form.$buttons.find('.o_form_button_save').click();
assert.strictEqual(form.$('.o_chatter').length, 1,
"chatter should be displayed");
// check if chatter buttons still work
form.$('.o_chatter_button_new_message').click();
assert.strictEqual(form.$('.o_thread_composer:visible').length, 1,
"chatter should be opened");
form.destroy();
});
QUnit.test('kanban activity widget with no activity', function (assert) {
assert.expect(4);
var rpcCount = 0;
var kanban = createView({
View: KanbanView,
model: 'partner',
data: this.data,
arch: '<kanban>' +
'<field name="activity_state"/>' +
'<templates><t t-name="kanban-box">' +
'<div><field name="activity_ids" widget="kanban_activity"/></div>' +
'</t></templates>' +
'</kanban>',
mockRPC: function (route, args) {
rpcCount++;
return this._super(route, args);
},
session: {uid: 2},
});
var $record = kanban.$('.o_kanban_record').first();
assert.ok($record.find('.o_mail_activity .o_activity_color_default').length,
"activity widget should have been rendered correctly");
assert.strictEqual(rpcCount, 1, '1 RPC (search_read) should have been done');
// click on the activity button
$record.find('.o_activity_btn').click();
assert.strictEqual(rpcCount, 1, 'no RPC should have been done as there is no activity');
assert.strictEqual($record.find('.o_no_activity').length, 1, "should have no activity scheduled");
// fixme: it would be nice to be able to test the scheduling of a new activity, but not
// possible for now as we can't mock a fields_view_get (required by the do_action)
kanban.destroy();
});
QUnit.test('kanban activity widget with an activity', function (assert) {
assert.expect(12);
this.data.partner.records[0].activity_ids = [1];
this.data.partner.records[0].activity_state = 'today';
this.data['mail.activity'].records = [{
id: 1,
display_name: "An activity",
date_deadline: moment().format("YYYY-MM-DD"), // now
state: "today",
user_id: 2,
create_user_id: 2,
activity_type_id: 1,
}];
var rpcCount = 0;
var kanban = createView({
View: KanbanView,
model: 'partner',
data: this.data,
arch: '<kanban>' +
'<field name="activity_state"/>' +
'<templates><t t-name="kanban-box">' +
'<div><field name="activity_ids" widget="kanban_activity"/></div>' +
'</t></templates>' +
'</kanban>',
mockRPC: function (route, args) {
rpcCount++;
if (route === '/web/dataset/call_kw/mail.activity/action_feedback') {
var current_ids = this.data.partner.records[0].activity_ids;
var done_ids = args.args[0];
this.data.partner.records[0].activity_ids = _.difference(current_ids, done_ids);
this.data.partner.records[0].activity_state = false;
return $.when();
}
return this._super(route, args);
},
session: {uid:2},
});
var $record = kanban.$('.o_kanban_record').first();
assert.ok($record.find('.o_mail_activity .o_activity_color_today').length,
"activity widget should have been rendered correctly");
assert.strictEqual(rpcCount, 1, '1 RPC (search_read) should have been done');
// click on the activity button
$record.find('.o_activity_btn').click();
assert.strictEqual(rpcCount, 2, 'a read should have been done to fetch the activity details');
assert.strictEqual($record.find('.o_activity_title').length, 1, "should have an activity scheduled");
var label = $record.find('.o_activity_log .o_activity_color_today');
assert.strictEqual(label.find('strong').text(), "Today", "should display the correct label");
assert.strictEqual(label.find('.badge-warning').text(), "1", "should display the correct count");
// click on the activity button to close the dropdown
$record.find('.o_activity_btn').click();
assert.strictEqual(rpcCount, 2, 'no RPC should be done when closing the dropdown');
// click on the activity button to re-open dropdown
$record.find('.o_activity_btn').click();
assert.strictEqual(rpcCount, 3, 'should have reloaded the activities');
// mark activity as done
$record.find('.o_mark_as_done').click();
$record.find('.o_activity_popover_done').click();
$record = kanban.$('.o_kanban_record').first(); // the record widget has been reset
assert.strictEqual(rpcCount, 5, 'should have done an RPC to mark activity as done, and a read');
assert.ok($record.find('.o_mail_activity .o_activity_color_default:not(.o_activity_color_today)').length,
"activity widget should have been updated correctly");
assert.strictEqual($record.find('.o_mail_activity.show').length, 1,
"dropdown should remain open when marking an activity as done");
assert.strictEqual($record.find('.o_no_activity').length, 1, "should have no activity scheduled");
kanban.destroy();
});
QUnit.test('kanban activity widget popover test', function (assert) {
assert.expect(3);
this.data.partner.records[0].activity_ids = [1];
this.data.partner.records[0].activity_state = 'today';
this.data['mail.activity'].records = [{
id: 1,
display_name: "An activity",
date_deadline: moment().format("YYYY-MM-DD"), // now
state: "today",
user_id: 2,
create_user_id: 2,
activity_type_id: 1,
}];
var rpcCount = 0;
var kanban = createView({
View: KanbanView,
model: 'partner',
data: this.data,
arch: '<kanban>' +
'<field name="activity_state"/>' +
'<templates><t t-name="kanban-box">' +
'<div><field name="activity_ids" widget="kanban_activity"/></div>' +
'</t></templates>' +
'</kanban>',
mockRPC: function (route, args) {
if (route === '/web/dataset/call_kw/mail.activity/action_feedback_schedule_next') {
rpcCount++;
var current_ids = this.data.partner.records[0].activity_ids;
var done_ids = args.args[0];
this.data.partner.records[0].activity_ids = _.difference(current_ids, done_ids);
this.data.partner.records[0].activity_state = false;
return $.when();
}
return this._super(route, args);
},
});
var $record = kanban.$('.o_kanban_record').first();
$record.find('.o_activity_btn').click();
// Click on button and see popover no RPC call
$record.find('.o_mark_as_done').click();
assert.equal(rpcCount, 0, "");
// Click on discard no RPC call
$record.find('.o_activity_popover_discard').click();
assert.equal(rpcCount, 0, "");
// Click on button and then on done and schedule next
// RPC call
$record.find('.o_activity_popover_done_next').click();
assert.equal(rpcCount, 1, "");
kanban.destroy();
});
QUnit.test('chatter: post, receive and star messages', function (assert) {
var done = assert.async();
assert.expect(27);
this.data.partner.records[0].message_ids = [1];
this.data['mail.message'].records = [{
author_id: ["1", "John Doe"],
body: "A message",
date: "2016-12-20 09:35:40",
id: 1,
is_note: false,
is_discussion: true,
is_notification: false,
is_starred: false,
model: 'partner',
res_id: 2,
}];
var getSuggestionsDef = $.Deferred();
var form = createView({
View: FormView,
model: 'partner',
data: this.data,
services: this.services,
arch: '<form string="Partners">' +
'<sheet>' +
'<field name="foo"/>' +
'</sheet>' +
'<div class="oe_chatter">' +
'<field name="message_ids" widget="mail_thread" options="{\'display_log_button\': True}"/>' +
'</div>' +
'</form>',
res_id: 2,
mockRPC: function (route, args) {
if (args.method === 'message_get_suggested_recipients') {
return $.when({2: []});
}
if (args.method === 'get_mention_suggestions') {
getSuggestionsDef.resolve();
return $.when([{email: "test@odoo.com", id: 1, name: "Test User"}]);
}
if (args.method === 'message_post') {
var lastMessageData = _.max(this.data['mail.message'].records, function (messageData) {
return messageData.id;
});
var messageID = lastMessageData.id + 1;
this.data['mail.message'].records.push({
attachment_ids: args.kwargs.attachment_ids,
author_id: ["42", "Me"],
body: args.kwargs.body,
date: "2016-12-20 10:35:40",
id: messageID,
is_note: args.kwargs.subtype === 'mail.mt_note',
is_discussion: args.kwargs.subtype === 'mail.mt_comment',
is_notification: false,
is_starred: false,
model: 'partner',
res_id: 2,
});
return $.when(messageID);
}
if (args.method === 'toggle_message_starred') {
assert.ok(_.contains(args.args[0], 2),
"toggle_star_status should have been triggered for message 2 (twice)");
var messageData = _.findWhere(
this.data['mail.message'].records,
{ id: args.args[0][0] }
);
messageData.is_starred = !messageData.is_starred;
// simulate notification received by mail_service from longpoll
var data = {
info: false,
message_ids: [messageData.id],
starred: messageData.is_starred,
type: 'toggle_star',
};
var notification = [[false, 'res.partner'], data];
form.call('bus_service', 'trigger', 'notification', [notification]);
return $.when();
}
return this._super(route, args);
},
session: {},
});
assert.ok(form.$('.o_chatter_topbar .o_chatter_button_log_note').length,
"log note button should be available");
assert.strictEqual(form.$('.o_thread_message').length, 1, "thread should contain one message");
assert.ok(form.$('.o_thread_message:first().o_mail_discussion').length,
"the message should be a discussion");
assert.ok(form.$('.o_thread_message:first() .o_thread_message_core').text().indexOf('A message') >= 0,
"the message's body should be correct");
assert.ok(form.$('.o_thread_message:first() .o_mail_info').text().indexOf('John Doe') >= 0,
"the message's author should be correct");
// send a message
form.$('.o_chatter_button_new_message').click();
assert.ok(!$('.oe_chatter .o_thread_composer').hasClass('o_hidden'), "chatter should be opened");
form.$('.oe_chatter .o_composer_text_field:first()').val("My first message");
form.$('.oe_chatter .o_composer_button_send').click();
assert.ok($('.oe_chatter .o_thread_composer').hasClass('o_hidden'), "chatter should be closed");
assert.strictEqual(form.$('.o_thread_message').length, 2, "thread should contain two messages");
assert.ok(form.$('.o_thread_message:first().o_mail_discussion').length,
"the last message should be a discussion");
assert.ok(form.$('.o_thread_message:first() .o_thread_message_core').text().indexOf('My first message') >= 0,
"the message's body should be correct");
assert.ok(form.$('.o_thread_message:first() .o_mail_info').text().indexOf('Me') >= 0,
"the message's author should be correct");
// log a note
form.$('.o_chatter_button_log_note').click();
assert.ok(!$('.oe_chatter .o_thread_composer').hasClass('o_hidden'), "chatter should be opened");
form.$('.oe_chatter .o_composer_text_field:first()').val("My first note");
form.$('.oe_chatter .o_composer_button_send').click();
assert.ok($('.oe_chatter .o_thread_composer').hasClass('o_hidden'), "chatter should be closed");
assert.strictEqual(form.$('.o_thread_message').length, 3, "thread should contain three messages");
assert.ok(!form.$('.o_thread_message:first().o_mail_discussion').length,
"the last message should not be a discussion");
assert.ok(form.$('.o_thread_message:first() .o_thread_message_core').text().indexOf('My first note') >= 0,
"the message's body should be correct");
assert.ok(form.$('.o_thread_message:first() .o_mail_info').text().indexOf('Me') >= 0,
"the message's author should be correct");
// star message 2
assert.ok(form.$('.o_thread_message[data-message-id=2] .o_thread_message_star.fa-star-o').length,
"message 2 should not be starred");
form.$('.o_thread_message[data-message-id=2] .o_thread_message_star').click();
assert.ok(form.$('.o_thread_message[data-message-id=2] .o_thread_message_star.fa-star').length,
"message 2 should be starred");
// unstar message 2
form.$('.o_thread_message[data-message-id=2] .o_thread_message_star').click();
assert.ok(form.$('.o_thread_message[data-message-id=2] .o_thread_message_star.fa-star-o').length,
"message 2 should not be starred");
// very basic test of mention
form.$('.o_chatter_button_new_message').click();
var $input = form.$('.oe_chatter .o_composer_text_field:first()');
$input.val('@');
// the cursor position must be set for the mention manager to detect that we are mentionning
$input[0].selectionStart = 1;
$input[0].selectionEnd = 1;
$input.trigger('keyup');
assert.strictEqual(getSuggestionsDef.state(), "pending",
"the mention suggestion RPC should be throttled");
getSuggestionsDef
.then(concurrency.delay.bind(concurrency, 0))
.then(function () {
assert.strictEqual(form.$('.o_mention_proposition:visible').length, 1,
"there should be one mention suggestion");
assert.strictEqual(form.$('.o_mention_proposition').data('id'), 1,
"suggestion's id should be correct");
assert.strictEqual(form.$('.o_mention_proposition .o_mention_name').text(), 'Test User',
"suggestion should be displayed correctly");
assert.strictEqual(form.$('.o_mention_proposition .o_mention_info').text(), '(test@odoo.com)',
"suggestion should be displayed correctly");
//cleanup
form.destroy();
done();
});
});
QUnit.test('chatter: post a message and switch in edit mode', function (assert) {
assert.expect(5);
var form = createView({
View: FormView,
model: 'partner',
data: this.data,
services: this.services,
arch: '<form string="Partners">' +
'<sheet>' +
'<field name="foo"/>' +
'</sheet>' +
'<div class="oe_chatter">' +
'<field name="message_ids" widget="mail_thread" options="{\'display_log_button\': True}"/>' +
'</div>' +
'</form>',
res_id: 2,
session: {},
mockRPC: function (route, args) {
if (args.method === 'message_get_suggested_recipients') {
return $.when({2: []});
}
if (args.method === 'message_post') {
this.data['mail.message'].records.push({
attachment_ids: args.kwargs.attachment_ids,
author_id: ["42", "Me"],
body: args.kwargs.body,
date: "2016-12-20 10:35:40",
id: 42,
is_note: args.kwargs.subtype === 'mail.mt_note',
is_discussion: args.kwargs.subtype === 'mail.mt_comment',
is_starred: false,
model: 'partner',
res_id: 2,
});
return $.when(42);
}
return this._super(route, args);
},
});
assert.strictEqual(form.$('.o_thread_message').length, 0, "thread should not contain messages");
// send a message
form.$('.o_chatter_button_new_message').click();
form.$('.oe_chatter .o_composer_text_field:first()').val("My first message");
form.$('.oe_chatter .o_composer_button_send').click();
assert.strictEqual(form.$('.o_thread_message').length, 1, "thread should contain a message");
assert.ok(form.$('.o_thread_message:first() .o_thread_message_core').text().indexOf('My first message') >= 0,
"the message's body should be correct");
// switch in edit mode
form.$buttons.find('.o_form_button_edit').click();
assert.strictEqual(form.$('.o_thread_message').length, 1, "thread should contain a message");
assert.ok(form.$('.o_thread_message:first() .o_thread_message_core').text().indexOf('My first message') >= 0,
"the message's body should be correct");
form.destroy();
});
QUnit.test('chatter: discard changes on message post with post_refresh "always"', function (assert) {
// After posting a message that always reloads the record, if the record
// is dirty (= has some unsaved changes), we should warn the user that
// these changes will be lost if he proceeds.
assert.expect(2);
var form = createView({
View: FormView,
model: 'partner',
data: this.data,
services: this.services,
arch: '<form string="Partners">' +
'<sheet>' +
'<field name="foo"/>' +
'</sheet>' +
'<div class="oe_chatter">' +
'<field name="message_ids" widget="mail_thread"' +
' options="{\'display_log_button\': True, \'post_refresh\': \'always\'}"/>' +
'</div>' +
'</form>',
res_id: 2,
session: {},
mockRPC: function (route, args) {
if (route === "/web/dataset/call_kw/partner/message_get_suggested_recipients") {
return $.when({2: []});
}
return this._super(route, args);
},
viewOptions: {
mode: 'edit',
},
});
// Make record dirty
form.$('.o_form_sheet input').val('trululu').trigger('input');
// Send a message
form.$('.o_chatter_button_new_message').click();
form.$('.oe_chatter .o_composer_text_field:first()').val("My first message");
form.$('.oe_chatter .o_composer_button_send').click();
var $modal = $('.modal-dialog');
assert.strictEqual($modal.length, 1, "should have a modal opened");
assert.strictEqual($modal.find('.modal-body').text(),
"The record has been modified, your changes will be discarded. Do you want to proceed?",
"should warn the user that any unsaved changes will be lost");
form.destroy();
});
QUnit.test('chatter: discard changes on message post without post_refresh', function (assert) {
// After posting a message, if the record is dirty and there are no
// post_refresh rule, it will not discard the changes on the record.
assert.expect(2);
var hasDiscardChanges = false; // set if `discard_changes` has been triggered up
var messages = [];
var form = createView({
View: FormView,
model: 'partner',
data: this.data,
services: this.services,
arch: '<form string="Partners">' +
'<sheet>' +
'<field name="foo"/>' +
'</sheet>' +
'<div class="oe_chatter">' +
'<field name="message_ids" widget="mail_thread"' +
' options="{\'display_log_button\': True}"/>' +
'</div>' +
'</form>',
res_id: 2,
session: {},
mockRPC: function (route, args) {
if (route === "/web/dataset/call_kw/partner/message_get_suggested_recipients") {
return $.when({2: []});
}
if (args.method === 'message_format') {
var requested_msgs = _.filter(messages, function (msg) {
return _.contains(args.args[0], msg.id);
});
return $.when(requested_msgs);
}
if (args.method === 'message_post') {
messages.push({
attachment_ids: [],
author_id: ["42", "Me"],
body: args.kwargs.body,
date: moment().format('YYYY-MM-DD HH:MM:SS'), // now
displayed_author: "Me",
id: 42,
is_note: args.kwargs.subtype === 'mail.mt_note',
is_starred: false,
model: 'partner',
res_id: 2,
});
return $.when(42);
}
return this._super(route, args);
},
intercepts: {
discard_changes: function () {
hasDiscardChanges = true; // should not do that
},
},
viewOptions: {
mode: 'edit',
},
});
// Make record dirty
form.$('.o_form_sheet input').val('trululu').trigger('input');
// Send a message
form.$('.o_chatter_button_new_message').click();
form.$('.oe_chatter .o_composer_text_field:first()').val("My first message");
form.$('.oe_chatter .o_composer_button_send').click();
var $modal = $('.modal-dialog');
assert.strictEqual($modal.length, 0, "should have no modal opened");
assert.notOk(hasDiscardChanges);
form.destroy();
});
QUnit.test('chatter: discard changes on message post with post_refresh "recipients"', function (assert) {
// After posting a message with mentions, the record will be reloaded,
// as the rpc `message_post` may make changes on some fields of the record.
// If the record is dirty (= has some unsaved changes), we should warn the
// user that these changes will be lost if he proceeds.
assert.expect(2);
var done = assert.async();
var getSuggestionsDef = $.Deferred();
var messages = [];
var form = createView({
View: FormView,
model: 'partner',
data: this.data,
services: this.services,
arch: '<form string="Partners">' +
'<sheet>' +
'<field name="foo"/>' +
'</sheet>' +
'<div class="oe_chatter">' +
'<field name="message_ids" widget="mail_thread"' +
' options="{\'display_log_button\': True, \'post_refresh\': \'recipients\'}"/>' +
'</div>' +
'</form>',
res_id: 2,