Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/components/ChannelPreview/__tests__/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import React from 'react';
import ReactMarkdown from 'react-markdown';
import '@testing-library/jest-dom';
import { nanoid } from 'nanoid';

Expand All @@ -14,6 +16,7 @@ import {

import { getDisplayImage, getDisplayTitle, getLatestMessagePreview } from '../utils';
import { generateStaticLocationResponse } from '../../../mock-builders';
import { render } from '@testing-library/react';

describe('ChannelPreview utils', () => {
const clientUser = generateUser();
Expand Down Expand Up @@ -55,16 +58,45 @@ describe('ChannelPreview utils', () => {
}),
],
});
const channelWithHTMLInMessage = generateChannel({
messages: [
generateMessage({
attachments: [generateImageAttachment()],
text:
'<h1>Hello, world!</h1> \n' +
'<p>This is my first web page.</p> \n' +
'<p>It contains a <strong>main heading</strong> and <em> paragraph </em>.</p>',
}),
],
});

const expectedTextWithHTMLRendering =
'<h1>Hello, world!</h1> <p>This is my first web page.</p> <p>It contains a <strong>main heading</strong> and <em> paragraph </em>.</p>';

function isReactMarkdownElement(x) {
return React.isValidElement(x) && x.type === ReactMarkdown;
}

it.each([
['Nothing yet...', 'channelWithEmptyMessage', channelWithEmptyMessage],
['Message deleted', 'channelWithDeletedMessage', channelWithDeletedMessage],
['🏙 Attachment...', 'channelWithAttachmentMessage', channelWithAttachmentMessage],
['📍Shared location', 'channelWithLocationMessage', channelWithLocationMessage],
[
expectedTextWithHTMLRendering,
'channelWithHTMLInMessage',
channelWithHTMLInMessage,
],
])('should return %s for %s', async (expectedValue, testCaseName, c) => {
const t = (text) => text;
const channel = await getQueriedChannelInstance(c);
expect(getLatestMessagePreview(channel, t)).toBe(expectedValue);
const preview = getLatestMessagePreview(channel, t);
if (isReactMarkdownElement(preview)) {
const { container } = render(preview);
expect(container).toHaveTextContent(expectedValue);
} else {
expect(getLatestMessagePreview(channel, t)).toBe(expectedValue);
}
});
});

Expand Down
12 changes: 11 additions & 1 deletion src/components/ChannelPreview/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@ import type { Channel, PollVote, TranslationLanguages, UserResponse } from 'stre

import type { TranslationContextValue } from '../../context/TranslationContext';
import type { ChatContextValue } from '../../context';
import type { PluggableList } from 'unified';
import { htmlToTextPlugin } from '../Message';
import remarkGfm from 'remark-gfm';

const remarkPlugins: PluggableList = [
htmlToTextPlugin,
[remarkGfm, { singleTilde: false }],
];

export const renderPreviewText = (text: string) => (
<ReactMarkdown skipHtml>{text}</ReactMarkdown>
<ReactMarkdown remarkPlugins={remarkPlugins} skipHtml>
{text}
</ReactMarkdown>
);

const getLatestPollVote = (latestVotesByOption: Record<string, PollVote[]>) => {
Expand Down