[MM-56921] Rendered Latex in Codeblock when rendering disabled (#27060)

* Rendered Latex in Codeblock when rendering disabled

* Fixed Lint error

* Removed render function in Latex Component

* Fix errors preventing LatexBlock unit tests from running

* Updated latex disabled test for Codeblock testing

* Fixed latex_block test lint errors

* Removed Latex disabled test Snapshot

---------

Co-authored-by: Harrison Healey <harrisonmhealey@gmail.com>
Co-authored-by: Mattermost Build <build@mattermost.com>
This commit is contained in:
Harmeet singh 2024-07-10 02:17:33 +05:30 committed by GitHub
parent e3b2b13292
commit 7c2a461de8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 39 additions and 24 deletions

View File

@ -61,7 +61,19 @@ const CodeBlock: React.FC<Props> = ({code, language, searchedContent}: Props) =>
// search term highlighting and overlap them // search term highlighting and overlap them
const [content, setContent] = useState(TextFormatting.sanitizeHtml(code)); const [content, setContent] = useState(TextFormatting.sanitizeHtml(code));
useEffect(() => { useEffect(() => {
SyntaxHighlighting.highlight(usedLanguage, code).then((content) => setContent(content)); let shouldSetContent = true;
SyntaxHighlighting.highlight(usedLanguage, code).then((content) => {
// Ensure the component is still mounted and that usedLanguage and code haven't changed to prevent two
// highlight calls from racing
if (shouldSetContent) {
setContent(content);
}
});
return () => {
shouldSetContent = false;
};
}, [usedLanguage, code]); }, [usedLanguage, code]);
let htmlContent = content; let htmlContent = content;

View File

@ -15,15 +15,6 @@ exports[`components/LatexBlock error in katex 1`] = `
</div> </div>
`; `;
exports[`components/LatexBlock latex is disabled 1`] = `
<div
class="post-body--code tex"
data-testid="latex-disabled"
>
\`\`\`latex e^{i\\pi} + 1 = 0\`\`\`
</div>
`;
exports[`components/LatexBlock should match snapshot 1`] = ` exports[`components/LatexBlock should match snapshot 1`] = `
<div <div
class="post-body--code tex" class="post-body--code tex"

View File

@ -1,12 +1,22 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information. // See LICENSE.txt for license information.
import {render, screen} from '@testing-library/react'; import {screen} from '@testing-library/react';
import React from 'react'; import React from 'react';
import LatexBlock from 'components/latex_block/latex_block'; import LatexBlock from 'components/latex_block/latex_block';
import {withIntl} from 'tests/helpers/intl-test-helper'; import {act, renderWithContext} from 'tests/react_testing_utils';
const actImmediate = () =>
act(
() =>
new Promise<void>((resolve) => {
setImmediate(() => {
resolve();
});
}),
);
describe('components/LatexBlock', () => { describe('components/LatexBlock', () => {
const defaultProps = { const defaultProps = {
@ -15,7 +25,7 @@ describe('components/LatexBlock', () => {
}; };
test('should match snapshot', async () => { test('should match snapshot', async () => {
render(<LatexBlock {...defaultProps}/>); renderWithContext(<LatexBlock {...defaultProps}/>);
const wrapper = await screen.findAllByTestId('latex-enabled'); const wrapper = await screen.findAllByTestId('latex-enabled');
expect(wrapper.length).toBe(1); expect(wrapper.length).toBe(1);
expect(wrapper.at(0)).toMatchSnapshot(); expect(wrapper.at(0)).toMatchSnapshot();
@ -27,10 +37,12 @@ describe('components/LatexBlock', () => {
enableLatex: false, enableLatex: false,
}; };
render(<LatexBlock {...props}/>); const {container} = renderWithContext(<LatexBlock {...props}/>);
const wrapper = await screen.findAllByTestId('latex-disabled');
expect(wrapper.length).toBe(1); expect(screen.getByText('LaTeX')).toBeInTheDocument();
expect(wrapper.at(0)).toMatchSnapshot(); expect(container.querySelector('.post-code__line-numbers')).toBeInTheDocument();
await actImmediate();
}); });
test('error in katex', async () => { test('error in katex', async () => {
@ -39,7 +51,7 @@ describe('components/LatexBlock', () => {
enableLatex: true, enableLatex: true,
}; };
render(withIntl(<LatexBlock {...props}/>)); renderWithContext(<LatexBlock {...props}/>);
const wrapper = await screen.findAllByTestId('latex-enabled'); const wrapper = await screen.findAllByTestId('latex-enabled');
expect(wrapper.length).toBe(1); expect(wrapper.length).toBe(1);
expect(wrapper.at(0)).toMatchSnapshot(); expect(wrapper.at(0)).toMatchSnapshot();

View File

@ -5,6 +5,8 @@ import type {KatexOptions} from 'katex';
import React, {useEffect, useState} from 'react'; import React, {useEffect, useState} from 'react';
import {FormattedMessage} from 'react-intl'; import {FormattedMessage} from 'react-intl';
import CodeBlock from 'components/code_block/code_block';
type Katex = typeof import('katex'); type Katex = typeof import('katex');
type Props = { type Props = {
@ -26,12 +28,10 @@ const LatexBlock = ({
if (!enableLatex || katex === undefined) { if (!enableLatex || katex === undefined) {
return ( return (
<div <CodeBlock
className='post-body--code tex' code={content}
data-testid='latex-disabled' language='latex'
> />
{content}
</div>
); );
} }