2023-07-03 22:09:41 +08:00
|
|
|
import { render, triggerEvent, waitFor } from "@ember/test-helpers";
|
2023-10-11 02:38:59 +08:00
|
|
|
import hbs from "htmlbars-inline-precompile";
|
2023-05-24 20:55:20 +08:00
|
|
|
import { module, test } from "qunit";
|
2023-10-11 02:38:59 +08:00
|
|
|
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
|
2023-07-07 02:39:23 +08:00
|
|
|
import pretender, { response } from "discourse/tests/helpers/create-pretender";
|
2023-05-24 20:55:20 +08:00
|
|
|
import { publishToMessageBus } from "discourse/tests/helpers/qunit-helpers";
|
2023-10-11 02:38:59 +08:00
|
|
|
import fabricators from "discourse/plugins/chat/discourse/lib/fabricators";
|
2023-05-24 20:55:20 +08:00
|
|
|
|
|
|
|
module(
|
|
|
|
"Discourse Chat | Component | chat-channel | status on mentions",
|
|
|
|
function (hooks) {
|
|
|
|
setupRenderingTest(hooks);
|
|
|
|
|
|
|
|
const channelId = 1;
|
|
|
|
const actingUser = {
|
|
|
|
id: 1,
|
|
|
|
username: "acting_user",
|
|
|
|
};
|
|
|
|
const mentionedUser = {
|
|
|
|
id: 1000,
|
|
|
|
username: "user1",
|
|
|
|
status: {
|
|
|
|
description: "surfing",
|
|
|
|
emoji: "surfing_man",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const mentionedUser2 = {
|
|
|
|
id: 2000,
|
|
|
|
username: "user2",
|
|
|
|
status: {
|
|
|
|
description: "vacation",
|
|
|
|
emoji: "desert_island",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const message = {
|
|
|
|
id: 1891,
|
|
|
|
message: `Hey @${mentionedUser.username}`,
|
|
|
|
cooked: `<p>Hey <a class="mention" href="/u/${mentionedUser.username}">@${mentionedUser.username}</a></p>`,
|
|
|
|
mentioned_users: [mentionedUser],
|
2023-07-27 15:57:03 +08:00
|
|
|
created_at: "2020-08-04T15:00:00.000Z",
|
2023-05-24 20:55:20 +08:00
|
|
|
user: {
|
|
|
|
id: 1,
|
|
|
|
username: "jesse",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
hooks.beforeEach(function () {
|
2023-07-27 15:57:03 +08:00
|
|
|
pretender.get(`/chat/api/channels/1/messages`, () =>
|
2023-07-07 02:39:23 +08:00
|
|
|
response({
|
2023-07-27 15:57:03 +08:00
|
|
|
messages: [message],
|
2023-05-24 20:55:20 +08:00
|
|
|
meta: { can_delete_self: true },
|
|
|
|
})
|
|
|
|
);
|
2024-03-18 15:35:07 +08:00
|
|
|
pretender.get(`/chat/api/me/channels`, () =>
|
|
|
|
response({
|
|
|
|
direct_message_channels: [],
|
|
|
|
public_channels: [],
|
|
|
|
})
|
|
|
|
);
|
2023-05-24 20:55:20 +08:00
|
|
|
|
|
|
|
this.channel = fabricators.channel({
|
|
|
|
id: channelId,
|
|
|
|
currentUserMembership: { following: true },
|
|
|
|
meta: { can_join_chat_channel: false },
|
|
|
|
});
|
2023-11-26 01:10:10 +08:00
|
|
|
this.appEvents = this.container.lookup("service:app-events");
|
2023-05-24 20:55:20 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test("it shows status on mentions", async function (assert) {
|
2023-10-06 20:51:31 +08:00
|
|
|
await render(hbs`<ChatChannel @channel={{this.channel}} />`);
|
2023-05-24 20:55:20 +08:00
|
|
|
|
|
|
|
assertStatusIsRendered(
|
|
|
|
assert,
|
|
|
|
statusSelector(mentionedUser.username),
|
|
|
|
mentionedUser.status
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("it updates status on mentions", async function (assert) {
|
2023-10-06 20:51:31 +08:00
|
|
|
await render(hbs`<ChatChannel @channel={{this.channel}} />`);
|
2023-05-24 20:55:20 +08:00
|
|
|
|
|
|
|
const newStatus = {
|
|
|
|
description: "off to dentist",
|
|
|
|
emoji: "tooth",
|
|
|
|
};
|
|
|
|
|
|
|
|
this.appEvents.trigger("user-status:changed", {
|
|
|
|
[mentionedUser.id]: newStatus,
|
|
|
|
});
|
|
|
|
|
|
|
|
const selector = statusSelector(mentionedUser.username);
|
|
|
|
await waitFor(selector);
|
DEV: FloatKit (#23650)
This PR introduces three new concepts to Discourse codebase through an addon called "FloatKit":
- menu
- tooltip
- toast
## Tooltips
### Component
Simple cases can be express with an API similar to DButton:
```hbs
<DTooltip
@Label={{i18n "foo.bar"}}
@ICON="check"
@content="Something"
/>
```
More complex cases can use blocks:
```hbs
<DTooltip>
<:trigger>
{{d-icon "check"}}
<span>{{i18n "foo.bar"}}</span>
</:trigger>
<:content>
Something
</:content>
</DTooltip>
```
### Service
You can manually show a tooltip using the `tooltip` service:
```javascript
const tooltipInstance = await this.tooltip.show(
document.querySelector(".my-span"),
options
)
// and later manual close or destroy it
tooltipInstance.close();
tooltipInstance.destroy();
// you can also just close any open tooltip through the service
this.tooltip.close();
```
The service also allows you to register event listeners on a trigger, it removes the need for you to manage open/close of a tooltip started through the service:
```javascript
const tooltipInstance = this.tooltip.register(
document.querySelector(".my-span"),
options
)
// when done you can destroy the instance to remove the listeners
tooltipInstance.destroy();
```
Note that the service also allows you to use a custom component as content which will receive `@data` and `@close` as args:
```javascript
const tooltipInstance = await this.tooltip.show(
document.querySelector(".my-span"),
{
component: MyComponent,
data: { foo: 1 }
}
)
```
## Menus
Menus are very similar to tooltips and provide the same kind of APIs:
### Component
```hbs
<DMenu @ICON="plus" @Label={{i18n "foo.bar"}}>
<ul>
<li>Foo</li>
<li>Bat</li>
<li>Baz</li>
</ul>
</DMenu>
```
They also support blocks:
```hbs
<DMenu>
<:trigger>
{{d-icon "plus"}}
<span>{{i18n "foo.bar"}}</span>
</:trigger>
<:content>
<ul>
<li>Foo</li>
<li>Bat</li>
<li>Baz</li>
</ul>
</:content>
</DMenu>
```
### Service
You can manually show a menu using the `menu` service:
```javascript
const menuInstance = await this.menu.show(
document.querySelector(".my-span"),
options
)
// and later manual close or destroy it
menuInstance.close();
menuInstance.destroy();
// you can also just close any open tooltip through the service
this.menu.close();
```
The service also allows you to register event listeners on a trigger, it removes the need for you to manage open/close of a tooltip started through the service:
```javascript
const menuInstance = this.menu.register(
document.querySelector(".my-span"),
options
)
// when done you can destroy the instance to remove the listeners
menuInstance.destroy();
```
Note that the service also allows you to use a custom component as content which will receive `@data` and `@close` as args:
```javascript
const menuInstance = await this.menu.show(
document.querySelector(".my-span"),
{
component: MyComponent,
data: { foo: 1 }
}
)
```
## Toasts
Interacting with toasts is made only through the `toasts` service.
A default component is provided (DDefaultToast) and can be used through dedicated service methods:
- this.toasts.success({ ... });
- this.toasts.warning({ ... });
- this.toasts.info({ ... });
- this.toasts.error({ ... });
- this.toasts.default({ ... });
```javascript
this.toasts.success({
data: {
title: "Foo",
message: "Bar",
actions: [
{
label: "Ok",
class: "btn-primary",
action: (componentArgs) => {
// eslint-disable-next-line no-alert
alert("Closing toast:" + componentArgs.data.title);
componentArgs.close();
},
}
]
},
});
```
You can also provide your own component:
```javascript
this.toasts.show(MyComponent, {
autoClose: false,
class: "foo",
data: { baz: 1 },
})
```
Co-authored-by: Martin Brennan <mjrbrennan@gmail.com>
Co-authored-by: Isaac Janzen <50783505+janzenisaac@users.noreply.github.com>
Co-authored-by: David Taylor <david@taylorhq.com>
Co-authored-by: Jarek Radosz <jradosz@gmail.com>
2023-09-26 19:39:52 +08:00
|
|
|
|
2023-05-24 20:55:20 +08:00
|
|
|
assertStatusIsRendered(
|
|
|
|
assert,
|
|
|
|
statusSelector(mentionedUser.username),
|
|
|
|
newStatus
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("it deletes status on mentions", async function (assert) {
|
2023-10-06 20:51:31 +08:00
|
|
|
await render(hbs`<ChatChannel @channel={{this.channel}} />`);
|
2023-05-24 20:55:20 +08:00
|
|
|
|
|
|
|
this.appEvents.trigger("user-status:changed", {
|
|
|
|
[mentionedUser.id]: null,
|
|
|
|
});
|
|
|
|
|
|
|
|
const selector = statusSelector(mentionedUser.username);
|
|
|
|
await waitFor(selector, { count: 0 });
|
|
|
|
assert.dom(selector).doesNotExist("status is deleted");
|
|
|
|
});
|
|
|
|
|
|
|
|
test("it shows status on mentions on messages that came from Message Bus", async function (assert) {
|
2023-10-06 20:51:31 +08:00
|
|
|
await render(hbs`<ChatChannel @channel={{this.channel}} />`);
|
2023-05-24 20:55:20 +08:00
|
|
|
|
|
|
|
await receiveChatMessageViaMessageBus();
|
|
|
|
|
|
|
|
assertStatusIsRendered(
|
|
|
|
assert,
|
|
|
|
statusSelector(mentionedUser2.username),
|
|
|
|
mentionedUser2.status
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("it updates status on mentions on messages that came from Message Bus", async function (assert) {
|
2023-10-06 20:51:31 +08:00
|
|
|
await render(hbs`<ChatChannel @channel={{this.channel}} />`);
|
2023-05-24 20:55:20 +08:00
|
|
|
await receiveChatMessageViaMessageBus();
|
|
|
|
|
|
|
|
const newStatus = {
|
|
|
|
description: "off to meeting",
|
|
|
|
emoji: "calendar",
|
|
|
|
};
|
|
|
|
this.appEvents.trigger("user-status:changed", {
|
|
|
|
[mentionedUser2.id]: newStatus,
|
|
|
|
});
|
|
|
|
|
|
|
|
const selector = statusSelector(mentionedUser2.username);
|
|
|
|
await waitFor(selector);
|
|
|
|
assertStatusIsRendered(
|
|
|
|
assert,
|
|
|
|
statusSelector(mentionedUser2.username),
|
|
|
|
newStatus
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("it deletes status on mentions on messages that came from Message Bus", async function (assert) {
|
2023-10-06 20:51:31 +08:00
|
|
|
await render(hbs`<ChatChannel @channel={{this.channel}} />`);
|
2023-05-24 20:55:20 +08:00
|
|
|
await receiveChatMessageViaMessageBus();
|
|
|
|
|
|
|
|
this.appEvents.trigger("user-status:changed", {
|
|
|
|
[mentionedUser2.id]: null,
|
|
|
|
});
|
|
|
|
|
|
|
|
const selector = statusSelector(mentionedUser2.username);
|
|
|
|
await waitFor(selector, { count: 0 });
|
|
|
|
assert.dom(selector).doesNotExist("status is deleted");
|
|
|
|
});
|
|
|
|
|
2023-10-06 20:51:31 +08:00
|
|
|
test("it shows status tooltip", async function (assert) {
|
|
|
|
await render(
|
|
|
|
hbs`<ChatChannel @channel={{this.channel}} /><DInlineTooltip />`
|
|
|
|
);
|
|
|
|
await triggerEvent(statusSelector(mentionedUser.username), "mousemove");
|
2023-07-03 22:09:41 +08:00
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
document
|
|
|
|
.querySelector(".user-status-tooltip-description")
|
|
|
|
.textContent.trim(),
|
2023-10-06 20:51:31 +08:00
|
|
|
mentionedUser.status.description,
|
2023-07-03 22:09:41 +08:00
|
|
|
"status description is correct"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.ok(
|
|
|
|
document.querySelector(
|
2023-10-06 20:51:31 +08:00
|
|
|
`.user-status-message-tooltip img[alt='${mentionedUser.status.emoji}']`
|
2023-07-03 22:09:41 +08:00
|
|
|
),
|
|
|
|
"status emoji is correct"
|
|
|
|
);
|
2023-10-06 20:51:31 +08:00
|
|
|
});
|
2023-07-03 22:09:41 +08:00
|
|
|
|
2023-10-06 20:51:31 +08:00
|
|
|
function assertStatusIsRendered(assert, selector, status) {
|
|
|
|
assert
|
|
|
|
.dom(selector)
|
|
|
|
.exists("status is rendered")
|
|
|
|
.hasAttribute(
|
|
|
|
"src",
|
|
|
|
new RegExp(`${status.emoji}.png`),
|
|
|
|
"status emoji is updated"
|
|
|
|
);
|
2023-07-03 22:09:41 +08:00
|
|
|
}
|
|
|
|
|
2023-05-24 20:55:20 +08:00
|
|
|
async function receiveChatMessageViaMessageBus() {
|
|
|
|
await publishToMessageBus(`/chat/${channelId}`, {
|
|
|
|
chat_message: {
|
|
|
|
id: 2138,
|
|
|
|
message: `Hey @${mentionedUser2.username}`,
|
|
|
|
cooked: `<p>Hey <a class="mention" href="/u/${mentionedUser2.username}">@${mentionedUser2.username}</a></p>`,
|
|
|
|
created_at: "2023-05-18T16:07:59.588Z",
|
|
|
|
excerpt: `Hey @${mentionedUser2.username}`,
|
|
|
|
available_flags: [],
|
|
|
|
chat_channel_id: 7,
|
|
|
|
mentioned_users: [mentionedUser2],
|
|
|
|
user: actingUser,
|
|
|
|
chat_webhook_event: null,
|
|
|
|
uploads: [],
|
|
|
|
},
|
|
|
|
type: "sent",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function statusSelector(username) {
|
2023-07-03 22:09:41 +08:00
|
|
|
return `.mention[href='/u/${username}'] .user-status-message img`;
|
2023-05-24 20:55:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|