2017-05-28 16:18:12 +02:00
|
|
|
set_global('document', 'document-stub');
|
|
|
|
set_global('$', global.make_zjquery());
|
2017-03-26 20:20:42 +02:00
|
|
|
|
2017-11-08 18:40:11 +01:00
|
|
|
zrequire('people');
|
|
|
|
zrequire('reactions');
|
2017-03-26 20:20:42 +02:00
|
|
|
|
|
|
|
set_global('emoji', {
|
2020-02-06 00:08:06 +01:00
|
|
|
all_realm_emojis: new Map(Object.entries({
|
2018-03-11 18:55:20 +01:00
|
|
|
991: {
|
2017-12-15 16:54:07 +01:00
|
|
|
id: '991',
|
2017-06-23 23:41:41 +02:00
|
|
|
emoji_name: 'realm_emoji',
|
|
|
|
emoji_url: 'TBD',
|
|
|
|
deactivated: false,
|
|
|
|
},
|
2018-03-11 18:55:20 +01:00
|
|
|
992: {
|
2017-12-15 16:54:07 +01:00
|
|
|
id: '992',
|
2017-10-08 17:02:14 +02:00
|
|
|
emoji_name: 'inactive_realm_emoji',
|
|
|
|
emoji_url: 'TBD',
|
|
|
|
deactivated: true,
|
|
|
|
},
|
2017-10-31 22:33:28 +01:00
|
|
|
zulip: {
|
2017-12-15 16:54:07 +01:00
|
|
|
id: 'zulip',
|
2017-10-31 22:33:28 +01:00
|
|
|
emoji_name: 'zulip',
|
|
|
|
emoji_url: 'TBD',
|
|
|
|
deactivated: false,
|
|
|
|
},
|
2020-02-06 00:08:06 +01:00
|
|
|
})),
|
2020-02-06 00:17:30 +01:00
|
|
|
active_realm_emojis: new Map(Object.entries({
|
2017-06-23 23:41:41 +02:00
|
|
|
realm_emoji: {
|
2017-12-15 16:54:07 +01:00
|
|
|
id: '991',
|
2017-06-23 23:41:41 +02:00
|
|
|
emoji_name: 'realm_emoji',
|
|
|
|
emoji_url: 'TBD',
|
|
|
|
},
|
2017-10-31 22:33:28 +01:00
|
|
|
zulip: {
|
2017-12-15 16:54:07 +01:00
|
|
|
id: 'zulip',
|
2017-10-31 22:33:28 +01:00
|
|
|
emoji_name: 'zulip',
|
|
|
|
emoji_url: 'TBD',
|
|
|
|
},
|
2020-02-06 00:17:30 +01:00
|
|
|
})),
|
2017-10-08 17:02:14 +02:00
|
|
|
deactivated_realm_emojis: {
|
|
|
|
inactive_realm_emoji: {
|
|
|
|
emoji_name: 'inactive_realm_emoji',
|
|
|
|
emoji_url: 'TBD',
|
|
|
|
},
|
|
|
|
},
|
2017-03-26 20:20:42 +02:00
|
|
|
});
|
|
|
|
|
2018-07-10 09:14:51 +02:00
|
|
|
set_global('blueslip', global.make_zblueslip());
|
2017-03-26 20:38:47 +02:00
|
|
|
|
2017-05-11 17:00:24 +02:00
|
|
|
set_global('page_params', {user_id: 5});
|
2017-03-26 20:20:42 +02:00
|
|
|
|
2017-05-28 16:18:12 +02:00
|
|
|
set_global('channel', {});
|
2017-05-29 21:36:24 +02:00
|
|
|
set_global('emoji_picker', {
|
|
|
|
hide_emoji_popover: function () {},
|
|
|
|
});
|
2017-05-28 16:18:12 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const alice = {
|
2017-05-28 17:04:30 +02:00
|
|
|
email: 'alice@example.com',
|
|
|
|
user_id: 5,
|
|
|
|
full_name: 'Alice',
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
const bob = {
|
2017-05-28 17:04:30 +02:00
|
|
|
email: 'bob@example.com',
|
|
|
|
user_id: 6,
|
|
|
|
full_name: 'Bob van Roberts',
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
const cali = {
|
2017-05-28 17:04:30 +02:00
|
|
|
email: 'cali@example.com',
|
|
|
|
user_id: 7,
|
|
|
|
full_name: 'Cali',
|
|
|
|
};
|
|
|
|
people.add_in_realm(alice);
|
|
|
|
people.add_in_realm(bob);
|
|
|
|
people.add_in_realm(cali);
|
2017-03-26 20:20:42 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const message = {
|
2017-05-28 16:18:12 +02:00
|
|
|
id: 1001,
|
|
|
|
reactions: [
|
2020-02-06 07:07:10 +01:00
|
|
|
{emoji_name: 'smile', user: {id: 5}, reaction_type: 'unicode_emoji', emoji_code: '263a'},
|
|
|
|
{emoji_name: 'smile', user: {id: 6}, reaction_type: 'unicode_emoji', emoji_code: '263a'},
|
|
|
|
{emoji_name: 'frown', user: {id: 7}, reaction_type: 'unicode_emoji', emoji_code: '1f641'},
|
2017-10-08 17:02:14 +02:00
|
|
|
{emoji_name: 'inactive_realm_emoji', user: {id: 5}, reaction_type: 'realm_emoji',
|
2017-12-15 16:54:07 +01:00
|
|
|
emoji_code: '992'},
|
2017-03-26 20:20:42 +02:00
|
|
|
|
2017-05-28 16:18:12 +02:00
|
|
|
// add some bogus user_ids
|
2017-10-31 22:33:28 +01:00
|
|
|
{emoji_name: 'octopus', user: {id: 8888}, reaction_type: 'unicode_emoji', emoji_code: '1f419'},
|
2020-02-06 07:07:10 +01:00
|
|
|
{emoji_name: 'frown', user: {id: 9999}, reaction_type: 'unicode_emoji', emoji_code: '1f641'},
|
2017-05-28 16:18:12 +02:00
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
set_global('message_store', {
|
|
|
|
get: function (message_id) {
|
|
|
|
assert.equal(message_id, 1001);
|
|
|
|
return message;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2017-09-18 22:06:39 +02:00
|
|
|
set_global('current_msg_list', {
|
|
|
|
selected_message: function () {
|
|
|
|
return { sent_by_me: true };
|
|
|
|
},
|
|
|
|
selected_row: function () {
|
|
|
|
return $('.selected-row');
|
|
|
|
},
|
|
|
|
selected_id: function () {
|
|
|
|
return 42;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('open_reactions_popover', () => {
|
2017-09-18 22:06:39 +02:00
|
|
|
$('.selected-row').set_find_results('.actions_hover', $('.target-action'));
|
|
|
|
$('.selected-row').set_find_results('.reaction_button', $('.target-reaction'));
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let called = false;
|
2017-09-18 22:06:39 +02:00
|
|
|
emoji_picker.toggle_emoji_popover = function (target, id) {
|
|
|
|
called = true;
|
|
|
|
assert.equal(id, 42);
|
|
|
|
assert.equal(target, $('.target-reaction')[0]);
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(reactions.open_reactions_popover());
|
|
|
|
assert(called);
|
|
|
|
|
|
|
|
current_msg_list.selected_message = function () { return { sent_by_me: false }; };
|
|
|
|
|
|
|
|
called = false;
|
|
|
|
emoji_picker.toggle_emoji_popover = function (target, id) {
|
|
|
|
called = true;
|
|
|
|
assert.equal(id, 42);
|
|
|
|
assert.equal(target, $('.target-action')[0]);
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(reactions.open_reactions_popover());
|
|
|
|
assert(called);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-09-18 22:06:39 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('basics', () => {
|
2018-07-10 09:14:51 +02:00
|
|
|
blueslip.set_test_data('warn', 'Unknown user_id 8888 in reaction for message 1001');
|
|
|
|
blueslip.set_test_data('warn', 'Unknown user_id 9999 in reaction for message 1001');
|
2019-11-02 00:06:25 +01:00
|
|
|
const result = reactions.get_message_reactions(message);
|
2018-07-10 09:14:51 +02:00
|
|
|
assert.equal(blueslip.get_test_logs('warn').length, 2);
|
|
|
|
blueslip.clear_test_data();
|
2020-02-06 07:07:10 +01:00
|
|
|
assert(reactions.current_user_has_reacted_to_emoji(message, '263a', 'unicode_emoji'));
|
|
|
|
assert(!reactions.current_user_has_reacted_to_emoji(message, '1f641', 'unicode_emoji'));
|
2017-05-11 17:00:24 +02:00
|
|
|
|
|
|
|
result.sort(function (a, b) { return a.count - b.count; });
|
2017-03-26 20:20:42 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const expected_result = [
|
2018-05-07 03:30:13 +02:00
|
|
|
{
|
|
|
|
emoji_name: 'frown',
|
|
|
|
reaction_type: 'unicode_emoji',
|
2020-02-06 07:07:10 +01:00
|
|
|
emoji_code: '1f641',
|
|
|
|
local_id: 'unicode_emoji,frown,1f641',
|
2018-05-07 03:30:13 +02:00
|
|
|
count: 1,
|
|
|
|
user_ids: [7],
|
2019-04-04 14:56:54 +02:00
|
|
|
label: 'Cali reacted with :frown:',
|
2018-05-07 03:30:13 +02:00
|
|
|
emoji_alt_code: false,
|
|
|
|
class: 'message_reaction',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
emoji_name: 'inactive_realm_emoji',
|
|
|
|
reaction_type: 'realm_emoji',
|
|
|
|
emoji_code: '992',
|
|
|
|
local_id: 'realm_emoji,inactive_realm_emoji,992',
|
|
|
|
count: 1,
|
|
|
|
user_ids: [5],
|
2019-04-04 14:56:54 +02:00
|
|
|
label: 'You (click to remove) reacted with :inactive_realm_emoji:',
|
2018-05-07 03:30:13 +02:00
|
|
|
emoji_alt_code: false,
|
|
|
|
is_realm_emoji: true,
|
|
|
|
url: 'TBD',
|
|
|
|
class: 'message_reaction reacted',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
emoji_name: 'smile',
|
|
|
|
reaction_type: 'unicode_emoji',
|
2020-02-06 07:07:10 +01:00
|
|
|
emoji_code: '263a',
|
|
|
|
local_id: 'unicode_emoji,smile,263a',
|
2018-05-07 03:30:13 +02:00
|
|
|
count: 2,
|
|
|
|
user_ids: [5, 6],
|
2019-04-04 14:56:54 +02:00
|
|
|
label: 'You (click to remove) and Bob van Roberts reacted with :smile:',
|
2018-05-07 03:30:13 +02:00
|
|
|
emoji_alt_code: false,
|
|
|
|
class: 'message_reaction reacted',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
assert.deepEqual(result, expected_result);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-05-28 16:18:12 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('sending', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const message_id = 1001; // see above for setup
|
|
|
|
let emoji_name = 'smile'; // should be a current reaction
|
2017-05-28 16:18:12 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const orig_remove_reaction = reactions.remove_reaction;
|
|
|
|
const orig_add_reaction = reactions.add_reaction;
|
2017-12-19 00:55:40 +01:00
|
|
|
reactions.remove_reaction = function () {};
|
|
|
|
reactions.add_reaction = function () {};
|
|
|
|
|
2017-05-28 16:18:12 +02:00
|
|
|
global.with_stub(function (stub) {
|
|
|
|
global.channel.del = stub.f;
|
2017-05-29 21:18:59 +02:00
|
|
|
reactions.toggle_emoji_reaction(message_id, emoji_name);
|
2019-11-02 00:06:25 +01:00
|
|
|
const args = stub.get_args('args').args;
|
2017-10-31 22:33:28 +01:00
|
|
|
assert.equal(args.url, '/json/messages/1001/reactions');
|
|
|
|
assert.deepEqual(args.data, {
|
|
|
|
reaction_type: 'unicode_emoji',
|
|
|
|
emoji_name: 'smile',
|
2020-02-06 07:07:10 +01:00
|
|
|
emoji_code: '263a',
|
2017-10-31 22:33:28 +01:00
|
|
|
});
|
2017-05-28 16:18:12 +02:00
|
|
|
// args.success() does nothing; just make sure it doesn't crash
|
|
|
|
args.success();
|
|
|
|
|
|
|
|
// similarly, we only exercise the failure codepath
|
2018-07-10 09:14:51 +02:00
|
|
|
// Since this path calls blueslip.warn, we need to handle it.
|
|
|
|
blueslip.set_test_data('warn', 'XHR Error Message.');
|
|
|
|
global.channel.xhr_error_message = function () {return 'XHR Error Message.';};
|
2017-05-28 16:18:12 +02:00
|
|
|
args.error();
|
2018-07-10 09:14:51 +02:00
|
|
|
assert.equal(blueslip.get_test_logs('warn').length, 1);
|
|
|
|
blueslip.clear_test_data();
|
2017-05-28 16:18:12 +02:00
|
|
|
});
|
|
|
|
emoji_name = 'alien'; // not set yet
|
|
|
|
global.with_stub(function (stub) {
|
2017-10-31 22:33:28 +01:00
|
|
|
global.channel.post = stub.f;
|
2017-05-29 21:18:59 +02:00
|
|
|
reactions.toggle_emoji_reaction(message_id, emoji_name);
|
2019-11-02 00:06:25 +01:00
|
|
|
const args = stub.get_args('args').args;
|
2017-10-31 22:33:28 +01:00
|
|
|
assert.equal(args.url, '/json/messages/1001/reactions');
|
|
|
|
assert.deepEqual(args.data, {
|
|
|
|
reaction_type: 'unicode_emoji',
|
|
|
|
emoji_name: 'alien',
|
|
|
|
emoji_code: '1f47d',
|
|
|
|
});
|
2017-05-28 16:18:12 +02:00
|
|
|
});
|
|
|
|
|
2017-12-15 16:54:07 +01:00
|
|
|
emoji_name = 'inactive_realm_emoji';
|
2017-10-08 17:02:14 +02:00
|
|
|
global.with_stub(function (stub) {
|
2017-12-15 16:54:07 +01:00
|
|
|
// Test removing a deactivated realm emoji. An 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.
|
2017-10-08 17:02:14 +02:00
|
|
|
global.channel.del = stub.f;
|
2017-12-15 16:54:07 +01:00
|
|
|
reactions.process_reaction_click(message_id, 'realm_emoji,inactive_realm_emoji,992');
|
2019-11-02 00:06:25 +01:00
|
|
|
const args = stub.get_args('args').args;
|
2017-10-31 22:33:28 +01:00
|
|
|
assert.equal(args.url, '/json/messages/1001/reactions');
|
|
|
|
assert.deepEqual(args.data, {
|
|
|
|
reaction_type: 'realm_emoji',
|
|
|
|
emoji_name: 'inactive_realm_emoji',
|
2017-12-15 16:54:07 +01:00
|
|
|
emoji_code: '992',
|
2017-10-31 22:33:28 +01:00
|
|
|
});
|
2017-10-08 17:02:14 +02:00
|
|
|
});
|
|
|
|
|
2017-10-31 22:33:28 +01:00
|
|
|
emoji_name = 'zulip'; // Test adding zulip emoji.
|
|
|
|
global.with_stub(function (stub) {
|
|
|
|
global.channel.post = stub.f;
|
|
|
|
reactions.toggle_emoji_reaction(message_id, emoji_name);
|
2019-11-02 00:06:25 +01:00
|
|
|
const args = stub.get_args('args').args;
|
2017-10-31 22:33:28 +01:00
|
|
|
assert.equal(args.url, '/json/messages/1001/reactions');
|
|
|
|
assert.deepEqual(args.data, {
|
|
|
|
reaction_type: 'zulip_extra_emoji',
|
|
|
|
emoji_name: 'zulip',
|
|
|
|
emoji_code: 'zulip',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
emoji_name = 'unknown-emoji'; // Test sending an emoji unknown to frontend.
|
2018-07-10 09:14:51 +02:00
|
|
|
blueslip.set_test_data('warn', 'Bad emoji name: ' + emoji_name);
|
2017-05-29 21:18:59 +02:00
|
|
|
reactions.toggle_emoji_reaction(message_id, emoji_name);
|
2018-07-10 09:14:51 +02:00
|
|
|
assert.equal(blueslip.get_test_logs('warn').length, 1);
|
|
|
|
blueslip.clear_test_data();
|
2017-12-19 00:55:40 +01:00
|
|
|
reactions.add_reaction = orig_add_reaction;
|
|
|
|
reactions.remove_reaction = orig_remove_reaction;
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-05-28 17:04:30 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('set_reaction_count', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const count_element = $.create('count-stub');
|
|
|
|
const reaction_element = $.create('reaction-stub');
|
2017-05-29 19:24:31 +02:00
|
|
|
|
2017-07-08 14:31:18 +02:00
|
|
|
reaction_element.set_find_results('.message_reaction_count', count_element);
|
2017-05-29 19:24:31 +02:00
|
|
|
|
|
|
|
reactions.set_reaction_count(reaction_element, 5);
|
|
|
|
|
2018-04-12 20:16:42 +02:00
|
|
|
assert.equal(count_element.text(), '5');
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-05-29 19:24:31 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('get_reaction_section', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const message_table = $.create('.message_table');
|
|
|
|
const message_row = $.create('some-message-row');
|
|
|
|
const message_reactions = $.create('our-reactions-section');
|
2017-06-29 17:19:14 +02:00
|
|
|
|
2017-07-08 14:31:18 +02:00
|
|
|
message_table.set_find_results("[zid='555']", message_row);
|
|
|
|
message_row.set_find_results('.message_reactions', message_reactions);
|
2017-06-29 17:19:14 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const section = reactions.get_reaction_section(555);
|
2017-06-29 17:19:14 +02:00
|
|
|
|
|
|
|
assert.equal(section, message_reactions);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-06-29 17:19:14 +02:00
|
|
|
|
2019-04-04 14:56:54 +02:00
|
|
|
run_test('emoji_reaction_title', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const message_id = 1001;
|
2020-02-06 07:07:10 +01:00
|
|
|
const local_id = 'unicode_emoji,smile,263a';
|
2019-04-04 14:56:54 +02:00
|
|
|
|
|
|
|
assert.equal(reactions.get_reaction_title_data(message_id, local_id),
|
|
|
|
"You (click to remove) and Bob van Roberts reacted with :smile:");
|
|
|
|
});
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('add_and_remove_reaction', () => {
|
2017-05-30 04:46:36 +02:00
|
|
|
// Insert 8ball for Alice.
|
2019-11-02 00:06:25 +01:00
|
|
|
let alice_event = {
|
2017-05-28 17:04:30 +02:00
|
|
|
message_id: 1001,
|
2017-10-31 22:33:28 +01:00
|
|
|
reaction_type: 'unicode_emoji',
|
2017-05-28 17:04:30 +02:00
|
|
|
emoji_name: '8ball',
|
2017-10-31 22:33:28 +01:00
|
|
|
emoji_code: '1f3b1',
|
2017-05-28 17:04:30 +02:00
|
|
|
user: {
|
|
|
|
user_id: alice.user_id,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const message_reactions = $.create('our-reactions');
|
2017-05-28 17:04:30 +02:00
|
|
|
|
2017-06-29 17:19:14 +02:00
|
|
|
reactions.get_reaction_section = function (message_id) {
|
|
|
|
assert.equal(message_id, 1001);
|
|
|
|
return message_reactions;
|
|
|
|
};
|
2017-05-28 17:04:30 +02:00
|
|
|
|
|
|
|
message_reactions.find = function (selector) {
|
|
|
|
assert.equal(selector, '.reaction_button');
|
|
|
|
return 'reaction-button-stub';
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let template_called;
|
2019-07-11 05:06:20 +02:00
|
|
|
global.stub_templates(function (template_name, data) {
|
2017-05-28 17:04:30 +02:00
|
|
|
template_called = true;
|
|
|
|
assert.equal(template_name, 'message_reaction');
|
2017-05-30 04:07:28 +02:00
|
|
|
assert.equal(data.class, 'message_reaction reacted');
|
|
|
|
assert(!data.is_realm_emoji);
|
2017-05-28 17:04:30 +02:00
|
|
|
assert.equal(data.message_id, 1001);
|
2019-04-04 14:56:54 +02:00
|
|
|
assert.equal(data.label, 'You (click to remove) reacted with :8ball:');
|
2017-07-08 15:16:19 +02:00
|
|
|
return '<new reaction html>';
|
2019-07-11 05:06:20 +02:00
|
|
|
});
|
2017-05-28 17:04:30 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let insert_called;
|
2017-07-08 15:16:19 +02:00
|
|
|
$('<new reaction html>').insertBefore = function (element) {
|
2017-05-28 17:04:30 +02:00
|
|
|
assert.equal(element, 'reaction-button-stub');
|
|
|
|
insert_called = true;
|
|
|
|
};
|
|
|
|
|
2017-05-29 20:13:58 +02:00
|
|
|
reactions.add_reaction(alice_event);
|
2017-05-28 17:04:30 +02:00
|
|
|
assert(template_called);
|
|
|
|
assert(insert_called);
|
|
|
|
|
2019-04-04 14:56:54 +02:00
|
|
|
// Testing tooltip title data for added reaction.
|
2019-11-02 00:06:25 +01:00
|
|
|
const local_id = 'unicode_emoji,8ball,1f3b1';
|
2019-04-04 14:56:54 +02:00
|
|
|
assert.equal(reactions.get_reaction_title_data(alice_event.message_id, local_id),
|
|
|
|
"You (click to remove) reacted with :8ball:");
|
|
|
|
|
2017-12-19 00:55:40 +01:00
|
|
|
// Running add_reaction again should not result in any changes
|
|
|
|
template_called = false;
|
|
|
|
insert_called = false;
|
|
|
|
reactions.add_reaction(alice_event);
|
|
|
|
assert(!template_called);
|
|
|
|
assert(!insert_called);
|
|
|
|
|
2017-05-30 04:46:36 +02:00
|
|
|
// Now, have Bob react to the same emoji (update).
|
2017-05-28 17:04:30 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const bob_event = {
|
2017-05-28 17:04:30 +02:00
|
|
|
message_id: 1001,
|
2017-10-31 22:33:28 +01:00
|
|
|
reaction_type: 'unicode_emoji',
|
2017-05-28 17:04:30 +02:00
|
|
|
emoji_name: '8ball',
|
2017-10-31 22:33:28 +01:00
|
|
|
emoji_code: '1f3b1',
|
2017-05-28 17:04:30 +02:00
|
|
|
user: {
|
|
|
|
user_id: bob.user_id,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const count_element = $.create('count-element');
|
|
|
|
const reaction_element = $.create('reaction-element');
|
2017-07-08 14:31:18 +02:00
|
|
|
reaction_element.set_find_results('.message_reaction_count', count_element);
|
2017-05-28 17:04:30 +02:00
|
|
|
|
|
|
|
message_reactions.find = function (selector) {
|
2017-10-31 22:33:28 +01:00
|
|
|
assert.equal(selector, "[data-reaction-id='unicode_emoji,8ball,1f3b1']");
|
2017-05-28 17:04:30 +02:00
|
|
|
return reaction_element;
|
|
|
|
};
|
|
|
|
|
2017-05-29 20:13:58 +02:00
|
|
|
reactions.add_reaction(bob_event);
|
2018-04-12 20:16:42 +02:00
|
|
|
assert.equal(count_element.text(), '2');
|
2017-05-28 17:04:30 +02:00
|
|
|
|
2017-05-29 20:13:58 +02:00
|
|
|
reactions.remove_reaction(bob_event);
|
2018-04-12 20:16:42 +02:00
|
|
|
assert.equal(count_element.text(), '1');
|
2017-05-29 20:13:58 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let current_emojis = reactions.get_emojis_used_by_user_for_message_id(1001);
|
2017-10-08 17:02:14 +02:00
|
|
|
assert.deepEqual(current_emojis, ['smile', 'inactive_realm_emoji', '8ball']);
|
2017-05-30 03:34:06 +02:00
|
|
|
|
2017-05-29 20:13:58 +02:00
|
|
|
// Next, remove Alice's reaction, which exercises removing the
|
|
|
|
// emoji icon.
|
2019-11-02 00:06:25 +01:00
|
|
|
let removed;
|
2017-05-29 20:13:58 +02:00
|
|
|
reaction_element.remove = function () {
|
|
|
|
removed = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
reactions.remove_reaction(alice_event);
|
|
|
|
assert(removed);
|
2017-05-30 03:34:06 +02:00
|
|
|
|
2017-12-19 00:55:40 +01:00
|
|
|
// Running remove_reaction again should not result in any changes
|
|
|
|
removed = false;
|
|
|
|
reactions.remove_reaction(alice_event);
|
|
|
|
assert(!removed);
|
|
|
|
|
2017-05-30 03:34:06 +02:00
|
|
|
current_emojis = reactions.get_emojis_used_by_user_for_message_id(1001);
|
2017-10-08 17:02:14 +02:00
|
|
|
assert.deepEqual(current_emojis, ['smile', 'inactive_realm_emoji']);
|
2017-05-30 04:07:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
// Now add Cali's realm_emoji reaction.
|
2019-11-02 00:06:25 +01:00
|
|
|
const cali_event = {
|
2017-05-30 04:07:28 +02:00
|
|
|
message_id: 1001,
|
2017-10-31 22:33:28 +01:00
|
|
|
reaction_type: 'realm_emoji',
|
2017-05-30 04:07:28 +02:00
|
|
|
emoji_name: 'realm_emoji',
|
2017-12-15 16:54:07 +01:00
|
|
|
emoji_code: '991',
|
2017-05-30 04:07:28 +02:00
|
|
|
user: {
|
|
|
|
user_id: cali.user_id,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
template_called = false;
|
2019-07-11 05:06:20 +02:00
|
|
|
global.stub_templates(function (template_name, data) {
|
2017-05-30 04:07:28 +02:00
|
|
|
assert.equal(data.class, 'message_reaction');
|
|
|
|
assert(data.is_realm_emoji);
|
|
|
|
template_called = true;
|
2017-07-08 15:16:19 +02:00
|
|
|
return '<new reaction html>';
|
2019-07-11 05:06:20 +02:00
|
|
|
});
|
2017-05-30 04:07:28 +02:00
|
|
|
|
|
|
|
message_reactions.find = function (selector) {
|
|
|
|
assert.equal(selector, '.reaction_button');
|
|
|
|
return 'reaction-button-stub';
|
|
|
|
};
|
|
|
|
|
|
|
|
reactions.add_reaction(cali_event);
|
|
|
|
assert(template_called);
|
2017-05-30 04:46:36 +02:00
|
|
|
assert(!reaction_element.hasClass('reacted'));
|
2017-05-30 04:07:28 +02:00
|
|
|
|
2017-05-30 04:46:36 +02:00
|
|
|
// And then have Alice update it.
|
|
|
|
alice_event = {
|
|
|
|
message_id: 1001,
|
2017-10-31 22:33:28 +01:00
|
|
|
reaction_type: 'realm_emoji',
|
2017-05-30 04:46:36 +02:00
|
|
|
emoji_name: 'realm_emoji',
|
2017-12-15 16:54:07 +01:00
|
|
|
emoji_code: '991',
|
2017-05-30 04:46:36 +02:00
|
|
|
user: {
|
|
|
|
user_id: alice.user_id,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
message_reactions.find = function (selector) {
|
2017-12-15 16:54:07 +01:00
|
|
|
assert.equal(selector, "[data-reaction-id='realm_emoji,realm_emoji,991']");
|
2017-05-30 04:46:36 +02:00
|
|
|
return reaction_element;
|
|
|
|
};
|
|
|
|
reaction_element.prop = function () {};
|
|
|
|
reactions.add_reaction(alice_event);
|
|
|
|
|
|
|
|
assert(reaction_element.hasClass('reacted'));
|
2018-07-10 09:14:51 +02:00
|
|
|
blueslip.set_test_data('warn', 'Unknown user_id 8888 in reaction for message 1001');
|
|
|
|
blueslip.set_test_data('warn', 'Unknown user_id 9999 in reaction for message 1001');
|
2019-11-02 00:06:25 +01:00
|
|
|
const result = reactions.get_message_reactions(message);
|
2018-07-10 09:14:51 +02:00
|
|
|
assert.equal(blueslip.get_test_logs('warn').length, 2);
|
|
|
|
blueslip.clear_test_data();
|
2020-02-08 03:33:46 +01:00
|
|
|
const realm_emoji_data = result.filter(v => v.emoji_name === 'realm_emoji')[0];
|
2017-05-30 23:24:21 +02:00
|
|
|
|
|
|
|
assert.equal(realm_emoji_data.count, 2);
|
|
|
|
assert.equal(realm_emoji_data.is_realm_emoji, true);
|
|
|
|
|
|
|
|
// And then remove Alice's reaction.
|
|
|
|
reactions.remove_reaction(alice_event);
|
|
|
|
assert(!reaction_element.hasClass('reacted'));
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-05-31 00:09:50 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('with_view_stubs', () => {
|
2017-06-28 22:47:47 +02:00
|
|
|
// This function tests reaction events by mocking out calls to
|
|
|
|
// the view.
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const message = {
|
2017-06-28 22:47:47 +02:00
|
|
|
id: 2001,
|
|
|
|
reactions: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
message_store.get = function () {
|
|
|
|
return message;
|
|
|
|
};
|
|
|
|
|
|
|
|
function test_view_calls(test_params) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const calls = [];
|
2017-06-28 22:47:47 +02:00
|
|
|
|
|
|
|
function add_call_func(name) {
|
|
|
|
return function (opts) {
|
|
|
|
calls.push({
|
|
|
|
name: name,
|
|
|
|
opts: opts,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
reactions.view = {
|
|
|
|
insert_new_reaction: add_call_func('insert_new_reaction'),
|
|
|
|
update_existing_reaction: add_call_func('update_existing_reaction'),
|
|
|
|
remove_reaction: add_call_func('remove_reaction'),
|
|
|
|
};
|
|
|
|
|
|
|
|
test_params.run_code();
|
|
|
|
|
|
|
|
assert.deepEqual(calls, test_params.expected_view_calls);
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const alice_8ball_event = {
|
2017-06-28 22:47:47 +02:00
|
|
|
message_id: 2001,
|
2017-10-31 22:33:28 +01:00
|
|
|
reaction_type: 'unicode_emoji',
|
2017-06-28 22:47:47 +02:00
|
|
|
emoji_name: '8ball',
|
2017-10-31 22:33:28 +01:00
|
|
|
emoji_code: '1f3b1',
|
2017-06-28 22:47:47 +02:00
|
|
|
user: {
|
|
|
|
user_id: alice.user_id,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const bob_8ball_event = {
|
2017-06-28 22:47:47 +02:00
|
|
|
message_id: 2001,
|
2017-10-31 22:33:28 +01:00
|
|
|
reaction_type: 'unicode_emoji',
|
2017-06-28 22:47:47 +02:00
|
|
|
emoji_name: '8ball',
|
2017-10-31 22:33:28 +01:00
|
|
|
emoji_code: '1f3b1',
|
2017-06-28 22:47:47 +02:00
|
|
|
user: {
|
|
|
|
user_id: bob.user_id,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const cali_airplane_event = {
|
2017-06-28 22:47:47 +02:00
|
|
|
message_id: 2001,
|
2017-10-31 22:33:28 +01:00
|
|
|
reaction_type: 'unicode_emoji',
|
2017-06-28 22:47:47 +02:00
|
|
|
emoji_name: 'airplane',
|
2017-10-31 22:33:28 +01:00
|
|
|
emoji_code: '2708',
|
2017-06-28 22:47:47 +02:00
|
|
|
user: {
|
|
|
|
user_id: cali.user_id,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
test_view_calls({
|
|
|
|
run_code: function () {
|
|
|
|
reactions.add_reaction(alice_8ball_event);
|
|
|
|
},
|
|
|
|
expected_view_calls: [
|
|
|
|
{
|
|
|
|
name: 'insert_new_reaction',
|
|
|
|
opts: {
|
|
|
|
message_id: 2001,
|
2017-10-31 22:33:28 +01:00
|
|
|
reaction_type: 'unicode_emoji',
|
2017-06-28 22:47:47 +02:00
|
|
|
emoji_name: '8ball',
|
2017-10-31 22:33:28 +01:00
|
|
|
emoji_code: '1f3b1',
|
2017-06-28 22:47:47 +02:00
|
|
|
user_id: alice.user_id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
test_view_calls({
|
|
|
|
run_code: function () {
|
|
|
|
reactions.add_reaction(bob_8ball_event);
|
|
|
|
},
|
|
|
|
expected_view_calls: [
|
|
|
|
{
|
|
|
|
name: 'update_existing_reaction',
|
|
|
|
opts: {
|
|
|
|
message_id: 2001,
|
2017-10-31 22:33:28 +01:00
|
|
|
reaction_type: 'unicode_emoji',
|
2017-06-28 22:47:47 +02:00
|
|
|
emoji_name: '8ball',
|
2017-10-31 22:33:28 +01:00
|
|
|
emoji_code: '1f3b1',
|
2017-06-28 22:47:47 +02:00
|
|
|
user_id: bob.user_id,
|
|
|
|
user_list: [alice.user_id, bob.user_id],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
test_view_calls({
|
|
|
|
run_code: function () {
|
|
|
|
reactions.add_reaction(cali_airplane_event);
|
|
|
|
},
|
|
|
|
expected_view_calls: [
|
|
|
|
{
|
|
|
|
name: 'insert_new_reaction',
|
|
|
|
opts: {
|
|
|
|
message_id: 2001,
|
2017-10-31 22:33:28 +01:00
|
|
|
reaction_type: 'unicode_emoji',
|
2017-06-28 22:47:47 +02:00
|
|
|
emoji_name: 'airplane',
|
2017-10-31 22:33:28 +01:00
|
|
|
emoji_code: '2708',
|
2017-06-28 22:47:47 +02:00
|
|
|
user_id: cali.user_id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
test_view_calls({
|
|
|
|
run_code: function () {
|
|
|
|
reactions.remove_reaction(bob_8ball_event);
|
|
|
|
},
|
|
|
|
expected_view_calls: [
|
|
|
|
{
|
|
|
|
name: 'remove_reaction',
|
|
|
|
opts: {
|
|
|
|
message_id: 2001,
|
2017-10-31 22:33:28 +01:00
|
|
|
reaction_type: 'unicode_emoji',
|
2017-06-28 22:47:47 +02:00
|
|
|
emoji_name: '8ball',
|
2017-10-31 22:33:28 +01:00
|
|
|
emoji_code: '1f3b1',
|
2017-06-28 22:47:47 +02:00
|
|
|
user_id: bob.user_id,
|
|
|
|
user_list: [alice.user_id],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
test_view_calls({
|
|
|
|
run_code: function () {
|
|
|
|
reactions.remove_reaction(alice_8ball_event);
|
|
|
|
},
|
|
|
|
expected_view_calls: [
|
|
|
|
{
|
|
|
|
name: 'remove_reaction',
|
|
|
|
opts: {
|
|
|
|
message_id: 2001,
|
2017-10-31 22:33:28 +01:00
|
|
|
reaction_type: 'unicode_emoji',
|
2017-06-28 22:47:47 +02:00
|
|
|
emoji_name: '8ball',
|
2017-10-31 22:33:28 +01:00
|
|
|
emoji_code: '1f3b1',
|
2017-06-28 22:47:47 +02:00
|
|
|
user_id: alice.user_id,
|
|
|
|
user_list: [],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-06-28 22:47:47 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('error_handling', () => {
|
2017-05-31 00:09:50 +02:00
|
|
|
global.message_store.get = function () {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
2018-07-10 09:14:51 +02:00
|
|
|
blueslip.set_test_data('error', 'reactions: Bad message id: 55');
|
2017-05-31 00:09:50 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const bogus_event = {
|
2017-05-31 00:09:50 +02:00
|
|
|
message_id: 55,
|
2017-12-15 16:54:07 +01:00
|
|
|
reaction_type: 'realm_emoji',
|
2017-05-31 00:09:50 +02:00
|
|
|
emoji_name: 'realm_emoji',
|
2017-12-15 16:54:07 +01:00
|
|
|
emoji_code: '991',
|
2017-05-31 00:09:50 +02:00
|
|
|
user: {
|
|
|
|
user_id: 99,
|
|
|
|
},
|
|
|
|
};
|
2017-10-31 22:33:28 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const original_func = reactions.current_user_has_reacted_to_emoji;
|
2017-10-31 22:33:28 +01:00
|
|
|
reactions.current_user_has_reacted_to_emoji = function () { return true; };
|
|
|
|
reactions.toggle_emoji_reaction(55, bogus_event.emoji_name);
|
2018-07-10 09:14:51 +02:00
|
|
|
assert.equal(blueslip.get_test_logs('error').length, 1);
|
2017-10-31 22:33:28 +01:00
|
|
|
reactions.current_user_has_reacted_to_emoji = original_func;
|
2018-07-10 09:14:51 +02:00
|
|
|
blueslip.clear_test_data();
|
2017-05-31 00:09:50 +02:00
|
|
|
|
|
|
|
reactions.add_reaction(bogus_event);
|
2018-07-10 09:14:51 +02:00
|
|
|
assert.equal(blueslip.get_test_logs('error').length, 0);
|
2017-05-31 00:09:50 +02:00
|
|
|
|
|
|
|
reactions.remove_reaction(bogus_event);
|
2018-07-10 09:14:51 +02:00
|
|
|
assert.equal(blueslip.get_test_logs('error').length, 0);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-10-31 22:33:28 +01:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('local_reaction_id', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const reaction_info = {
|
2017-10-31 22:33:28 +01:00
|
|
|
reaction_type: 'unicode_emoji',
|
|
|
|
emoji_name: 'thumbs_up',
|
|
|
|
emoji_code: '1f44d',
|
|
|
|
};
|
2019-11-02 00:06:25 +01:00
|
|
|
const local_id = reactions.get_local_reaction_id(reaction_info);
|
2017-10-31 22:33:28 +01:00
|
|
|
assert.equal(local_id, 'unicode_emoji,thumbs_up,1f44d');
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const reverse_info = reactions.get_reaction_info(local_id);
|
2017-10-31 22:33:28 +01:00
|
|
|
assert.deepEqual(reverse_info, reaction_info);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-10-31 22:33:28 +01:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('process_reaction_click', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const message_id = 1001;
|
|
|
|
let expected_reaction_info = {
|
2017-10-31 22:33:28 +01:00
|
|
|
reaction_type: 'unicode_emoji',
|
|
|
|
emoji_name: '8ball',
|
|
|
|
emoji_code: '1f3b1',
|
|
|
|
};
|
|
|
|
global.message_store.get = function (message_id) {
|
|
|
|
assert.equal(message_id, 1001);
|
|
|
|
return message;
|
|
|
|
};
|
|
|
|
|
|
|
|
global.with_stub(function (stub) {
|
|
|
|
global.channel.post = stub.f;
|
|
|
|
reactions.process_reaction_click(message_id, 'unicode_emoji,8ball,1f3b1');
|
2019-11-02 00:06:25 +01:00
|
|
|
const args = stub.get_args('args').args;
|
2017-10-31 22:33:28 +01:00
|
|
|
assert.equal(args.url, '/json/messages/1001/reactions');
|
|
|
|
assert.deepEqual(args.data, expected_reaction_info);
|
|
|
|
});
|
|
|
|
|
|
|
|
expected_reaction_info = {
|
|
|
|
reaction_type: 'unicode_emoji',
|
|
|
|
emoji_name: 'smile',
|
2020-02-06 07:07:10 +01:00
|
|
|
emoji_code: '263a',
|
2017-10-31 22:33:28 +01:00
|
|
|
};
|
|
|
|
global.with_stub(function (stub) {
|
|
|
|
global.channel.del = stub.f;
|
2020-02-06 07:07:10 +01:00
|
|
|
reactions.process_reaction_click(message_id, 'unicode_emoji,smile,263a');
|
2019-11-02 00:06:25 +01:00
|
|
|
const args = stub.get_args('args').args;
|
2017-10-31 22:33:28 +01:00
|
|
|
assert.equal(args.url, '/json/messages/1001/reactions');
|
|
|
|
assert.deepEqual(args.data, expected_reaction_info);
|
|
|
|
});
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|