Skip to content

Commit

Permalink
Chat: Fix message group container
Browse files Browse the repository at this point in the history
  • Loading branch information
marker-dao authored Aug 30, 2024
1 parent cbba54f commit a44b2df
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class MessageList extends Widget<MessageListOptions> {

private _$content!: dxElementWrapper;

private _scrollable!: Scrollable<unknown>;
private _scrollable?: Scrollable<unknown>;

_getDefaultOptions(): MessageListOptions {
return {
Expand Down Expand Up @@ -60,7 +60,8 @@ class MessageList extends Widget<MessageListOptions> {
}

_createMessageGroupComponent(items: Message[], userId: string | number | undefined): void {
const $messageGroup = $('<div>').appendTo(this._$content);
const $messageGroupContainer = this._scrollable ? this._scrollable.content() : this._$content;
const $messageGroup = $('<div>').appendTo($messageGroupContainer);

const messageGroup = this._createComponent($messageGroup, MessageGroup, {
items,
Expand Down Expand Up @@ -141,11 +142,12 @@ class MessageList extends Widget<MessageListOptions> {
const lastMessageGroup = this._messageGroups[this._messageGroups.length - 1];
const element = lastMessageGroup.$element()[0];

this._scrollable.scrollToElement(element);
this._scrollable?.scrollToElement(element);
}

_clean(): void {
this._messageGroups = [];
this._scrollable = undefined;

super._clean();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ QUnit.module('Chat', moduleConfig, () => {
assert.strictEqual(lastItem, newMessage);
});

QUnit.test('Message Group should be created if items are empty', function(assert) {
QUnit.test('Message Group should be created if items was empty', function(assert) {
this.instance.option({ items: [] });

const author = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import $ from 'jquery';
import MessageList from '__internal/ui/chat/chat_message_list';
import Scrollable from 'ui/scroll_view/ui.scrollable';
import {
generateMessages, userFirst, userSecond,
NOW, MOCK_COMPANION_USER_ID, MOCK_CURRENT_USER_ID
generateMessages,
userFirst,
NOW,
MOCK_COMPANION_USER_ID,
MOCK_CURRENT_USER_ID,
} from './chat.tests.js';
import MessageGroup from '__internal/ui/chat/chat_message_group';

Expand All @@ -22,7 +25,9 @@ const moduleConfig = {
this.instance = new MessageList($('#messageList'), options);
this.$element = $(this.instance.$element());

this.scrollable = Scrollable.getInstance(this.$element.find(`.${SCROLLABLE_CLASS}`));
this.getScrollable = () => Scrollable.getInstance(this.$element.find(`.${SCROLLABLE_CLASS}`));

this.scrollable = this.getScrollable();
};

this.reinit = (options) => {
Expand Down Expand Up @@ -60,6 +65,61 @@ QUnit.module('MessageList', moduleConfig, () => {
QUnit.test('scrollable should be rendered inside root element', function(assert) {
assert.ok(Scrollable.getInstance(this.$element.children().first()) instanceof Scrollable);
});

QUnit.test('Message Group should be rendered in the scrollable content', function(assert) {
const newMessage = {
author: { id: MOCK_CURRENT_USER_ID },
timestamp: NOW,
text: 'NEW MESSAGE',
};

this.reinit({ items: [newMessage] });

const $messageGroups = $(this.scrollable.content()).find(`.${CHAT_MESSAGE_GROUP_CLASS}`);

assert.strictEqual($messageGroups.length, 1);
});

QUnit.test('Message Group should be rendered in the scrollable content after adding 1 new message', function(assert) {
const newMessage = {
author: { id: MOCK_CURRENT_USER_ID },
timestamp: NOW,
text: 'NEW MESSAGE',
};

this.instance.option({ items: [newMessage] });

const $messageGroups = $(this.scrollable.content()).find(`.${CHAT_MESSAGE_GROUP_CLASS}`);

assert.strictEqual($messageGroups.length, 1);
});

QUnit.test('Message Group should be rendered in the scrollable content after adding 1 new message to items', function(assert) {
const items = generateMessages(52);

this.reinit({ items });

const newMessage = {
author: { id: 'another user' },
timestamp: NOW,
text: 'NEW MESSAGE',
};

this.instance.option({ items: [...items, newMessage] });

const $messageGroups = $(this.scrollable.content()).find(`.${CHAT_MESSAGE_GROUP_CLASS}`);

assert.strictEqual($messageGroups.length, 27);
});

QUnit.test('Message Group should be rendered in the scrollable content after updating items at runtime', function(assert) {
this.instance.option({ items: generateMessages(52) });

const scrollableContent = this.getScrollable().content();
const $messageGroups = $(scrollableContent).find(`.${CHAT_MESSAGE_GROUP_CLASS}`);

assert.strictEqual($messageGroups.length, 26);
});
});

QUnit.module('MessageGroup integration', () => {
Expand Down

0 comments on commit a44b2df

Please sign in to comment.