notifications: Extract should_send_*_notification for testing.

This commit is contained in:
Tim Abbott
2019-12-10 12:54:08 -08:00
parent 016487163f
commit 22cefeede8
2 changed files with 31 additions and 6 deletions

View File

@@ -11,12 +11,15 @@ set_global('document', {
set_global('page_params', { set_global('page_params', {
is_admin: false, is_admin: false,
realm_users: [], realm_users: [],
enable_desktop_notifications: true,
enable_sounds: true,
}); });
const _navigator = { const _navigator = {
userAgent: 'Mozilla/5.0 AppleWebKit/537.36 Chrome/64.0.3282.167 Safari/537.36', userAgent: 'Mozilla/5.0 AppleWebKit/537.36 Chrome/64.0.3282.167 Safari/537.36',
}; };
set_global('navigator', _navigator); set_global('navigator', _navigator);
zrequire('alert_words');
zrequire('muting'); zrequire('muting');
zrequire('stream_data'); zrequire('stream_data');
zrequire('people'); zrequire('people');
@@ -56,12 +59,16 @@ run_test('message_is_notifiable', () => {
content: 'message number 1', content: 'message number 1',
sent_by_me: true, sent_by_me: true,
notification_sent: false, notification_sent: false,
mentioned: true,
mentioned_me_directly: true, mentioned_me_directly: true,
type: 'stream', type: 'stream',
stream: 'general', stream: 'general',
stream_id: general.stream_id, stream_id: general.stream_id,
topic: 'whatever', topic: 'whatever',
}; };
assert.equal(notifications.should_send_desktop_notification(message), true);
assert.equal(notifications.should_send_audible_notification(message), true);
// Not notifiable because it was sent by the current user
assert.equal(notifications.message_is_notifiable(message), false); assert.equal(notifications.message_is_notifiable(message), false);
// Case 2: If the user has already been sent a notificaton about this message, // Case 2: If the user has already been sent a notificaton about this message,
@@ -74,12 +81,15 @@ run_test('message_is_notifiable', () => {
content: 'message number 2', content: 'message number 2',
sent_by_me: false, sent_by_me: false,
notification_sent: true, notification_sent: true,
mentioned: true,
mentioned_me_directly: true, mentioned_me_directly: true,
type: 'stream', type: 'stream',
stream: 'general', stream: 'general',
stream_id: general.stream_id, stream_id: general.stream_id,
topic: 'whatever', topic: 'whatever',
}; };
assert.equal(notifications.should_send_desktop_notification(message), true);
assert.equal(notifications.should_send_audible_notification(message), true);
assert.equal(notifications.message_is_notifiable(message), false); assert.equal(notifications.message_is_notifiable(message), false);
// Case 3: If a message mentions the user directly, // Case 3: If a message mentions the user directly,
@@ -90,12 +100,15 @@ run_test('message_is_notifiable', () => {
content: 'message number 3', content: 'message number 3',
sent_by_me: false, sent_by_me: false,
notification_sent: false, notification_sent: false,
mentioned: true,
mentioned_me_directly: true, mentioned_me_directly: true,
type: 'stream', type: 'stream',
stream: 'muted', stream: 'muted',
stream_id: muted.stream_id, stream_id: muted.stream_id,
topic: 'topic_three', topic: 'topic_three',
}; };
assert.equal(notifications.should_send_desktop_notification(message), true);
assert.equal(notifications.should_send_audible_notification(message), true);
assert.equal(notifications.message_is_notifiable(message), true); assert.equal(notifications.message_is_notifiable(message), true);
// Case 4: // Case 4:
@@ -105,12 +118,15 @@ run_test('message_is_notifiable', () => {
content: 'message number 4', content: 'message number 4',
sent_by_me: false, sent_by_me: false,
notification_sent: false, notification_sent: false,
mentioned: true,
mentioned_me_directly: true, mentioned_me_directly: true,
type: 'stream', type: 'stream',
stream: 'general', stream: 'general',
stream_id: general.stream_id, stream_id: general.stream_id,
topic: 'vanilla', topic: 'vanilla',
}; };
assert.equal(notifications.should_send_desktop_notification(message), true);
assert.equal(notifications.should_send_audible_notification(message), true);
assert.equal(notifications.message_is_notifiable(message), true); assert.equal(notifications.message_is_notifiable(message), true);
// Case 5: If a message is in a muted stream // Case 5: If a message is in a muted stream
@@ -121,12 +137,15 @@ run_test('message_is_notifiable', () => {
content: 'message number 5', content: 'message number 5',
sent_by_me: false, sent_by_me: false,
notification_sent: false, notification_sent: false,
mentioned: true,
mentioned_me_directly: false, mentioned_me_directly: false,
type: 'stream', type: 'stream',
stream: 'muted', stream: 'muted',
stream_id: muted.stream_id, stream_id: muted.stream_id,
topic: 'whatever', topic: 'whatever',
}; };
assert.equal(notifications.should_send_desktop_notification(message), true);
assert.equal(notifications.should_send_audible_notification(message), true);
assert.equal(notifications.message_is_notifiable(message), false); assert.equal(notifications.message_is_notifiable(message), false);
// Case 6: If a message is in a muted topic // Case 6: If a message is in a muted topic
@@ -137,12 +156,15 @@ run_test('message_is_notifiable', () => {
content: 'message number 6', content: 'message number 6',
sent_by_me: false, sent_by_me: false,
notification_sent: false, notification_sent: false,
mentioned: true,
mentioned_me_directly: false, mentioned_me_directly: false,
type: 'stream', type: 'stream',
stream: 'general', stream: 'general',
stream_id: general.stream_id, stream_id: general.stream_id,
topic: 'muted topic', topic: 'muted topic',
}; };
assert.equal(notifications.should_send_desktop_notification(message), true);
assert.equal(notifications.should_send_audible_notification(message), true);
assert.equal(notifications.message_is_notifiable(message), false); assert.equal(notifications.message_is_notifiable(message), false);
// Case 7 // Case 7
@@ -155,12 +177,15 @@ run_test('message_is_notifiable', () => {
content: 'message number 7', content: 'message number 7',
sent_by_me: false, sent_by_me: false,
notification_sent: false, notification_sent: false,
mentioned: true,
mentioned_me_directly: false, mentioned_me_directly: false,
type: 'stream', type: 'stream',
stream: 'general', stream: 'general',
stream_id: general.stream_id, stream_id: general.stream_id,
topic: 'whatever', topic: 'whatever',
}; };
assert.equal(notifications.should_send_desktop_notification(message), true);
assert.equal(notifications.should_send_audible_notification(message), true);
assert.equal(notifications.message_is_notifiable(message), true); assert.equal(notifications.message_is_notifiable(message), true);
}); });

