discourse/plugins/chat/test/javascripts/unit/lib/chat-audio-test.js
David Battersby 2a5e1aa8e6
FEATURE: only play chat sound when chat badge icon is shown (#28387)
Every time a desktop chat sound plays, there should be some visual cue as to why the sound was played in the first place.

This change follows the chat indicator preference:

- All New Messages - a blue dot is shown for all messages, so we attempt to play a sound every time
- Direct Messages, Mentions and Watched Threads - a green dot is shown for all urgent messages, so we attempt to play a sound for urgent chat notifications
- Only Mentions - only play chat sounds when user is mentioned
- Never - we never play chat sounds, as user wouldn’t know why the sound was played
2024-09-06 18:25:25 +04:00

102 lines
3.1 KiB
JavaScript

import { getOwner } from "@ember/owner";
import { module, test } from "qunit";
import sinon from "sinon";
import { withPluginApi } from "discourse/lib/plugin-api";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import chatAudioInitializer from "discourse/plugins/chat/discourse/initializers/chat-audio";
module("Discourse Chat | Unit | chat-audio", function (hooks) {
setupRenderingTest(hooks);
hooks.beforeEach(function () {
const chatAudioManager = getOwner(this).lookup(
"service:chat-audio-manager"
);
this.chat = getOwner(this).lookup("service:chat");
sinon.stub(this.chat, "userCanChat").value(true);
this.siteSettings = getOwner(this).lookup("service:site-settings");
this.siteSettings.chat_enabled = true;
this.currentUser.user_option.has_chat_enabled = true;
this.currentUser.user_option.chat_sound = "ding";
this.currentUser.user_option.chat_header_indicator_preference = "all_new";
withPluginApi("0.12.1", async (api) => {
this.stub = sinon.spy(api, "registerDesktopNotificationHandler");
chatAudioInitializer.initialize(getOwner(this));
this.notificationHandler = this.stub.getCall(0).callback;
this.playStub = sinon.stub(chatAudioManager, "play");
this.handleNotification = (data = {}) => {
if (!data.notification_type) {
data.notification_type = 30;
}
this.notificationHandler(data, this.siteSettings, this.currentUser);
};
});
});
test("it registers desktop notification handler", function (assert) {
assert.ok(this.stub.calledOnce);
});
test("it plays chat sound", function (assert) {
this.handleNotification();
assert.ok(this.playStub.calledOnce);
});
test("it skips chat sound for user in DND mode", function (assert) {
this.currentUser.isInDoNotDisturb = () => true;
this.handleNotification();
assert.ok(this.playStub.notCalled);
});
test("it skips chat sound for user with no chat sound set", function (assert) {
this.currentUser.user_option.chat_sound = null;
this.handleNotification();
assert.ok(this.playStub.notCalled);
});
test("it plays a chat sound for mentions", function (assert) {
this.currentUser.user_option.chat_header_indicator_preference =
"only_mentions";
this.handleNotification({ notification_type: 29 });
assert.ok(this.playStub.calledOnce);
});
test("it skips chat sound for non-mentions", function (assert) {
this.currentUser.user_option.chat_header_indicator_preference =
"only_mentions";
this.handleNotification();
assert.ok(this.playStub.notCalled);
});
test("it plays a chat sound for DMs", function (assert) {
this.currentUser.user_option.chat_header_indicator_preference =
"dm_and_mentions";
this.handleNotification({ isDirectMessageChannel: true });
assert.ok(this.playStub.calledOnce);
});
test("it skips chat sound for non-DM messages", function (assert) {
this.currentUser.user_option.chat_header_indicator_preference =
"dm_and_mentions";
this.handleNotification({ isDirectMessageChannel: false });
assert.ok(this.playStub.notCalled);
});
});