diff --git a/webapp/channels/src/components/code_block/code_block.tsx b/webapp/channels/src/components/code_block/code_block.tsx index d1fb5774ca..73443f2428 100644 --- a/webapp/channels/src/components/code_block/code_block.tsx +++ b/webapp/channels/src/components/code_block/code_block.tsx @@ -61,7 +61,19 @@ const CodeBlock: React.FC = ({code, language, searchedContent}: Props) => // search term highlighting and overlap them const [content, setContent] = useState(TextFormatting.sanitizeHtml(code)); 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]); let htmlContent = content; diff --git a/webapp/channels/src/components/latex_block/__snapshots__/latex_block.test.tsx.snap b/webapp/channels/src/components/latex_block/__snapshots__/latex_block.test.tsx.snap index bd1d799984..b5d20e238e 100644 --- a/webapp/channels/src/components/latex_block/__snapshots__/latex_block.test.tsx.snap +++ b/webapp/channels/src/components/latex_block/__snapshots__/latex_block.test.tsx.snap @@ -15,15 +15,6 @@ exports[`components/LatexBlock error in katex 1`] = ` `; -exports[`components/LatexBlock latex is disabled 1`] = ` -
- \`\`\`latex e^{i\\pi} + 1 = 0\`\`\` -
-`; - exports[`components/LatexBlock should match snapshot 1`] = `
+ act( + () => + new Promise((resolve) => { + setImmediate(() => { + resolve(); + }); + }), + ); describe('components/LatexBlock', () => { const defaultProps = { @@ -15,7 +25,7 @@ describe('components/LatexBlock', () => { }; test('should match snapshot', async () => { - render(); + renderWithContext(); const wrapper = await screen.findAllByTestId('latex-enabled'); expect(wrapper.length).toBe(1); expect(wrapper.at(0)).toMatchSnapshot(); @@ -27,10 +37,12 @@ describe('components/LatexBlock', () => { enableLatex: false, }; - render(); - const wrapper = await screen.findAllByTestId('latex-disabled'); - expect(wrapper.length).toBe(1); - expect(wrapper.at(0)).toMatchSnapshot(); + const {container} = renderWithContext(); + + expect(screen.getByText('LaTeX')).toBeInTheDocument(); + expect(container.querySelector('.post-code__line-numbers')).toBeInTheDocument(); + + await actImmediate(); }); test('error in katex', async () => { @@ -39,7 +51,7 @@ describe('components/LatexBlock', () => { enableLatex: true, }; - render(withIntl()); + renderWithContext(); const wrapper = await screen.findAllByTestId('latex-enabled'); expect(wrapper.length).toBe(1); expect(wrapper.at(0)).toMatchSnapshot(); diff --git a/webapp/channels/src/components/latex_block/latex_block.tsx b/webapp/channels/src/components/latex_block/latex_block.tsx index fc7b196157..836f70ee74 100644 --- a/webapp/channels/src/components/latex_block/latex_block.tsx +++ b/webapp/channels/src/components/latex_block/latex_block.tsx @@ -5,6 +5,8 @@ import type {KatexOptions} from 'katex'; import React, {useEffect, useState} from 'react'; import {FormattedMessage} from 'react-intl'; +import CodeBlock from 'components/code_block/code_block'; + type Katex = typeof import('katex'); type Props = { @@ -26,12 +28,10 @@ const LatexBlock = ({ if (!enableLatex || katex === undefined) { return ( -
- {content} -
+ ); }