View File

@@ -490,7 +490,7 @@ exports.message_is_notifiable = function (message) {
return true; return true;
}; };
function should_send_desktop_notification(message) { exports.should_send_desktop_notification = function (message) {
// For streams, send if desktop notifications are enabled for this // For streams, send if desktop notifications are enabled for this
// stream. // stream.
if (message.type === "stream" && if (message.type === "stream" &&
@@ -518,9 +518,9 @@ function should_send_desktop_notification(message) {
} }
return false; return false;
} };
function should_send_audible_notification(message) { exports.should_send_audible_notification = function (message) {
// For streams, ding if sounds are enabled for this stream. // For streams, ding if sounds are enabled for this stream.
if (message.type === "stream" && if (message.type === "stream" &&
stream_data.receives_notifications(message.stream, "audible_notifications")) { stream_data.receives_notifications(message.stream, "audible_notifications")) {
@@ -542,7 +542,7 @@ function should_send_audible_notification(message) {
} }
return false; return false;
} };
exports.granted_desktop_notifications_permission = function () { exports.granted_desktop_notifications_permission = function () {
return notifications_api && return notifications_api &&
@@ -569,13 +569,13 @@ exports.received_messages = function (messages) {
message.notification_sent = true; message.notification_sent = true;
if (should_send_desktop_notification(message)) { if (exports.should_send_desktop_notification(message)) {
process_notification({ process_notification({
message: message, message: message,
desktop_notify: exports.granted_desktop_notifications_permission(), desktop_notify: exports.granted_desktop_notifications_permission(),
}); });
} }
if (should_send_audible_notification(message) && supports_sound) { if (exports.should_send_audible_notification(message) && supports_sound) {
$("#notifications-area").find("audio")[0].play(); $("#notifications-area").find("audio")[0].play();
} }
}); });