MM-59099 Show invalid emoji text with its original case (#27603)

Этот коммит содержится в:
Harrison Healey
2024-07-15 12:51:20 -04:00
коммит произвёл GitHub
родитель d3dac41cda
Коммит fd0f1cf87e
5 изменённых файлов: 50 добавлений и 8 удалений

Просмотреть файл

@@ -9,6 +9,7 @@ import PostEmoji from './post_emoji';
describe('PostEmoji', () => { describe('PostEmoji', () => {
const baseProps = { const baseProps = {
children: ':emoji:',
imageUrl: '/api/v4/emoji/1234/image', imageUrl: '/api/v4/emoji/1234/image',
name: 'emoji', name: 'emoji',
}; };
@@ -26,7 +27,7 @@ describe('PostEmoji', () => {
expect(screen.queryByTestId('postEmoji.:' + baseProps.name + ':')).toHaveTextContent(`:${baseProps.name}:`); expect(screen.queryByTestId('postEmoji.:' + baseProps.name + ':')).toHaveTextContent(`:${baseProps.name}:`);
}); });
test('should render original text when imageUrl is empty', () => { test('should render children as fallback when imageUrl is empty', () => {
const props = { const props = {
...baseProps, ...baseProps,
imageUrl: '', imageUrl: '',

Просмотреть файл

@@ -5,7 +5,8 @@ import React from 'react';
import WithTooltip from 'components/with_tooltip'; import WithTooltip from 'components/with_tooltip';
interface Props { export interface Props {
children: React.ReactNode;
name: string; name: string;
imageUrl: string; imageUrl: string;
} }
@@ -15,12 +16,12 @@ declare module 'react' {
} }
} }
const PostEmoji = ({name, imageUrl}: Props) => { const PostEmoji = ({children, name, imageUrl}: Props) => {
const emojiText = `:${name}:`; const emojiText = `:${name}:`;
const backgroundImageUrl = `url(${imageUrl})`; const backgroundImageUrl = `url(${imageUrl})`;
if (!imageUrl) { if (!imageUrl) {
return <>{emojiText}</>; return <>{children}</>;
} }
return ( return (
@@ -37,7 +38,7 @@ const PostEmoji = ({name, imageUrl}: Props) => {
data-testid={`postEmoji.${emojiText}`} data-testid={`postEmoji.${emojiText}`}
style={{backgroundImage: backgroundImageUrl}} style={{backgroundImage: backgroundImageUrl}}
> >
{emojiText} {children}
</span> </span>
</WithTooltip> </WithTooltip>
); );

Просмотреть файл

@@ -90,5 +90,5 @@ export function handleEmoticons(
} }
export function renderEmoji(name: string, matchText: string): string { export function renderEmoji(name: string, matchText: string): string {
return `<span data-emoticon="${name.toLowerCase()}">${matchText.toLowerCase()}</span>`; return `<span data-emoticon="${name.toLowerCase()}">${matchText}</span>`;
} }

Просмотреть файл

@@ -6,6 +6,7 @@ import {shallow} from 'enzyme';
import AtMention from 'components/at_mention'; import AtMention from 'components/at_mention';
import MarkdownImage from 'components/markdown_image'; import MarkdownImage from 'components/markdown_image';
import {renderWithContext, screen} from 'tests/react_testing_utils';
import Constants from 'utils/constants'; import Constants from 'utils/constants';
import EmojiMap from 'utils/emoji_map'; import EmojiMap from 'utils/emoji_map';
import messageHtmlToComponent from 'utils/message_html_to_component'; import messageHtmlToComponent from 'utils/message_html_to_component';
@@ -146,4 +147,43 @@ const myFunction = () => {
expect(messageHtmlToComponent(html)).toMatchSnapshot(); expect(messageHtmlToComponent(html)).toMatchSnapshot();
}); });
describe('emojis', () => {
test('should render valid named emojis as spans with background images', () => {
const input = 'These are emojis: :taco: :astronaut:';
const {container} = renderWithContext(messageHtmlToComponent(TextFormatting.formatText(input, {}, emptyEmojiMap)));
expect(screen.getByTestId('postEmoji.:taco:')).toBeInTheDocument();
expect(screen.getByTestId('postEmoji.:taco:').getAttribute('style')).toContain('background-image');
expect(screen.getByTestId('postEmoji.:astronaut:')).toBeInTheDocument();
expect(screen.getByTestId('postEmoji.:astronaut:').getAttribute('style')).toContain('background-image');
expect(container).toHaveTextContent('These are emojis: :taco: :astronaut:');
});
test('should render invalid named emojis as spans with background images', () => {
const input = 'These are emojis: :fake: :notAnEmoji:';
const {container} = renderWithContext(messageHtmlToComponent(TextFormatting.formatText(input, {}, emptyEmojiMap)));
expect(screen.queryByTestId('postEmoji.:taco:')).not.toBeInTheDocument();
expect(screen.queryByTestId('postEmoji.:astronaut:')).not.toBeInTheDocument();
expect(container).toHaveTextContent('These are emojis: :fake: :notAnEmoji:');
});
test('should render supported unicode emojis as spans with background images', () => {
const input = 'These are emojis: 🌮 🧑‍🚀';
const {container} = renderWithContext(messageHtmlToComponent(TextFormatting.formatText(input, {}, emptyEmojiMap)));
expect(screen.getByTestId('postEmoji.:taco:')).toBeInTheDocument();
expect(screen.getByTestId('postEmoji.:taco:').getAttribute('style')).toContain('background-image');
expect(screen.getByTestId('postEmoji.:astronaut:')).toBeInTheDocument();
expect(screen.getByTestId('postEmoji.:astronaut:').getAttribute('style')).toContain('background-image');
expect(container).toHaveTextContent('These are emojis: 🌮 🧑‍🚀');
});
});
}); });

Просмотреть файл

@@ -181,10 +181,10 @@ export function messageHtmlToComponent(html: string, options: Options = {}) {
processingInstructions.push({ processingInstructions.push({
replaceChildren: true, replaceChildren: true,
shouldProcessNode: (node: any) => node.attribs && node.attribs[emojiAttrib], shouldProcessNode: (node: any) => node.attribs && node.attribs[emojiAttrib],
processNode: (node: any) => { processNode: (node: any, children: any) => {
const emojiName = node.attribs[emojiAttrib]; const emojiName = node.attribs[emojiAttrib];
return <PostEmoji name={emojiName}/>; return <PostEmoji name={emojiName}>{children}</PostEmoji>;
}, },
}); });
} }