discourse/plugins/chat/test/javascripts/components/chat-message-test.js
Joffrey JAFFEUX b5e736504a
PERF: applies optimisations on chat-live pane (#20532)
- group writes when computing separators positions
- shows skeleton only on initial load
- forces date separator to be pinned when first message to prevent a pinned - not pinned - pinned sequence when loading more in past
- relies on `message.visible` property instead of checking `isElementInViewport`
- attempts to load next/prev messages earlier
- do not scroll to on fetch more
- hides `last visit` text while pinned
2023-03-06 16:42:11 +01:00

112 lines
3.6 KiB
JavaScript

import User from "discourse/models/user";
import { render } from "@ember/test-helpers";
import ChatMessage from "discourse/plugins/chat/discourse/models/chat-message";
import { exists } from "discourse/tests/helpers/qunit-helpers";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import hbs from "htmlbars-inline-precompile";
import ChatChannel from "discourse/plugins/chat/discourse/models/chat-channel";
import { module, test } from "qunit";
module("Discourse Chat | Component | chat-message", function (hooks) {
setupRenderingTest(hooks);
function generateMessageProps(messageData = {}) {
const chatChannel = ChatChannel.create({
chatable: { id: 1 },
chatable_type: "Category",
id: 9,
title: "Site",
last_message_sent_at: "2021-11-08T21:26:05.710Z",
current_user_membership: {
unread_count: 0,
muted: false,
},
canInteractWithChat: true,
canDeleteSelf: true,
canDeleteOthers: true,
canFlag: true,
userSilenced: false,
canModerate: true,
});
return {
message: ChatMessage.create(
chatChannel,
Object.assign(
{
id: 178,
message: "from deleted user",
cooked: "<p>from deleted user</p>",
excerpt: "<p>from deleted user</p>",
created_at: "2021-07-22T08:14:16.950Z",
flag_count: 0,
user: User.create({ username: "someguy", id: 1424 }),
edited: false,
},
messageData
)
),
chatChannel,
setReplyTo: () => {},
replyMessageClicked: () => {},
editButtonClicked: () => {},
afterExpand: () => {},
selectingMessages: false,
onStartSelectingMessages: () => {},
onSelectMessage: () => {},
bulkSelectMessages: () => {},
onHoverMessage: () => {},
didShowMessage: () => {},
didHideMessage: () => {},
forceRendering: () => {},
};
}
const template = hbs`
<ChatMessage
@message={{this.message}}
@canInteractWithChat={{this.canInteractWithChat}}
@channel={{this.chatChannel}}
@setReplyTo={{this.setReplyTo}}
@replyMessageClicked={{this.replyMessageClicked}}
@editButtonClicked={{this.editButtonClicked}}
@selectingMessages={{this.selectingMessages}}
@onStartSelectingMessages={{this.onStartSelectingMessages}}
@onSelectMessage={{this.onSelectMessage}}
@bulkSelectMessages={{this.bulkSelectMessages}}
@onHoverMessage={{this.onHoverMessage}}
@didShowMessage={{this.didShowMessage}}
@didHideMessage={{this.didHideMessage}}
@didHideMessage={{this.didHideMessage}}
@forceRendering={{this.forceRendering}}
/>
`;
test("Message with edits", async function (assert) {
this.setProperties(generateMessageProps({ edited: true }));
await render(template);
assert.true(
exists(".chat-message-edited"),
"has the correct edited css class"
);
});
test("Deleted message", async function (assert) {
this.setProperties(generateMessageProps({ deleted_at: moment() }));
await render(template);
assert.true(
exists(".chat-message-deleted .chat-message-expand"),
"has the correct deleted css class and expand button within"
);
});
test("Hidden message", async function (assert) {
this.setProperties(generateMessageProps({ hidden: true }));
await render(template);
assert.true(
exists(".chat-message-hidden .chat-message-expand"),
"has the correct hidden css class and expand button within"
);
});
});