forked from zulip/zulip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreactions.js
1027 lines (890 loc) · 31.3 KB
/
reactions.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
"use strict";
const {strict: assert} = require("assert");
const {mock_esm, set_global, zrequire} = require("../zjsunit/namespace");
const {make_stub} = require("../zjsunit/stub");
const {run_test} = require("../zjsunit/test");
const blueslip = require("../zjsunit/zblueslip");
const $ = require("../zjsunit/zjquery");
const {page_params} = require("../zjsunit/zpage_params");
const alice_user_id = 5;
const sample_message = {
id: 1001,
reactions: [
{emoji_name: "smile", user_id: 5, reaction_type: "unicode_emoji", emoji_code: "1f642"},
{emoji_name: "smile", user_id: 6, reaction_type: "unicode_emoji", emoji_code: "1f642"},
{emoji_name: "frown", user_id: 7, reaction_type: "unicode_emoji", emoji_code: "1f641"},
{emoji_name: "tada", user_id: 7, reaction_type: "unicode_emoji", emoji_code: "1f389"},
{emoji_name: "tada", user_id: 8, reaction_type: "unicode_emoji", emoji_code: "1f389"},
{emoji_name: "rocket", user_id: 5, reaction_type: "unicode_emoji", emoji_code: "1f680"},
{emoji_name: "rocket", user_id: 6, reaction_type: "unicode_emoji", emoji_code: "1f680"},
{emoji_name: "rocket", user_id: 7, reaction_type: "unicode_emoji", emoji_code: "1f680"},
{emoji_name: "wave", user_id: 6, reaction_type: "unicode_emoji", emoji_code: "1f44b"},
{emoji_name: "wave", user_id: 7, reaction_type: "unicode_emoji", emoji_code: "1f44b"},
{emoji_name: "wave", user_id: 8, reaction_type: "unicode_emoji", emoji_code: "1f44b"},
{
emoji_name: "inactive_realm_emoji",
user_id: 5,
reaction_type: "realm_emoji",
emoji_code: "992",
},
],
};
const channel = mock_esm("../../static/js/channel");
const emoji_picker = mock_esm("../../static/js/emoji_picker", {
hide_emoji_popover() {},
});
const message_lists = mock_esm("../../static/js/message_lists");
const message_store = mock_esm("../../static/js/message_store");
const spectators = mock_esm("../../static/js/spectators", {
login_to_access() {},
});
message_lists.current = {
selected_message() {
return {sent_by_me: true};
},
selected_row() {
return $(".selected-row");
},
selected_id() {
return 42;
},
};
set_global("document", "document-stub");
const emoji_codes = zrequire("../generated/emoji/emoji_codes.json");
const emoji = zrequire("../shared/js/emoji");
const people = zrequire("people");
const reactions = zrequire("reactions");
const emoji_params = {
realm_emoji: {
991: {
id: "991",
name: "realm_emoji",
source_url: "/url/for/991",
deactivated: false,
},
992: {
id: "992",
name: "inactive_realm_emoji",
source_url: "/url/for/992",
still_url: "/still/url/for/992",
deactivated: true,
},
},
emoji_codes,
};
emoji.initialize(emoji_params);
const alice = {
email: "[email protected]",
user_id: alice_user_id,
full_name: "Alice",
};
const bob = {
email: "[email protected]",
user_id: 6,
full_name: "Bob van Roberts",
};
const cali = {
email: "[email protected]",
user_id: 7,
full_name: "Cali",
};
const alexus = {
email: "[email protected]",
user_id: 8,
full_name: "Alexus",
};
people.add_active_user(alice);
people.add_active_user(bob);
people.add_active_user(cali);
people.add_active_user(alexus);
function test(label, f) {
run_test(label, ({override, override_rewire, mock_template}) => {
page_params.user_id = alice_user_id;
f({override, override_rewire, mock_template});
});
}
test("open_reactions_popover (sent by me)", () => {
message_lists.current.selected_message = () => ({sent_by_me: true});
$(".selected-row").set_find_results(".actions_hover", ["action-stub"]);
let called = false;
emoji_picker.toggle_emoji_popover = (target, id) => {
called = true;
assert.equal(id, 42);
assert.equal(target, "action-stub");
};
assert.ok(reactions.open_reactions_popover());
assert.ok(called);
});
test("open_reactions_popover (not sent by me)", () => {
message_lists.current.selected_message = () => ({sent_by_me: false});
$(".selected-row").set_find_results(".reaction_button", ["reaction-stub"]);
let called = false;
emoji_picker.toggle_emoji_popover = (target, id) => {
called = true;
assert.equal(id, 42);
assert.equal(target, "reaction-stub");
};
assert.ok(reactions.open_reactions_popover());
assert.ok(called);
});
test("basics", () => {
const message = {...sample_message};
const result = reactions.get_message_reactions(message);
assert.ok(reactions.current_user_has_reacted_to_emoji(message, "unicode_emoji,1f642"));
assert.ok(!reactions.current_user_has_reacted_to_emoji(message, "bogus"));
result.sort((a, b) => a.count - b.count);
const expected_result = [
{
emoji_name: "frown",
reaction_type: "unicode_emoji",
emoji_code: "1f641",
local_id: "unicode_emoji,1f641",
count: 1,
user_ids: [7],
label: "translated: Cali reacted with :frown:",
emoji_alt_code: false,
class: "message_reaction",
is_realm_emoji: false,
},
{
emoji_name: "inactive_realm_emoji",
reaction_type: "realm_emoji",
emoji_code: "992",
local_id: "realm_emoji,992",
count: 1,
user_ids: [5],
label: "translated: You (click to remove) reacted with :inactive_realm_emoji:",
emoji_alt_code: false,
is_realm_emoji: true,
url: "/url/for/992",
still_url: "/still/url/for/992",
class: "message_reaction reacted",
},
{
emoji_name: "smile",
reaction_type: "unicode_emoji",
emoji_code: "1f642",
local_id: "unicode_emoji,1f642",
count: 2,
user_ids: [5, 6],
label: "translated: You (click to remove) and Bob van Roberts reacted with :smile:",
emoji_alt_code: false,
class: "message_reaction reacted",
is_realm_emoji: false,
},
{
emoji_name: "tada",
reaction_type: "unicode_emoji",
emoji_code: "1f389",
local_id: "unicode_emoji,1f389",
count: 2,
user_ids: [7, 8],
label: "translated: Cali and Alexus reacted with :tada:",
emoji_alt_code: false,
class: "message_reaction",
is_realm_emoji: false,
},
{
emoji_name: "rocket",
reaction_type: "unicode_emoji",
emoji_code: "1f680",
local_id: "unicode_emoji,1f680",
count: 3,
user_ids: [5, 6, 7],
label: "translated: You (click to remove), Bob van Roberts and Cali reacted with :rocket:",
emoji_alt_code: false,
class: "message_reaction reacted",
is_realm_emoji: false,
},
{
emoji_name: "wave",
reaction_type: "unicode_emoji",
emoji_code: "1f44b",
local_id: "unicode_emoji,1f44b",
count: 3,
user_ids: [6, 7, 8],
label: "translated: Bob van Roberts, Cali and Alexus reacted with :wave:",
emoji_alt_code: false,
class: "message_reaction",
is_realm_emoji: false,
},
];
assert.deepEqual(result, expected_result);
});
test("unknown realm emojis (add)", () => {
assert.throws(
() =>
reactions.view.insert_new_reaction({
reaction_type: "realm_emoji",
emoji_name: "false_emoji",
emoji_code: "broken",
user_ids: [alice.user_id],
}),
{
name: "Error",
message: "Cannot find realm emoji for code 'broken'.",
},
);
});
test("unknown realm emojis (insert)", () => {
assert.throws(
() =>
reactions.view.insert_new_reaction({
reaction_type: "realm_emoji",
emoji_name: "fake_emoji",
emoji_code: "bogus",
user_id: bob.user_id,
}),
{
name: "Error",
message: "Cannot find realm emoji for code 'bogus'.",
},
);
});
test("sending", ({override, override_rewire}) => {
const message = {...sample_message};
assert.equal(message.id, 1001);
override(message_store, "get", (message_id) => {
assert.equal(message_id, message.id);
return message;
});
let emoji_name = "smile"; // should be a current reaction
override_rewire(reactions, "add_reaction", () => {});
override_rewire(reactions, "remove_reaction", () => {});
{
const stub = make_stub();
channel.del = stub.f;
reactions.toggle_emoji_reaction(message.id, emoji_name);
assert.equal(stub.num_calls, 1);
const args = stub.get_args("args").args;
assert.equal(args.url, "/json/messages/1001/reactions");
assert.deepEqual(args.data, {
reaction_type: "unicode_emoji",
emoji_name: "smile",
emoji_code: "1f642",
});
// args.success() does nothing; just make sure it doesn't crash
args.success();
// similarly, we only exercise the failure codepath
// Since this path calls blueslip.warn, we need to handle it.
blueslip.expect("warn", "XHR error message.");
channel.xhr_error_message = () => "XHR error message.";
args.error();
}
emoji_name = "alien"; // not set yet
{
const stub = make_stub();
channel.post = stub.f;
reactions.toggle_emoji_reaction(message.id, emoji_name);
assert.equal(stub.num_calls, 1);
const args = stub.get_args("args").args;
assert.equal(args.url, "/json/messages/1001/reactions");
assert.deepEqual(args.data, {
reaction_type: "unicode_emoji",
emoji_name: "alien",
emoji_code: "1f47d",
});
}
emoji_name = "inactive_realm_emoji";
{
// Test removing a deactivated realm emoji. A user can interact with a
// deactivated realm emoji only by clicking on a reaction, hence, only
// `process_reaction_click()` codepath supports deleting/adding a deactivated
// realm emoji.
const stub = make_stub();
channel.del = stub.f;
reactions.process_reaction_click(message.id, "realm_emoji,992");
assert.equal(stub.num_calls, 1);
const args = stub.get_args("args").args;
assert.equal(args.url, "/json/messages/1001/reactions");
assert.deepEqual(args.data, {
reaction_type: "realm_emoji",
emoji_name: "inactive_realm_emoji",
emoji_code: "992",
});
}
emoji_name = "zulip"; // Test adding zulip emoji.
{
const stub = make_stub();
channel.post = stub.f;
reactions.toggle_emoji_reaction(message.id, emoji_name);
assert.equal(stub.num_calls, 1);
const args = stub.get_args("args").args;
assert.equal(args.url, "/json/messages/1001/reactions");
assert.deepEqual(args.data, {
reaction_type: "zulip_extra_emoji",
emoji_name: "zulip",
emoji_code: "zulip",
url: "/static/generated/emoji/images/emoji/unicode/zulip.png",
});
}
emoji_name = "unknown-emoji"; // Test sending an emoji unknown to frontend.
assert.throws(() => reactions.toggle_emoji_reaction(message.id, emoji_name), {
name: "Error",
message: "Bad emoji name: unknown-emoji",
});
});
test("set_reaction_count", () => {
const $count_element = $.create("count-stub");
const $reaction_element = $.create("reaction-stub");
$reaction_element.set_find_results(".message_reaction_count", $count_element);
reactions.set_reaction_count($reaction_element, 5);
assert.equal($count_element.text(), "5");
});
test("find_reaction", ({override_rewire}) => {
const message_id = 99;
const local_id = "unicode_emoji,1f44b";
const $reaction_section = $.create("section-stub");
const reaction_stub = "reaction-stub";
$reaction_section.set_find_results(
`[data-reaction-id='${CSS.escape(local_id)}']`,
reaction_stub,
);
override_rewire(reactions, "get_reaction_section", (arg) => {
assert.equal(arg, message_id);
return $reaction_section;
});
assert.equal(reactions.find_reaction(message_id, local_id), reaction_stub);
});
test("get_reaction_section", () => {
const $message_table = $.create(".message_table");
const $message_row = $.create("some-message-row");
const $message_reactions = $.create("our-reactions-section");
$message_table.set_find_results(`[zid='${CSS.escape(555)}']`, $message_row);
$message_row.set_find_results(".message_reactions", $message_reactions);
const $section = reactions.get_reaction_section(555);
assert.equal($section, $message_reactions);
});
test("emoji_reaction_title", ({override}) => {
const message = {...sample_message};
override(message_store, "get", () => message);
const local_id = "unicode_emoji,1f642";
assert.equal(
reactions.get_reaction_title_data(message.id, local_id),
"translated: You (click to remove) and Bob van Roberts reacted with :smile:",
);
});
test("add_reaction/remove_reaction", ({override}) => {
// This function tests reaction events by mocking out calls to
// the view.
const message = {
id: 2001,
reactions: [],
};
override(message_store, "get", () => message);
let view_calls = [];
override(reactions.view, "insert_new_reaction", (opts) => {
view_calls.push({name: "insert_new_reaction", opts});
});
override(reactions.view, "update_existing_reaction", (opts) => {
view_calls.push({name: "update_existing_reaction", opts});
});
override(reactions.view, "remove_reaction", (opts) => {
view_calls.push({name: "remove_reaction", opts});
});
function test_view_calls(test_params) {
view_calls = [];
test_params.run_code();
assert.deepEqual(view_calls, test_params.expected_view_calls);
assert.deepEqual(
new Set(reactions.get_emojis_used_by_user_for_message_id(message.message_id)),
new Set(test_params.alice_emojis),
);
}
const alice_8ball_event = {
message_id: 2001,
reaction_type: "unicode_emoji",
emoji_name: "8ball",
emoji_code: "1f3b1",
user_id: alice.user_id,
};
const bob_8ball_event = {
message_id: 2001,
reaction_type: "unicode_emoji",
emoji_name: "8ball",
emoji_code: "1f3b1",
user_id: bob.user_id,
};
const cali_airplane_event = {
message_id: 2001,
reaction_type: "unicode_emoji",
emoji_name: "airplane",
emoji_code: "2708",
user_id: cali.user_id,
};
test_view_calls({
run_code() {
reactions.add_reaction(alice_8ball_event);
},
expected_view_calls: [
{
name: "insert_new_reaction",
opts: {
message_id: 2001,
reaction_type: "unicode_emoji",
emoji_name: "8ball",
emoji_code: "1f3b1",
user_id: alice.user_id,
},
},
],
alice_emojis: ["8ball"],
});
// Add redundant reaction.
test_view_calls({
run_code() {
reactions.add_reaction(alice_8ball_event);
},
expected_view_calls: [],
alice_emojis: ["8ball"],
});
test_view_calls({
run_code() {
reactions.add_reaction(bob_8ball_event);
},
expected_view_calls: [
{
name: "update_existing_reaction",
opts: {
message_id: 2001,
reaction_type: "unicode_emoji",
emoji_name: "8ball",
emoji_code: "1f3b1",
user_id: bob.user_id,
user_list: [alice.user_id, bob.user_id],
},
},
],
alice_emojis: ["8ball"],
});
test_view_calls({
run_code() {
reactions.add_reaction(cali_airplane_event);
},
expected_view_calls: [
{
name: "insert_new_reaction",
opts: {
message_id: 2001,
reaction_type: "unicode_emoji",
emoji_name: "airplane",
emoji_code: "2708",
user_id: cali.user_id,
},
},
],
alice_emojis: ["8ball"],
});
test_view_calls({
run_code() {
reactions.remove_reaction(bob_8ball_event);
},
expected_view_calls: [
{
name: "remove_reaction",
opts: {
message_id: 2001,
reaction_type: "unicode_emoji",
emoji_name: "8ball",
emoji_code: "1f3b1",
user_id: bob.user_id,
user_list: [alice.user_id],
},
},
],
alice_emojis: ["8ball"],
});
test_view_calls({
run_code() {
reactions.remove_reaction(alice_8ball_event);
},
expected_view_calls: [
{
name: "remove_reaction",
opts: {
message_id: 2001,
reaction_type: "unicode_emoji",
emoji_name: "8ball",
emoji_code: "1f3b1",
user_id: alice.user_id,
user_list: [],
},
},
],
alice_emojis: [],
});
// Test redundant remove.
test_view_calls({
run_code() {
reactions.remove_reaction(alice_8ball_event);
},
expected_view_calls: [],
alice_emojis: [],
});
});
test("view.insert_new_reaction (me w/unicode emoji)", ({override_rewire, mock_template}) => {
const opts = {
message_id: 501,
reaction_type: "unicode_emoji",
emoji_name: "8ball",
emoji_code: "1f3b1",
user_id: alice.user_id,
};
const $message_reactions = $.create("our-reactions");
override_rewire(reactions, "get_reaction_section", (message_id) => {
assert.equal(message_id, opts.message_id);
return $message_reactions;
});
$message_reactions.find = (selector) => {
assert.equal(selector, ".reaction_button");
return "reaction-button-stub";
};
mock_template("message_reaction.hbs", false, (data) => {
assert.deepEqual(data, {
count: 1,
emoji_alt_code: false,
emoji_name: "8ball",
emoji_code: "1f3b1",
local_id: "unicode_emoji,1f3b1",
class: "message_reaction reacted",
message_id: opts.message_id,
label: "translated: You (click to remove) reacted with :8ball:",
reaction_type: opts.reaction_type,
is_realm_emoji: false,
});
return "<new-reaction-stub>";
});
let insert_called;
$("<new-reaction-stub>").insertBefore = (element) => {
assert.equal(element, "reaction-button-stub");
insert_called = true;
};
reactions.view.insert_new_reaction(opts);
assert.ok(insert_called);
});
test("view.insert_new_reaction (them w/zulip emoji)", ({override_rewire, mock_template}) => {
const opts = {
message_id: 502,
reaction_type: "realm_emoji",
emoji_name: "zulip",
emoji_code: "zulip",
user_id: bob.user_id,
};
const $message_reactions = $.create("our-reactions");
override_rewire(reactions, "get_reaction_section", (message_id) => {
assert.equal(message_id, opts.message_id);
return $message_reactions;
});
$message_reactions.find = (selector) => {
assert.equal(selector, ".reaction_button");
return "reaction-button-stub";
};
mock_template("message_reaction.hbs", false, (data) => {
assert.deepEqual(data, {
count: 1,
url: "/static/generated/emoji/images/emoji/unicode/zulip.png",
is_realm_emoji: true,
emoji_alt_code: false,
emoji_name: "zulip",
emoji_code: "zulip",
local_id: "realm_emoji,zulip",
class: "message_reaction",
message_id: opts.message_id,
label: "translated: Bob van Roberts reacted with :zulip:",
still_url: undefined,
reaction_type: opts.reaction_type,
});
return "<new-reaction-stub>";
});
let insert_called;
$("<new-reaction-stub>").insertBefore = (element) => {
assert.equal(element, "reaction-button-stub");
insert_called = true;
};
reactions.view.insert_new_reaction(opts);
assert.ok(insert_called);
});
test("view.update_existing_reaction (me)", ({override_rewire}) => {
const opts = {
message_id: 503,
reaction_type: "unicode_emoji",
emoji_name: "8ball",
emoji_code: "1f3b1",
user_id: alice.user_id,
user_list: [alice.user_id, bob.user_id],
};
const $our_reaction = $.create("our-reaction-stub");
override_rewire(reactions, "find_reaction", (message_id, local_id) => {
assert.equal(message_id, opts.message_id);
assert.equal(local_id, "unicode_emoji,1f3b1");
return $our_reaction;
});
override_rewire(reactions, "set_reaction_count", (reaction, count) => {
assert.equal(reaction, $our_reaction);
assert.equal(count, 2);
});
reactions.view.update_existing_reaction(opts);
assert.ok($our_reaction.hasClass("reacted"));
assert.equal(
$our_reaction.attr("aria-label"),
"translated: You (click to remove) and Bob van Roberts reacted with :8ball:",
);
});
test("view.update_existing_reaction (them)", ({override_rewire}) => {
const opts = {
message_id: 504,
reaction_type: "unicode_emoji",
emoji_name: "8ball",
emoji_code: "1f3b1",
user_id: bob.user_id,
user_list: [alice.user_id, bob.user_id, cali.user_id, alexus.user_id],
};
const $our_reaction = $.create("our-reaction-stub");
override_rewire(reactions, "find_reaction", (message_id, local_id) => {
assert.equal(message_id, opts.message_id);
assert.equal(local_id, "unicode_emoji,1f3b1");
return $our_reaction;
});
override_rewire(reactions, "set_reaction_count", (reaction, count) => {
assert.equal(reaction, $our_reaction);
assert.equal(count, 4);
});
reactions.view.update_existing_reaction(opts);
assert.ok(!$our_reaction.hasClass("reacted"));
assert.equal(
$our_reaction.attr("aria-label"),
"translated: You (click to remove), Bob van Roberts, Cali and Alexus reacted with :8ball:",
);
});
test("view.remove_reaction (me)", ({override_rewire}) => {
const opts = {
message_id: 505,
reaction_type: "unicode_emoji",
emoji_name: "8ball",
emoji_code: "1f3b1",
user_id: alice.user_id,
user_list: [bob.user_id, cali.user_id],
};
const $our_reaction = $.create("our-reaction-stub");
$our_reaction.addClass("reacted");
override_rewire(reactions, "find_reaction", (message_id, local_id) => {
assert.equal(message_id, opts.message_id);
assert.equal(local_id, "unicode_emoji,1f3b1");
return $our_reaction;
});
override_rewire(reactions, "set_reaction_count", (reaction, count) => {
assert.equal(reaction, $our_reaction);
assert.equal(count, 2);
});
reactions.view.remove_reaction(opts);
assert.ok(!$our_reaction.hasClass("reacted"));
assert.equal(
$our_reaction.attr("aria-label"),
"translated: Bob van Roberts and Cali reacted with :8ball:",
);
});
test("view.remove_reaction (them)", ({override_rewire}) => {
const opts = {
message_id: 506,
reaction_type: "unicode_emoji",
emoji_name: "8ball",
emoji_code: "1f3b1",
user_id: bob.user_id,
user_list: [alice.user_id],
};
const $our_reaction = $.create("our-reaction-stub");
$our_reaction.addClass("reacted");
override_rewire(reactions, "find_reaction", (message_id, local_id) => {
assert.equal(message_id, opts.message_id);
assert.equal(local_id, "unicode_emoji,1f3b1");
return $our_reaction;
});
override_rewire(reactions, "set_reaction_count", (reaction, count) => {
assert.equal(reaction, $our_reaction);
assert.equal(count, 1);
});
$our_reaction.addClass("reacted");
reactions.view.remove_reaction(opts);
assert.ok($our_reaction.hasClass("reacted"));
assert.equal(
$our_reaction.attr("aria-label"),
"translated: You (click to remove) reacted with :8ball:",
);
});
test("view.remove_reaction (last person)", ({override_rewire}) => {
const opts = {
message_id: 507,
reaction_type: "unicode_emoji",
emoji_name: "8ball",
emoji_code: "1f3b1",
user_id: bob.user_id,
user_list: [],
};
const $our_reaction = $.create("our-reaction-stub");
override_rewire(reactions, "find_reaction", (message_id, local_id) => {
assert.equal(message_id, opts.message_id);
assert.equal(local_id, "unicode_emoji,1f3b1");
return $our_reaction;
});
let removed;
$our_reaction.remove = () => {
removed = true;
};
reactions.view.remove_reaction(opts);
assert.ok(removed);
});
test("error_handling", ({override, override_rewire}) => {
override(message_store, "get", () => {});
blueslip.expect("error", "reactions: Bad message id: 55");
const bogus_event = {
message_id: 55,
reaction_type: "realm_emoji",
emoji_name: "realm_emoji",
emoji_code: "991",
user_id: 99,
};
override_rewire(reactions, "current_user_has_reacted_to_emoji", () => true);
reactions.toggle_emoji_reaction(55, bogus_event.emoji_name);
reactions.add_reaction(bogus_event);
reactions.remove_reaction(bogus_event);
});
test("remove spurious user", ({override}) => {
// get coverage for removing non-user (it should just
// silently fail)
const message = {...sample_message};
override(message_store, "get", () => message);
const event = {
reaction_type: "unicode_emoji",
emoji_name: "frown",
emoji_code: "1f641",
message_id: message.id,
user_id: alice.user_id,
};
reactions.remove_reaction(event);
});
test("remove last user", ({override}) => {
const message = {...sample_message};
override(message_store, "get", () => message);
override(reactions.view, "remove_reaction", () => {});
function assert_names(names) {
assert.deepEqual(
reactions.get_message_reactions(message).map((r) => r.emoji_name),
names,
);
}
assert_names(["smile", "frown", "tada", "rocket", "wave", "inactive_realm_emoji"]);
const event = {
reaction_type: "unicode_emoji",
emoji_name: "frown",
emoji_code: "1f641",
message_id: message.id,
user_id: cali.user_id,
};
reactions.remove_reaction(event);
assert_names(["smile", "tada", "rocket", "wave", "inactive_realm_emoji"]);
});
test("local_reaction_id", () => {
const reaction_info = {
reaction_type: "unicode_emoji",
emoji_code: "1f44d",
};
const local_id = reactions.get_local_reaction_id(reaction_info);
assert.equal(local_id, "unicode_emoji,1f44d");
});
test("process_reaction_click", ({override}) => {
override(reactions.view, "remove_reaction", () => {});
const message = {...sample_message};
override(message_store, "get", () => message);
const expected_reaction_info = {
reaction_type: "unicode_emoji",
emoji_name: "smile",
emoji_code: "1f642",
};
// Test spectator cannot react.
page_params.is_spectator = true;
let stub = make_stub();
spectators.login_to_access = stub.f;
reactions.process_reaction_click(message.id, "unicode_emoji,1f642");
let args = stub.get_args("args").args;
assert.equal(args, undefined);
page_params.is_spectator = false;
stub = make_stub();
channel.del = stub.f;
reactions.process_reaction_click(message.id, "unicode_emoji,1f642");
assert.equal(stub.num_calls, 1);
args = stub.get_args("args").args;
assert.equal(args.url, "/json/messages/1001/reactions");
assert.deepEqual(args.data, expected_reaction_info);
});
test("warnings", () => {
const message = {
id: 3001,
reactions: [
{emoji_name: "smile", user_id: 5, reaction_type: "unicode_emoji", emoji_code: "1f642"},
// add some bogus user_ids
{
emoji_name: "octopus",
user_id: 8888,
reaction_type: "unicode_emoji",
emoji_code: "1f419",
},
{
emoji_name: "frown",
user_id: 9999,
reaction_type: "unicode_emoji",
emoji_code: "1f641",
},
],
};
blueslip.expect("warn", "Unknown user_id 8888 in reaction for message 3001");
blueslip.expect("warn", "Unknown user_id 9999 in reaction for message 3001");
reactions.get_message_reactions(message);
});
test("code coverage", ({override}) => {
/*
We just silently fail in a few places in the reaction
code, since events may come for messages that we don't
have yet, or reactions may be for deactivated users, etc.
Here we just cheaply ensure 100% line coverage to make
it easy to enforce 100% coverage for more significant
code additions.
*/
override(message_store, "get", (id) => {
assert.equal(id, 42);
return {
reactions: [],
};
});
reactions.remove_reaction({
message_id: 42, // TODO: REACTIONS API
});
});
test("duplicates", () => {
const dup_reaction_message = {
id: 1001,
reactions: [
{emoji_name: "smile", user_id: 5, reaction_type: "unicode_emoji", emoji_code: "1f642"},