)}
- {latestMessage}
+
+ {latestMessagePreview}
+
);
diff --git a/src/components/ChannelPreview/__tests__/ChannelPreview.test.js b/src/components/ChannelPreview/__tests__/ChannelPreview.test.js
index b95e59416b..ccc882d9b0 100644
--- a/src/components/ChannelPreview/__tests__/ChannelPreview.test.js
+++ b/src/components/ChannelPreview/__tests__/ChannelPreview.test.js
@@ -8,6 +8,7 @@ import { Chat } from '../../Chat';
import { ChatContext } from '../../../context/ChatContext';
import {
+ dispatchChannelTruncatedEvent,
dispatchMessageDeletedEvent,
dispatchMessageNewEvent,
dispatchMessageUpdatedEvent,
@@ -24,11 +25,15 @@ import {
useMockedApis,
} from 'mock-builders';
+const EMPTY_CHANNEL_PREVIEW_TEXT = 'Empty channel';
+
const PreviewUIComponent = (props) => (
<>
{props.channel.id}
{props.unread}
- {props.lastMessage && props.lastMessage.text}
+
+ {props.lastMessage ? props.lastMessage.text : EMPTY_CHANNEL_PREVIEW_TEXT}
+
>
);
@@ -65,7 +70,12 @@ describe('ChannelPreview', () => {
beforeEach(async () => {
client = await getTestClientWithUser(user);
- useMockedApis(client, [queryChannelsApi([generateChannel(), generateChannel()])]);
+ useMockedApis(client, [
+ queryChannelsApi([
+ generateChannel({ messages: Array.from({ length: 5 }, generateMessage) }),
+ generateChannel({ messages: Array.from({ length: 5 }, generateMessage) }),
+ ]),
+ ]);
[c0, c1] = await client.queryChannels({}, {});
});
@@ -132,10 +142,11 @@ describe('ChannelPreview', () => {
['message.new', dispatchMessageNewEvent],
['message.updated', dispatchMessageUpdatedEvent],
['message.deleted', dispatchMessageDeletedEvent],
+ ['message.undeleted', dispatchMessageDeletedEvent],
];
describe.each(eventCases)('On %s event', (eventType, dispatcher) => {
- it('should update lastMessage', async () => {
+ it('should update latest message preview', async () => {
const newUnreadCount = getRandomInt(1, 10);
c0.countUnread = () => newUnreadCount;
@@ -151,9 +162,10 @@ describe('ChannelPreview', () => {
expect(getByTestId('channel-id')).toBeInTheDocument();
});
- const message = generateMessage();
- act(() => {
- dispatcher(client, message, c0);
+ const message =
+ eventType === 'message.new' ? generateMessage() : c0.state.messages.slice(-1)[0];
+ await act(async () => {
+ await dispatcher(client, message, c0);
});
await expectLastEventMessageToBe(getByTestId, message.text);
@@ -233,6 +245,61 @@ describe('ChannelPreview', () => {
});
});
+ it('on channel.truncated event should update latest message preview', async () => {
+ const newUnreadCount = getRandomInt(1, 10);
+ c0.countUnread = () => newUnreadCount;
+
+ const { getByTestId } = renderComponent(
+ {
+ activeChannel: c1,
+ channel: c0,
+ },
+ render,
+ );
+
+ await waitFor(() => {
+ expect(getByTestId('channel-id')).toBeInTheDocument();
+ });
+
+ await act(async () => {
+ await dispatchChannelTruncatedEvent(client, c0);
+ });
+
+ await expectLastEventMessageToBe(getByTestId, EMPTY_CHANNEL_PREVIEW_TEXT);
+ });
+
+ it.each([
+ ['message.updated', dispatchMessageUpdatedEvent],
+ ['message.deleted', dispatchMessageDeletedEvent],
+ ['message.undeleted', dispatchMessageDeletedEvent],
+ ])(
+ 'on %s event should not update latest message preview for the non-last message',
+ async (_, dispatcher) => {
+ const newUnreadCount = getRandomInt(1, 10);
+ c0.countUnread = () => newUnreadCount;
+
+ const { getByTestId } = renderComponent(
+ {
+ activeChannel: c1,
+ channel: c0,
+ },
+ render,
+ );
+
+ await waitFor(() => {
+ expect(getByTestId('channel-id')).toBeInTheDocument();
+ });
+
+ const lastMessage = c0.state.messages.slice(-1)[0];
+ const penultimateMessage = c0.state.messages.slice(-2)[0];
+ await act(async () => {
+ await dispatcher(client, penultimateMessage, c0);
+ });
+
+ await expectLastEventMessageToBe(getByTestId, lastMessage.text);
+ },
+ );
+
describe('notification.mark_read', () => {
it('should set unread count to 0 for event missing CID', () => {
const unreadCount = getRandomInt(1, 10);
diff --git a/src/components/ChannelPreview/__tests__/ChannelPreviewMessenger.test.js b/src/components/ChannelPreview/__tests__/ChannelPreviewMessenger.test.js
index 36447f8b2c..56b26c7d4f 100644
--- a/src/components/ChannelPreview/__tests__/ChannelPreviewMessenger.test.js
+++ b/src/components/ChannelPreview/__tests__/ChannelPreviewMessenger.test.js
@@ -30,7 +30,7 @@ describe('ChannelPreviewMessenger', () => {
channel={channel}
displayImage='https://randomimage.com/src.jpg'
displayTitle='Channel name'
- latestMessage='Latest message!'
+ latestMessagePreview='Latest message!'
setActiveChannel={jest.fn()}
unread={10}
{...props}
diff --git a/src/components/ChannelPreview/utils.tsx b/src/components/ChannelPreview/utils.tsx
index 3efaa3a6f6..a8cc5bd8c5 100644
--- a/src/components/ChannelPreview/utils.tsx
+++ b/src/components/ChannelPreview/utils.tsx
@@ -32,8 +32,7 @@ export const getLatestMessagePreview = <
}
if (previewTextToRender) {
- const renderedText = renderPreviewText(previewTextToRender);
- return renderedText;
+ return renderPreviewText(previewTextToRender);
}
if (latestMessage.command) {
diff --git a/src/mock-builders/event/messageUndeleted.js b/src/mock-builders/event/messageUndeleted.js
new file mode 100644
index 0000000000..5407563fde
--- /dev/null
+++ b/src/mock-builders/event/messageUndeleted.js
@@ -0,0 +1,10 @@
+export default (client, message, channel = {}) => {
+ const [channel_id, channel_type] = channel.cid.split(':');
+ client.dispatchEvent({
+ channel_id,
+ channel_type,
+ cid: channel.cid,
+ message,
+ type: 'message.undeleted',
+ });
+};