2023-10-11 02:38:59 +08:00
|
|
|
import { render } from "@ember/test-helpers";
|
|
|
|
import hbs from "htmlbars-inline-precompile";
|
|
|
|
import { module, test } from "qunit";
|
2022-12-22 21:35:18 +08:00
|
|
|
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
|
2022-11-02 21:41:30 +08:00
|
|
|
import { exists, query } from "discourse/tests/helpers/qunit-helpers";
|
2023-05-17 23:49:52 +08:00
|
|
|
import fabricators from "discourse/plugins/chat/discourse/lib/fabricators";
|
2022-11-02 21:41:30 +08:00
|
|
|
import { CHATABLE_TYPES } from "discourse/plugins/chat/discourse/models/chat-channel";
|
|
|
|
|
|
|
|
module("Discourse Chat | Component | chat-channel-title", function (hooks) {
|
|
|
|
setupRenderingTest(hooks);
|
|
|
|
|
2022-12-22 21:35:18 +08:00
|
|
|
test("category channel", async function (assert) {
|
2023-11-10 18:29:28 +08:00
|
|
|
this.channel = fabricators.channel();
|
2022-12-22 21:35:18 +08:00
|
|
|
|
|
|
|
await render(hbs`<ChatChannelTitle @channel={{this.channel}} />`);
|
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
query(".chat-channel-title__category-badge").getAttribute("style"),
|
|
|
|
`color: #${this.channel.chatable.color}`
|
|
|
|
);
|
|
|
|
assert.strictEqual(
|
|
|
|
query(".chat-channel-title__name").innerText,
|
|
|
|
this.channel.title
|
|
|
|
);
|
2022-11-02 21:41:30 +08:00
|
|
|
});
|
|
|
|
|
2022-12-22 21:35:18 +08:00
|
|
|
test("category channel - escapes title", async function (assert) {
|
2023-05-17 23:49:52 +08:00
|
|
|
this.channel = fabricators.channel({
|
2023-05-09 00:24:41 +08:00
|
|
|
chatable_type: CHATABLE_TYPES.categoryChannel,
|
|
|
|
title: "<div class='xss'>evil</div>",
|
|
|
|
});
|
2022-12-22 21:35:18 +08:00
|
|
|
|
|
|
|
await render(hbs`<ChatChannelTitle @channel={{this.channel}} />`);
|
|
|
|
|
|
|
|
assert.false(exists(".xss"));
|
2022-11-02 21:41:30 +08:00
|
|
|
});
|
|
|
|
|
2022-12-22 21:35:18 +08:00
|
|
|
test("category channel - read restricted", async function (assert) {
|
2023-05-17 23:49:52 +08:00
|
|
|
this.channel = fabricators.channel({
|
2023-11-10 18:29:28 +08:00
|
|
|
chatable: fabricators.category({ read_restricted: true }),
|
2023-05-09 00:24:41 +08:00
|
|
|
});
|
2022-12-22 21:35:18 +08:00
|
|
|
|
|
|
|
await render(hbs`<ChatChannelTitle @channel={{this.channel}} />`);
|
|
|
|
|
|
|
|
assert.true(exists(".d-icon-lock"));
|
2022-11-02 21:41:30 +08:00
|
|
|
});
|
|
|
|
|
2022-12-22 21:35:18 +08:00
|
|
|
test("category channel - not read restricted", async function (assert) {
|
2023-05-17 23:49:52 +08:00
|
|
|
this.channel = fabricators.channel({
|
2023-11-10 18:29:28 +08:00
|
|
|
chatable: fabricators.category({ read_restricted: false }),
|
2023-05-09 00:24:41 +08:00
|
|
|
});
|
2022-12-22 21:35:18 +08:00
|
|
|
|
|
|
|
await render(hbs`<ChatChannelTitle @channel={{this.channel}} />`);
|
|
|
|
|
|
|
|
assert.false(exists(".d-icon-lock"));
|
2022-11-02 21:41:30 +08:00
|
|
|
});
|
|
|
|
|
2022-12-22 21:35:18 +08:00
|
|
|
test("direct message channel - one user", async function (assert) {
|
2023-05-17 23:49:52 +08:00
|
|
|
this.channel = fabricators.directMessageChannel({
|
|
|
|
chatable: fabricators.directMessage({
|
|
|
|
users: [fabricators.user()],
|
|
|
|
}),
|
|
|
|
});
|
2022-11-02 21:41:30 +08:00
|
|
|
|
2022-12-22 21:35:18 +08:00
|
|
|
await render(hbs`<ChatChannelTitle @channel={{this.channel}} />`);
|
2022-11-02 21:41:30 +08:00
|
|
|
|
2022-12-22 21:35:18 +08:00
|
|
|
const user = this.channel.chatable.users[0];
|
2022-11-02 21:41:30 +08:00
|
|
|
|
2023-11-10 18:29:28 +08:00
|
|
|
assert.true(exists(`.chat-user-avatar .avatar[title="${user.username}"]`));
|
2022-12-22 21:35:18 +08:00
|
|
|
assert.strictEqual(
|
|
|
|
query(".chat-channel-title__name").innerText.trim(),
|
|
|
|
user.username
|
|
|
|
);
|
2022-11-02 21:41:30 +08:00
|
|
|
});
|
|
|
|
|
2022-12-22 21:35:18 +08:00
|
|
|
test("direct message channel - multiple users", async function (assert) {
|
2023-11-10 18:29:28 +08:00
|
|
|
this.channel = fabricators.directMessageChannel({
|
|
|
|
users: [fabricators.user(), fabricators.user(), fabricators.user()],
|
2022-12-22 21:35:18 +08:00
|
|
|
});
|
2023-11-10 18:29:28 +08:00
|
|
|
this.channel.chatable.group = true;
|
2022-12-22 21:35:18 +08:00
|
|
|
|
|
|
|
await render(hbs`<ChatChannelTitle @channel={{this.channel}} />`);
|
|
|
|
|
|
|
|
const users = this.channel.chatable.users;
|
2023-11-10 18:29:28 +08:00
|
|
|
|
2022-12-22 21:35:18 +08:00
|
|
|
assert.strictEqual(
|
|
|
|
parseInt(query(".chat-channel-title__users-count").innerText.trim(), 10),
|
|
|
|
users.length
|
|
|
|
);
|
|
|
|
assert.strictEqual(
|
|
|
|
query(".chat-channel-title__name").innerText.trim(),
|
|
|
|
users.mapBy("username").join(", ")
|
|
|
|
);
|
2022-11-02 21:41:30 +08:00
|
|
|
});
|
|
|
|
});
|