2024-07-25 21:09:06 +08:00
|
|
|
import { getOwner } from "@ember/owner";
|
2024-01-25 22:30:21 +08:00
|
|
|
import { render } from "@ember/test-helpers";
|
|
|
|
import { module, test } from "qunit";
|
2024-04-09 03:00:09 +08:00
|
|
|
import CoreFabricators from "discourse/lib/fabricators";
|
2024-01-25 22:30:21 +08:00
|
|
|
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
|
2024-11-10 08:30:33 +08:00
|
|
|
import { query } from "discourse/tests/helpers/qunit-helpers";
|
2024-01-25 22:30:21 +08:00
|
|
|
import ChannelName from "discourse/plugins/chat/discourse/components/channel-name";
|
2024-04-09 03:00:09 +08:00
|
|
|
import ChatFabricators from "discourse/plugins/chat/discourse/lib/fabricators";
|
2024-01-25 22:30:21 +08:00
|
|
|
import { CHATABLE_TYPES } from "discourse/plugins/chat/discourse/models/chat-channel";
|
|
|
|
|
|
|
|
const CHANNEL_NAME_LABEL = ".chat-channel-name__label";
|
|
|
|
|
|
|
|
module("Discourse Chat | Component | <ChannelName />", function (hooks) {
|
|
|
|
setupRenderingTest(hooks);
|
|
|
|
|
|
|
|
test("category channel - label", async function (assert) {
|
2024-04-09 03:00:09 +08:00
|
|
|
const channel = new ChatFabricators(getOwner(this)).channel();
|
2024-01-25 22:30:21 +08:00
|
|
|
|
|
|
|
await render(<template><ChannelName @channel={{channel}} /></template>);
|
|
|
|
|
2024-10-15 23:11:20 +08:00
|
|
|
assert.dom(CHANNEL_NAME_LABEL).hasText(channel.title);
|
2024-01-25 22:30:21 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test("category channel - escapes label", async function (assert) {
|
2024-04-09 03:00:09 +08:00
|
|
|
const channel = new ChatFabricators(getOwner(this)).channel({
|
2024-01-25 22:30:21 +08:00
|
|
|
chatable_type: CHATABLE_TYPES.categoryChannel,
|
|
|
|
title: "<div class='xss'>evil</div>",
|
|
|
|
});
|
|
|
|
|
|
|
|
await render(<template><ChannelName @channel={{channel}} /></template>);
|
|
|
|
|
2024-11-10 08:30:33 +08:00
|
|
|
assert.dom(".xss").doesNotExist();
|
2024-01-25 22:30:21 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test("dm channel - one user", async function (assert) {
|
2024-04-09 03:00:09 +08:00
|
|
|
const channel = new ChatFabricators(getOwner(this)).directMessageChannel({
|
|
|
|
chatable: new ChatFabricators(getOwner(this)).directMessage({
|
|
|
|
users: [new CoreFabricators(getOwner(this)).user()],
|
2024-01-25 22:30:21 +08:00
|
|
|
}),
|
|
|
|
});
|
|
|
|
const user = channel.chatable.users[0];
|
|
|
|
|
|
|
|
await render(<template><ChannelName @channel={{channel}} /></template>);
|
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
query(CHANNEL_NAME_LABEL).innerText.trim(),
|
|
|
|
user.username
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("dm channel - multiple users", async function (assert) {
|
2024-04-09 03:00:09 +08:00
|
|
|
const channel = new ChatFabricators(getOwner(this)).directMessageChannel({
|
|
|
|
users: [
|
|
|
|
new CoreFabricators(getOwner(this)).user(),
|
|
|
|
new CoreFabricators(getOwner(this)).user(),
|
|
|
|
new CoreFabricators(getOwner(this)).user(),
|
|
|
|
],
|
2024-01-25 22:30:21 +08:00
|
|
|
});
|
|
|
|
channel.chatable.group = true;
|
|
|
|
const users = channel.chatable.users;
|
|
|
|
|
|
|
|
await render(<template><ChannelName @channel={{channel}} /></template>);
|
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
query(CHANNEL_NAME_LABEL).innerText.trim(),
|
|
|
|
users.mapBy("username").join(", ")
|
|
|
|
);
|
|
|
|
});
|
2024-09-05 17:36:50 +08:00
|
|
|
|
2024-09-16 22:12:13 +08:00
|
|
|
test("dm channel - self", async function (assert) {
|
|
|
|
const channel = new ChatFabricators(getOwner(this)).directMessageChannel({
|
|
|
|
chatable: new ChatFabricators(getOwner(this)).directMessage({
|
|
|
|
users: [],
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
|
|
|
await render(<template><ChannelName @channel={{channel}} /></template>);
|
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
query(CHANNEL_NAME_LABEL).innerText.trim(),
|
|
|
|
this.currentUser.username
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("dm channel - prefers name", async function (assert) {
|
|
|
|
const siteSettings = getOwner(this).lookup("service:site-settings");
|
|
|
|
siteSettings.enable_names = true;
|
|
|
|
siteSettings.display_name_on_posts = true;
|
|
|
|
siteSettings.prioritize_username_in_ux = false;
|
|
|
|
|
|
|
|
const channel = new ChatFabricators(getOwner(this)).directMessageChannel({
|
|
|
|
users: [
|
|
|
|
new CoreFabricators(getOwner(this)).user({ name: "Alice" }),
|
|
|
|
new CoreFabricators(getOwner(this)).user({ name: "Bob" }),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
channel.chatable.group = true;
|
|
|
|
const users = channel.chatable.users;
|
|
|
|
|
|
|
|
await render(<template><ChannelName @channel={{channel}} /></template>);
|
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
query(CHANNEL_NAME_LABEL).innerText.trim(),
|
|
|
|
users.mapBy("name").join(", ")
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2024-09-05 17:36:50 +08:00
|
|
|
test("unreadIndicator", async function (assert) {
|
|
|
|
const channel = new ChatFabricators(getOwner(this)).directMessageChannel();
|
|
|
|
channel.tracking.unreadCount = 1;
|
|
|
|
|
|
|
|
let unreadIndicator = true;
|
2024-09-16 22:12:13 +08:00
|
|
|
await render(<template>
|
|
|
|
<ChannelName @channel={{channel}} @unreadIndicator={{unreadIndicator}} />
|
|
|
|
</template>);
|
2024-09-05 17:36:50 +08:00
|
|
|
|
2024-11-09 03:27:32 +08:00
|
|
|
assert.dom(".chat-channel-unread-indicator").exists();
|
2024-09-05 17:36:50 +08:00
|
|
|
|
|
|
|
unreadIndicator = false;
|
2024-09-16 22:12:13 +08:00
|
|
|
await render(<template>
|
|
|
|
<ChannelName @channel={{channel}} @unreadIndicator={{unreadIndicator}} />
|
|
|
|
</template>);
|
2024-09-05 17:36:50 +08:00
|
|
|
|
2024-11-10 08:30:33 +08:00
|
|
|
assert.dom(".chat-channel-unread-indicator").doesNotExist();
|
2024-09-05 17:36:50 +08:00
|
|
|
});
|
2024-01-25 22:30:21 +08:00
|
|
|
});
|