2019-11-02 00:06:25 +01:00
|
|
|
const noop = function () {};
|
|
|
|
const return_true = function () { return true; };
|
2017-07-01 22:17:22 +02:00
|
|
|
set_global('$', global.make_zjquery());
|
2017-07-08 15:16:19 +02:00
|
|
|
set_global('document', 'document-stub');
|
2020-02-04 21:50:55 +01:00
|
|
|
set_global('i18n', global.stub_i18n);
|
2017-07-01 22:17:22 +02:00
|
|
|
|
|
|
|
set_global('colorspace', {
|
|
|
|
sRGB_to_linear: noop,
|
|
|
|
luminance_to_lightness: function () {
|
|
|
|
return 1;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2018-08-04 19:44:31 +02:00
|
|
|
zrequire('people');
|
2017-11-08 18:58:08 +01:00
|
|
|
zrequire('stream_data');
|
|
|
|
zrequire('stream_events');
|
2019-11-02 00:06:25 +01:00
|
|
|
const with_overrides = global.with_overrides;
|
2017-07-01 22:17:22 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const george = {
|
2018-08-04 19:44:31 +02:00
|
|
|
email: 'george@zulip.com',
|
|
|
|
full_name: 'George',
|
|
|
|
user_id: 103,
|
|
|
|
};
|
|
|
|
people.add(george);
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const frontend = {
|
2017-07-03 21:55:03 +02:00
|
|
|
subscribed: false,
|
2017-07-01 22:17:22 +02:00
|
|
|
color: 'yellow',
|
|
|
|
name: 'frontend',
|
|
|
|
stream_id: 1,
|
2019-05-15 08:54:25 +02:00
|
|
|
is_muted: true,
|
2017-07-01 22:17:22 +02:00
|
|
|
invite_only: false,
|
|
|
|
};
|
2018-08-04 19:44:31 +02:00
|
|
|
|
|
|
|
stream_data.add_sub(frontend.name, frontend);
|
2017-07-01 22:17:22 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('update_property', () => {
|
2017-07-01 22:17:22 +02:00
|
|
|
// Invoke error for non-existent stream/property
|
|
|
|
with_overrides(function (override) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let errors = 0;
|
2017-07-01 22:17:22 +02:00
|
|
|
override('blueslip.warn', function () {
|
|
|
|
errors += 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
stream_events.update_property(99, 'color', 'blue');
|
|
|
|
assert.equal(errors, 1);
|
|
|
|
|
|
|
|
stream_events.update_property(1, 'not_real', 42);
|
|
|
|
assert.equal(errors, 2);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Test update color
|
|
|
|
with_overrides(function (override) {
|
|
|
|
global.with_stub(function (stub) {
|
|
|
|
override('stream_color.update_stream_color', stub.f);
|
|
|
|
stream_events.update_property(1, 'color', 'blue');
|
2019-11-02 00:06:25 +01:00
|
|
|
const args = stub.get_args('sub', 'val');
|
2017-07-01 22:17:22 +02:00
|
|
|
assert.equal(args.sub.stream_id, 1);
|
|
|
|
assert.equal(args.val, 'blue');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Test in home view
|
|
|
|
with_overrides(function (override) {
|
|
|
|
global.with_stub(function (stub) {
|
2019-05-21 10:41:53 +02:00
|
|
|
override('stream_muting.update_is_muted', stub.f);
|
|
|
|
stream_events.update_property(1, 'in_home_view', false);
|
2019-11-02 00:06:25 +01:00
|
|
|
const args = stub.get_args('sub', 'val');
|
2017-07-01 22:17:22 +02:00
|
|
|
assert.equal(args.sub.stream_id, 1);
|
|
|
|
assert.equal(args.val, true);
|
|
|
|
});
|
|
|
|
});
|
2017-07-03 19:57:24 +02:00
|
|
|
|
|
|
|
// Test desktop notifications
|
|
|
|
stream_events.update_property(1, 'desktop_notifications', true);
|
|
|
|
assert.equal(frontend.desktop_notifications, true);
|
2019-11-02 00:06:25 +01:00
|
|
|
let checkbox = $("#desktop_notifications_1");
|
2017-07-03 19:57:24 +02:00
|
|
|
assert.equal(checkbox.prop('checked'), true);
|
|
|
|
|
|
|
|
// Tests audible notifications
|
|
|
|
stream_events.update_property(1, 'audible_notifications', true);
|
|
|
|
assert.equal(frontend.audible_notifications, true);
|
2019-06-14 19:29:48 +02:00
|
|
|
checkbox = $("#audible_notifications_1");
|
2017-07-03 19:57:24 +02:00
|
|
|
assert.equal(checkbox.prop('checked'), true);
|
|
|
|
|
2017-08-17 16:55:32 +02:00
|
|
|
// Tests push notifications
|
|
|
|
stream_events.update_property(1, 'push_notifications', true);
|
|
|
|
assert.equal(frontend.push_notifications, true);
|
2019-06-14 19:29:48 +02:00
|
|
|
checkbox = $("#push_notifications_1");
|
2017-08-17 16:55:32 +02:00
|
|
|
assert.equal(checkbox.prop('checked'), true);
|
|
|
|
|
2017-11-21 05:58:26 +01:00
|
|
|
// Tests email notifications
|
|
|
|
stream_events.update_property(1, 'email_notifications', true);
|
|
|
|
assert.equal(frontend.email_notifications, true);
|
2019-06-14 19:29:48 +02:00
|
|
|
checkbox = $("#email_notifications_1");
|
2017-11-21 05:58:26 +01:00
|
|
|
assert.equal(checkbox.prop('checked'), true);
|
|
|
|
|
2019-11-26 02:37:12 +01:00
|
|
|
// Tests wildcard_mentions_notify notifications
|
|
|
|
stream_events.update_property(1, 'wildcard_mentions_notify', true);
|
|
|
|
assert.equal(frontend.wildcard_mentions_notify, true);
|
|
|
|
checkbox = $("#wildcard_mentions_notify_1");
|
|
|
|
assert.equal(checkbox.prop('checked'), true);
|
|
|
|
|
2017-07-03 19:57:24 +02:00
|
|
|
// Test name change
|
|
|
|
with_overrides(function (override) {
|
|
|
|
global.with_stub(function (stub) {
|
|
|
|
override('subs.update_stream_name', stub.f);
|
|
|
|
stream_events.update_property(1, 'name', 'the frontend');
|
2019-11-02 00:06:25 +01:00
|
|
|
const args = stub.get_args('sub', 'val');
|
2017-07-03 19:57:24 +02:00
|
|
|
assert.equal(args.sub.stream_id, 1);
|
|
|
|
assert.equal(args.val, 'the frontend');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Test description change
|
|
|
|
with_overrides(function (override) {
|
|
|
|
global.with_stub(function (stub) {
|
|
|
|
override('subs.update_stream_description', stub.f);
|
2019-05-07 08:21:21 +02:00
|
|
|
stream_events.update_property(1, 'description', 'we write code', {rendered_description: 'we write code'});
|
2019-11-02 00:06:25 +01:00
|
|
|
const args = stub.get_args('sub', 'val');
|
2017-07-03 19:57:24 +02:00
|
|
|
assert.equal(args.sub.stream_id, 1);
|
|
|
|
assert.equal(args.val, 'we write code');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Test email address change
|
|
|
|
stream_events.update_property(1, 'email_address', 'zooly@zulip.com');
|
|
|
|
assert.equal(frontend.email_address, 'zooly@zulip.com');
|
|
|
|
|
|
|
|
// Test pin to top
|
|
|
|
with_overrides(function (override) {
|
|
|
|
override('stream_list.refresh_pinned_or_unpinned_stream', noop);
|
|
|
|
stream_events.update_property(1, 'pin_to_top', true);
|
2019-06-14 19:29:48 +02:00
|
|
|
checkbox = $("#pin_to_top_1");
|
2017-07-03 19:57:24 +02:00
|
|
|
assert.equal(checkbox.prop('checked'), true);
|
|
|
|
});
|
2017-07-03 21:55:03 +02:00
|
|
|
|
2019-05-02 19:43:27 +02:00
|
|
|
// Test stream privacy change event
|
|
|
|
with_overrides(function (override) {
|
|
|
|
global.with_stub(function (stub) {
|
|
|
|
override('subs.update_stream_privacy', stub.f);
|
|
|
|
stream_events.update_property(1, 'invite_only', true, {
|
|
|
|
history_public_to_subscribers: true,
|
|
|
|
});
|
2019-11-02 00:06:25 +01:00
|
|
|
const args = stub.get_args('sub', 'val');
|
2019-05-02 19:43:27 +02:00
|
|
|
assert.equal(args.sub.stream_id, 1);
|
|
|
|
assert.deepEqual(args.val, {
|
|
|
|
invite_only: true,
|
|
|
|
history_public_to_subscribers: true,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-02-04 21:50:55 +01:00
|
|
|
// Test stream stream_post_policy change event
|
2019-05-02 19:43:27 +02:00
|
|
|
with_overrides(function (override) {
|
|
|
|
global.with_stub(function (stub) {
|
2020-02-04 21:50:55 +01:00
|
|
|
override('subs.update_stream_post_policy', stub.f);
|
|
|
|
stream_events.update_property(1, 'stream_post_policy', stream_data.stream_post_policy_values.admins.code);
|
2019-11-02 00:06:25 +01:00
|
|
|
const args = stub.get_args('sub', 'val');
|
2019-05-02 19:43:27 +02:00
|
|
|
assert.equal(args.sub.stream_id, 1);
|
2020-02-04 21:50:55 +01:00
|
|
|
assert.equal(args.val, stream_data.stream_post_policy_values.admins.code);
|
2019-05-02 19:43:27 +02:00
|
|
|
});
|
|
|
|
});
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-07-03 21:55:03 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('marked_subscribed', () => {
|
2017-07-03 21:55:03 +02:00
|
|
|
// Test undefined error
|
|
|
|
with_overrides(function (override) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let errors = 0;
|
2018-11-10 01:57:10 +01:00
|
|
|
override('stream_color.update_stream_color', noop);
|
2017-07-03 21:55:03 +02:00
|
|
|
override('blueslip.error', function () {
|
|
|
|
errors += 1;
|
|
|
|
});
|
|
|
|
stream_events.mark_subscribed(undefined, [], 'yellow');
|
|
|
|
assert.equal(errors, 1);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Test early return if subscribed
|
|
|
|
with_overrides(function (override) {
|
2019-11-02 00:06:25 +01:00
|
|
|
let completed = false;
|
2017-07-03 21:55:03 +02:00
|
|
|
override('message_util.do_unread_count_updates', function () {
|
|
|
|
completed = true; // This gets run if we continue and don't early return
|
|
|
|
});
|
2019-11-02 00:06:25 +01:00
|
|
|
const subscribed = {subscribed: true};
|
2017-07-03 21:55:03 +02:00
|
|
|
stream_events.mark_subscribed(subscribed, [], 'yellow');
|
|
|
|
assert.equal(completed, false);
|
|
|
|
});
|
|
|
|
|
|
|
|
set_global('message_list', {
|
|
|
|
all: {
|
|
|
|
all_messages: function () { return ['msg']; },
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2018-08-04 19:44:31 +02:00
|
|
|
stream_data.subscribe_myself = noop;
|
|
|
|
stream_data.set_subscribers = noop;
|
|
|
|
stream_data.get_colors = noop;
|
|
|
|
stream_data.update_calculated_fields = noop;
|
|
|
|
|
2017-07-03 21:55:03 +02:00
|
|
|
set_global('subs', { update_settings_for_subscribed: noop });
|
|
|
|
set_global('narrow_state', { is_for_stream_id: noop });
|
2017-10-18 08:36:56 +02:00
|
|
|
set_global('overlays', { streams_open: return_true });
|
2017-07-03 21:55:03 +02:00
|
|
|
|
|
|
|
// Test unread count update
|
|
|
|
with_overrides(function (override) {
|
|
|
|
global.with_stub(function (stub) {
|
2018-11-10 01:57:10 +01:00
|
|
|
override('stream_color.update_stream_color', noop);
|
2017-07-03 21:55:03 +02:00
|
|
|
override('message_util.do_unread_count_updates', stub.f);
|
|
|
|
stream_events.mark_subscribed(frontend, [], '');
|
2019-11-02 00:06:25 +01:00
|
|
|
const args = stub.get_args('messages');
|
2017-07-03 21:55:03 +02:00
|
|
|
assert.deepEqual(args.messages, ['msg']);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
set_global('message_util', { do_unread_count_updates: noop });
|
|
|
|
|
|
|
|
// Test jQuery event
|
2018-11-10 01:57:10 +01:00
|
|
|
with_overrides(function (override) {
|
|
|
|
override('stream_color.update_stream_color', noop);
|
|
|
|
global.with_stub(function (stub) {
|
|
|
|
$(document).on('subscription_add_done.zulip', stub.f);
|
|
|
|
stream_events.mark_subscribed(frontend, [], '');
|
2019-11-02 00:06:25 +01:00
|
|
|
const args = stub.get_args('event');
|
2018-11-10 01:57:10 +01:00
|
|
|
assert.equal(args.event.sub.stream_id, 1);
|
|
|
|
});
|
2018-05-07 03:30:13 +02:00
|
|
|
});
|
2017-07-03 21:55:03 +02:00
|
|
|
|
|
|
|
// Test bookend update
|
|
|
|
with_overrides(function (override) {
|
2018-11-10 01:57:10 +01:00
|
|
|
override('stream_color.update_stream_color', noop);
|
2017-07-03 21:55:03 +02:00
|
|
|
override('narrow_state.is_for_stream_id', function () {
|
|
|
|
return true;
|
|
|
|
});
|
2019-11-02 00:06:25 +01:00
|
|
|
let updated = false;
|
2017-07-03 21:55:03 +02:00
|
|
|
override('current_msg_list.update_trailing_bookend', function () {
|
|
|
|
updated = true;
|
|
|
|
});
|
|
|
|
stream_events.mark_subscribed(frontend, [], '');
|
|
|
|
assert.equal(updated, true);
|
|
|
|
});
|
|
|
|
|
2017-11-09 16:26:38 +01:00
|
|
|
// reset overridden value
|
2017-07-03 21:55:03 +02:00
|
|
|
set_global('narrow_state', { is_for_stream_id: noop });
|
|
|
|
|
2018-04-13 20:51:25 +02:00
|
|
|
// Test setting color
|
2018-11-10 01:57:10 +01:00
|
|
|
with_overrides(function (override) {
|
|
|
|
override('stream_color.update_stream_color', noop);
|
|
|
|
stream_events.mark_subscribed(frontend, [], 'blue');
|
|
|
|
assert.equal(frontend.color, 'blue');
|
|
|
|
});
|
2017-07-03 21:55:03 +02:00
|
|
|
|
|
|
|
// Test assigning generated color
|
|
|
|
with_overrides(function (override) {
|
|
|
|
frontend.color = undefined;
|
2018-11-28 22:07:14 +01:00
|
|
|
override('color_data.pick_color', function () {
|
2017-07-03 21:55:03 +02:00
|
|
|
return 'green';
|
|
|
|
});
|
2019-11-02 00:06:25 +01:00
|
|
|
let warnings = 0;
|
2017-07-03 21:55:03 +02:00
|
|
|
override('blueslip.warn', function () {
|
|
|
|
warnings += 1;
|
|
|
|
});
|
|
|
|
|
|
|
|
global.with_stub(function (stub) {
|
2018-11-10 01:57:10 +01:00
|
|
|
override('stream_color.update_stream_color', noop);
|
2017-07-03 21:55:03 +02:00
|
|
|
override('subs.set_color', stub.f);
|
|
|
|
stream_events.mark_subscribed(frontend, [], undefined);
|
2019-11-02 00:06:25 +01:00
|
|
|
const args = stub.get_args('id', 'color');
|
2017-07-03 21:55:03 +02:00
|
|
|
assert.equal(args.id, 1);
|
|
|
|
assert.equal(args.color, 'green');
|
|
|
|
assert.equal(warnings, 1);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Test assigning subscriber emails
|
|
|
|
with_overrides(function (override) {
|
2018-11-10 01:57:10 +01:00
|
|
|
override('stream_color.update_stream_color', noop);
|
2017-07-03 21:55:03 +02:00
|
|
|
global.with_stub(function (stub) {
|
2017-10-07 16:00:39 +02:00
|
|
|
override('stream_data.set_subscribers', stub.f);
|
2019-11-02 00:06:25 +01:00
|
|
|
const user_ids = [15, 20, 25];
|
2017-10-07 16:00:39 +02:00
|
|
|
stream_events.mark_subscribed(frontend, user_ids, '');
|
2019-11-02 00:06:25 +01:00
|
|
|
const args = stub.get_args('sub', 'subscribers');
|
2017-07-03 21:55:03 +02:00
|
|
|
assert.deepEqual(frontend, args.sub);
|
2017-10-07 16:00:39 +02:00
|
|
|
assert.deepEqual(user_ids, args.subscribers);
|
2017-07-03 21:55:03 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// assign self as well
|
|
|
|
global.with_stub(function (stub) {
|
|
|
|
override('stream_data.subscribe_myself', stub.f);
|
|
|
|
stream_events.mark_subscribed(frontend, [], '');
|
2019-11-02 00:06:25 +01:00
|
|
|
const args = stub.get_args('sub');
|
2017-07-03 21:55:03 +02:00
|
|
|
assert.deepEqual(frontend, args.sub);
|
|
|
|
});
|
|
|
|
|
|
|
|
// and finally update subscriber settings
|
|
|
|
global.with_stub(function (stub) {
|
|
|
|
override('subs.update_settings_for_subscribed', stub.f);
|
|
|
|
stream_events.mark_subscribed(frontend, [], '');
|
2019-11-02 00:06:25 +01:00
|
|
|
const args = stub.get_args('sub');
|
2017-07-03 21:55:03 +02:00
|
|
|
assert.deepEqual(frontend, args.sub);
|
|
|
|
});
|
|
|
|
});
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-07-03 22:17:16 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('mark_unsubscribed', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
let removed_sub = false;
|
2017-07-03 22:17:16 +02:00
|
|
|
$(document).on('subscription_remove_done.zulip', function () {
|
|
|
|
removed_sub = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
// take no action if no sub specified
|
|
|
|
stream_events.mark_unsubscribed();
|
|
|
|
assert.equal(removed_sub, false);
|
|
|
|
|
|
|
|
// take no action if already unsubscribed
|
|
|
|
frontend.subscribed = false;
|
|
|
|
stream_events.mark_unsubscribed(frontend);
|
|
|
|
assert.equal(removed_sub, false);
|
|
|
|
|
|
|
|
// Test unsubscribe
|
|
|
|
frontend.subscribed = true;
|
|
|
|
with_overrides(function (override) {
|
|
|
|
global.with_stub(function (stub) {
|
|
|
|
override('stream_data.unsubscribe_myself', stub.f);
|
|
|
|
override('subs.update_settings_for_unsubscribed', noop);
|
|
|
|
stream_events.mark_unsubscribed(frontend);
|
2019-11-02 00:06:25 +01:00
|
|
|
const args = stub.get_args('sub');
|
2017-07-03 22:17:16 +02:00
|
|
|
assert.deepEqual(args.sub, frontend);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Test update settings after unsubscribe
|
|
|
|
with_overrides(function (override) {
|
|
|
|
global.with_stub(function (stub) {
|
|
|
|
override('subs.update_settings_for_unsubscribed', stub.f);
|
|
|
|
override('stream_data.unsubscribe_myself', noop);
|
|
|
|
stream_events.mark_unsubscribed(frontend);
|
2019-11-02 00:06:25 +01:00
|
|
|
const args = stub.get_args('sub');
|
2017-07-03 22:17:16 +02:00
|
|
|
assert.deepEqual(args.sub, frontend);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Test update bookend and remove done event
|
|
|
|
with_overrides(function (override) {
|
|
|
|
override('stream_data.unsubscribe_myself', noop);
|
|
|
|
override('subs.update_settings_for_unsubscribed', noop);
|
|
|
|
override('narrow_state.is_for_stream_id', function () {
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let updated = false;
|
2017-07-06 15:43:41 +02:00
|
|
|
override('current_msg_list.update_trailing_bookend', function () {
|
|
|
|
updated = true;
|
2017-07-03 22:17:16 +02:00
|
|
|
});
|
2017-07-06 15:43:41 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let event_triggered = false;
|
2017-07-06 15:43:41 +02:00
|
|
|
$(document).trigger = function (ev) {
|
|
|
|
assert.equal(ev.name, 'subscription_remove_done.zulip');
|
|
|
|
assert.deepEqual(ev.data.sub, frontend);
|
|
|
|
event_triggered = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
stream_events.mark_unsubscribed(frontend);
|
|
|
|
assert.equal(updated, true);
|
|
|
|
assert.equal(event_triggered, true);
|
2017-07-03 22:17:16 +02:00
|
|
|
});
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-08-04 19:44:31 +02:00
|
|
|
|
|
|
|
stream_data.clear_subscriptions();
|
2019-11-02 00:06:25 +01:00
|
|
|
const dev_help = {
|
2018-08-04 19:44:31 +02:00
|
|
|
subscribed: true,
|
|
|
|
color: 'blue',
|
|
|
|
name: 'dev help',
|
|
|
|
stream_id: 2,
|
2019-05-15 08:54:25 +02:00
|
|
|
is_muted: true,
|
2018-08-04 19:44:31 +02:00
|
|
|
invite_only: false,
|
|
|
|
};
|
|
|
|
stream_data.add_sub(dev_help.name, dev_help);
|
|
|
|
|
|
|
|
run_test('remove_deactivated_user_from_all_streams', () => {
|
|
|
|
subs.rerender_subscriptions_settings = () => {};
|
|
|
|
|
|
|
|
dev_help.can_access_subscribers = true;
|
|
|
|
|
|
|
|
// verify that deactivating user should unsubscribe user from all streams
|
|
|
|
assert(stream_data.add_subscriber(dev_help.name, george.user_id));
|
|
|
|
assert(dev_help.subscribers.has(george.user_id));
|
|
|
|
|
|
|
|
stream_events.remove_deactivated_user_from_all_streams(george.user_id);
|
|
|
|
|
|
|
|
assert(!dev_help.subscribers.has(george.user_id));
|
|
|
|
});
|