0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-14 13:58:53 +08:00
discourse/plugins/chat/test/javascripts/components/channel-icon-test.gjs
Martin Brennan c78e16875b
UX: Show avatar when only 1 other user in chat group DM (#40269)
For chat group DMs, currently we always show the number of
other users in the group DM, even if there is only 1 other user.
This looks a little weird, and for our own use case of an AI
helper bot on new sites, it's a little unfreindly to see a
(1) instead of the avatar of the bot.

This commit modifies the ChannelIcon logic to show the avatar
of the other user in a group DM if there is only 1 other user.
If there is > 1 other user, we still show the count/emoji as
appropriate.

**Before**

<img width="241" height="44" alt="image"
src="https://github.com/user-attachments/assets/727f1c85-901f-4d43-9a00-f7e16b7b4737"
/>

**After**

<img width="252" height="43" alt="image"
src="https://github.com/user-attachments/assets/999e5dbd-d2ee-4345-8ca1-c3cd50f5e606"
/>
2026-05-26 09:37:47 +10:00

139 lines
4.7 KiB
Text
Vendored

import { getOwner } from "@ember/owner";
import { render } from "@ember/test-helpers";
import { module, test } from "qunit";
import CoreFabricators from "discourse/lib/fabricators";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import ChannelIcon from "discourse/plugins/chat/discourse/components/channel-icon";
import ChatFabricators from "discourse/plugins/chat/discourse/lib/fabricators";
import { CHATABLE_TYPES } from "discourse/plugins/chat/discourse/models/chat-channel";
module("Component | <ChannelIcon />", function (hooks) {
setupRenderingTest(hooks);
test("category channel - badge", async function (assert) {
const channel = new ChatFabricators(getOwner(this)).channel();
await render(<template><ChannelIcon @channel={{channel}} /></template>);
assert
.dom(".chat-channel-icon.--icon")
.hasAttribute("style", `color: #${channel.chatable.color}`);
});
test("category channel - escapes label", async function (assert) {
const channel = new ChatFabricators(getOwner(this)).channel({
chatable_type: CHATABLE_TYPES.categoryChannel,
title: "<div class='xss'>evil</div>",
});
await render(<template><ChannelIcon @channel={{channel}} /></template>);
assert.dom(".xss").doesNotExist();
});
test("category channel - read restricted", async function (assert) {
const channel = new ChatFabricators(getOwner(this)).channel({
chatable: new CoreFabricators(getOwner(this)).category({
read_restricted: true,
}),
});
await render(<template><ChannelIcon @channel={{channel}} /></template>);
assert.dom(".d-icon-lock").exists();
});
test("category channel - not read restricted", async function (assert) {
const channel = new ChatFabricators(getOwner(this)).channel({
chatable: new CoreFabricators(getOwner(this)).category({
read_restricted: false,
}),
});
await render(<template><ChannelIcon @channel={{channel}} /></template>);
assert.dom(".d-icon-lock").doesNotExist();
});
test("dm channel - one user", async function (assert) {
const channel = new ChatFabricators(getOwner(this)).directMessageChannel({
chatable: new ChatFabricators(getOwner(this)).directMessage({
users: [new CoreFabricators(getOwner(this)).user()],
}),
});
const user = channel.chatable.users[0];
await render(<template><ChannelIcon @channel={{channel}} /></template>);
assert.dom(`.chat-user-avatar .avatar[title="${user.username}"]`).exists();
});
test("group DM channel - 2 users", async function (assert) {
const channel = new ChatFabricators(getOwner(this)).directMessageChannel({
chatable: new ChatFabricators(getOwner(this)).directMessage({
users: [
new CoreFabricators(getOwner(this)).user(),
new CoreFabricators(getOwner(this)).user(),
],
}),
group: true,
});
const user = channel.chatable.users[0];
await render(<template><ChannelIcon @channel={{channel}} /></template>);
assert.dom(".chat-channel-icon.--avatar").exists();
assert
.dom(
`.chat-channel-icon.--avatar .chat-user-avatar__container .avatar[title="${user.username}"]`
)
.exists();
});
test("group DM channel - 2 users with emoji", async function (assert) {
const channel = new ChatFabricators(getOwner(this)).directMessageChannel({
users: [
new CoreFabricators(getOwner(this)).user(),
new CoreFabricators(getOwner(this)).user(),
],
group: true,
emoji: "tada",
});
await render(<template><ChannelIcon @channel={{channel}} /></template>);
assert.dom(".chat-channel-icon.--emoji .emoji[title='tada']").exists();
});
test("group DM channel - 3 users", async function (assert) {
const channel = new ChatFabricators(getOwner(this)).directMessageChannel({
users: [
new CoreFabricators(getOwner(this)).user(),
new CoreFabricators(getOwner(this)).user(),
new CoreFabricators(getOwner(this)).user(),
],
group: true,
});
await render(<template><ChannelIcon @channel={{channel}} /></template>);
assert.dom(".chat-channel-icon.--users-count").exists().hasText("3");
});
test("group DM channel - with emoji", async function (assert) {
const channel = new ChatFabricators(getOwner(this)).directMessageChannel({
users: [
new CoreFabricators(getOwner(this)).user(),
new CoreFabricators(getOwner(this)).user(),
new CoreFabricators(getOwner(this)).user(),
],
group: true,
emoji: "tada",
});
await render(<template><ChannelIcon @channel={{channel}} /></template>);
assert.dom(".chat-channel-icon.--emoji .emoji[title='tada']").exists();
});
});