Skip to content

A platform for community discussion. Free, open, simple.

4.7 KB
Raw Blame
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();
  });
});