2022-12-22 08:35:18 -05:00
|
|
|
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
|
|
|
|
import { click, fillIn, render } from "@ember/test-helpers";
|
2022-11-02 09:41:30 -04:00
|
|
|
import hbs from "htmlbars-inline-precompile";
|
|
|
|
import { exists, query } from "discourse/tests/helpers/qunit-helpers";
|
2023-05-08 12:24:41 -04:00
|
|
|
import ChatChannel from "discourse/plugins/chat/discourse/models/chat-channel";
|
2022-11-02 09:41:30 -04:00
|
|
|
import { Promise } from "rsvp";
|
2023-05-17 11:49:52 -04:00
|
|
|
import fabricators from "discourse/plugins/chat/discourse/lib/fabricators";
|
2022-12-22 08:35:18 -05:00
|
|
|
import { module, test } from "qunit";
|
2022-11-02 09:41:30 -04:00
|
|
|
|
|
|
|
function mockChat(context, options = {}) {
|
|
|
|
const mock = context.container.lookup("service:chat");
|
|
|
|
mock.searchPossibleDirectMessageUsers = () => {
|
|
|
|
return Promise.resolve({
|
|
|
|
users: options.users || [{ username: "hawk" }, { username: "mark" }],
|
|
|
|
});
|
|
|
|
};
|
|
|
|
mock.getDmChannelForUsernames = () => {
|
2023-05-17 11:49:52 -04:00
|
|
|
return Promise.resolve({ chat_channel: fabricators.channel() });
|
2022-11-02 09:41:30 -04:00
|
|
|
};
|
|
|
|
return mock;
|
|
|
|
}
|
|
|
|
|
|
|
|
module("Discourse Chat | Component | direct-message-creator", function (hooks) {
|
|
|
|
setupRenderingTest(hooks);
|
|
|
|
|
2022-12-22 08:35:18 -05:00
|
|
|
test("search", async function (assert) {
|
|
|
|
this.set("chat", mockChat(this));
|
2023-05-08 12:24:41 -04:00
|
|
|
this.set("channel", ChatChannel.createDirectMessageChannelDraft());
|
2022-11-02 09:41:30 -04:00
|
|
|
|
2022-12-22 08:35:18 -05:00
|
|
|
await render(
|
|
|
|
hbs`<DirectMessageCreator @channel={{this.channel}} @chat={{this.chat}} />`
|
|
|
|
);
|
2022-11-02 09:41:30 -04:00
|
|
|
|
2022-12-22 08:35:18 -05:00
|
|
|
await fillIn(".filter-usernames", "hawk");
|
|
|
|
assert.true(exists("li.user[data-username='hawk']"));
|
2022-11-02 09:41:30 -04:00
|
|
|
});
|
|
|
|
|
2022-12-22 08:35:18 -05:00
|
|
|
test("select/deselect", async function (assert) {
|
|
|
|
this.set("chat", mockChat(this));
|
2023-05-08 12:24:41 -04:00
|
|
|
this.set("channel", ChatChannel.createDirectMessageChannelDraft());
|
2022-11-02 09:41:30 -04:00
|
|
|
|
2022-12-22 08:35:18 -05:00
|
|
|
await render(
|
|
|
|
hbs`<DirectMessageCreator @channel={{this.channel}} @chat={{this.chat}} />`
|
|
|
|
);
|
|
|
|
assert.false(exists(".selected-user"));
|
2022-11-02 09:41:30 -04:00
|
|
|
|
2022-12-22 08:35:18 -05:00
|
|
|
await fillIn(".filter-usernames", "hawk");
|
|
|
|
await click("li.user[data-username='hawk']");
|
|
|
|
assert.true(exists(".selected-user"));
|
2022-11-02 09:41:30 -04:00
|
|
|
|
2022-12-22 08:35:18 -05:00
|
|
|
await click(".selected-user");
|
|
|
|
assert.false(exists(".selected-user"));
|
2022-11-02 09:41:30 -04:00
|
|
|
});
|
|
|
|
|
2022-12-22 08:35:18 -05:00
|
|
|
test("no search results", async function (assert) {
|
|
|
|
this.set("chat", mockChat(this, { users: [] }));
|
2023-05-08 12:24:41 -04:00
|
|
|
this.set("channel", ChatChannel.createDirectMessageChannelDraft());
|
2022-11-02 09:41:30 -04:00
|
|
|
|
2022-12-22 08:35:18 -05:00
|
|
|
await render(
|
|
|
|
hbs`<DirectMessageCreator @channel={{this.channel}} @chat={{this.chat}} />`
|
|
|
|
);
|
2022-11-02 09:41:30 -04:00
|
|
|
|
2022-12-22 08:35:18 -05:00
|
|
|
await fillIn(".filter-usernames", "bad cat");
|
|
|
|
assert.true(exists(".no-results"));
|
2022-11-02 09:41:30 -04:00
|
|
|
});
|
|
|
|
|
2022-12-22 08:35:18 -05:00
|
|
|
test("loads user on first load", async function (assert) {
|
|
|
|
this.set("chat", mockChat(this));
|
2023-05-08 12:24:41 -04:00
|
|
|
this.set("channel", ChatChannel.createDirectMessageChannelDraft());
|
2022-11-02 09:41:30 -04:00
|
|
|
|
2022-12-22 08:35:18 -05:00
|
|
|
await render(
|
|
|
|
hbs`<DirectMessageCreator @channel={{this.channel}} @chat={{this.chat}} />`
|
|
|
|
);
|
2022-11-02 09:41:30 -04:00
|
|
|
|
2022-12-22 08:35:18 -05:00
|
|
|
assert.true(exists("li.user[data-username='hawk']"));
|
|
|
|
assert.true(exists("li.user[data-username='mark']"));
|
2022-11-02 09:41:30 -04:00
|
|
|
});
|
|
|
|
|
2022-12-22 08:35:18 -05:00
|
|
|
test("do not load more users after selection", async function (assert) {
|
|
|
|
this.set("chat", mockChat(this));
|
2023-05-08 12:24:41 -04:00
|
|
|
this.set("channel", ChatChannel.createDirectMessageChannelDraft());
|
2022-11-02 09:41:30 -04:00
|
|
|
|
2022-12-22 08:35:18 -05:00
|
|
|
await render(
|
|
|
|
hbs`<DirectMessageCreator @channel={{this.channel}} @chat={{this.chat}} />`
|
|
|
|
);
|
2022-11-02 09:41:30 -04:00
|
|
|
|
2022-12-22 08:35:18 -05:00
|
|
|
await click("li.user[data-username='hawk']");
|
|
|
|
assert.false(exists("li.user[data-username='mark']"));
|
2022-11-02 09:41:30 -04:00
|
|
|
});
|
|
|
|
|
2022-12-22 08:35:18 -05:00
|
|
|
test("apply is-focused to filter-area on focus input", async function (assert) {
|
|
|
|
this.set("chat", mockChat(this));
|
2023-05-08 12:24:41 -04:00
|
|
|
this.set("channel", ChatChannel.createDirectMessageChannelDraft());
|
2022-11-02 09:41:30 -04:00
|
|
|
|
2022-12-22 08:35:18 -05:00
|
|
|
await render(
|
|
|
|
hbs`<DirectMessageCreator @channel={{this.channel}} @chat={{this.chat}} /><button class="test-blur">blur</button>`
|
|
|
|
);
|
2022-11-02 09:41:30 -04:00
|
|
|
|
2022-12-22 08:35:18 -05:00
|
|
|
await click(".filter-usernames");
|
|
|
|
assert.true(exists(".filter-area.is-focused"));
|
2022-11-02 09:41:30 -04:00
|
|
|
|
2022-12-22 08:35:18 -05:00
|
|
|
await click(".test-blur");
|
|
|
|
assert.false(exists(".filter-area.is-focused"));
|
2022-11-02 09:41:30 -04:00
|
|
|
});
|
|
|
|
|
2022-12-22 08:35:18 -05:00
|
|
|
test("state is reset on channel change", async function (assert) {
|
|
|
|
this.set("chat", mockChat(this));
|
2023-05-08 12:24:41 -04:00
|
|
|
this.set("channel", ChatChannel.createDirectMessageChannelDraft());
|
2022-11-02 09:41:30 -04:00
|
|
|
|
2022-12-22 08:35:18 -05:00
|
|
|
await render(
|
|
|
|
hbs`<DirectMessageCreator @channel={{this.channel}} @chat={{this.chat}} />`
|
|
|
|
);
|
2022-11-02 09:41:30 -04:00
|
|
|
|
2022-12-22 08:35:18 -05:00
|
|
|
await fillIn(".filter-usernames", "hawk");
|
|
|
|
assert.strictEqual(query(".filter-usernames").value, "hawk");
|
2022-11-02 09:41:30 -04:00
|
|
|
|
2023-05-17 11:49:52 -04:00
|
|
|
this.set("channel", fabricators.channel());
|
2023-05-08 12:24:41 -04:00
|
|
|
this.set("channel", ChatChannel.createDirectMessageChannelDraft());
|
2022-11-02 09:41:30 -04:00
|
|
|
|
2022-12-22 08:35:18 -05:00
|
|
|
assert.strictEqual(query(".filter-usernames").value, "");
|
|
|
|
assert.true(exists(".filter-area.is-focused"));
|
|
|
|
assert.true(exists("li.user[data-username='hawk']"));
|
2022-11-02 09:41:30 -04:00
|
|
|
});
|
|
|
|
|
2022-12-22 08:35:18 -05:00
|
|
|
test("shows user status", async function (assert) {
|
|
|
|
const userWithStatus = {
|
|
|
|
username: "hawk",
|
|
|
|
status: { emoji: "tooth", description: "off to dentist" },
|
|
|
|
};
|
|
|
|
const chat = mockChat(this, { users: [userWithStatus] });
|
|
|
|
this.set("chat", chat);
|
2023-05-08 12:24:41 -04:00
|
|
|
this.set("channel", ChatChannel.createDirectMessageChannelDraft());
|
2022-12-22 08:35:18 -05:00
|
|
|
|
|
|
|
await render(
|
|
|
|
hbs`<DirectMessageCreator @channel={{this.channel}} @chat={{this.chat}} />`
|
|
|
|
);
|
|
|
|
|
|
|
|
await fillIn(".filter-usernames", "hawk");
|
|
|
|
assert.true(exists(".user-status-message"));
|
2022-11-02 09:41:30 -04:00
|
|
|
});
|
|
|
|
});
|