Skip to content

Commit 1ba5930

Browse files
committed
test: improve test coverage for MarkdownRenderer
1 parent 9aac283 commit 1ba5930

1 file changed

Lines changed: 85 additions & 34 deletions

File tree

‎packages/frontend/test/unit/components/Content/MarkdownContent/MarkdownRenderer/MarkdownRenderer.test.tsx‎

Lines changed: 85 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,6 @@ jest.mock('../../../../../../src/components/Content/MarkdownContent/MarkdownRend
2626
jest.mock('react-markdown', () => {
2727
const React = jest.requireActual('react');
2828
const ActualReactMarkdown = jest.requireActual('react-markdown').default;
29-
30-
31-
const MockCode = (props: React.ComponentProps<'pre'>) => {
32-
if (props.className && props.className.includes('language-mermaid')) {
33-
return <div data-testid="mock-mermaid-chart">{props.children}</div>;
34-
}
35-
return (
36-
<pre data-testid="mock-syntax-highlighter" className={props.className}>
37-
<code data-testid="mock-syntax-highlighter-code">{props.children}</code>
38-
</pre>
39-
);
40-
};
4129
const MockH1 = (props: React.ComponentProps<'h1'>) => <h1 data-testid="mock-h1" {...props}>{props.children}</h1>;
4230
const MockP = (props: React.ComponentProps<'p'>) => <p data-testid="mock-p" {...props}>{props.children}</p>;
4331

@@ -51,11 +39,50 @@ jest.mock('react-markdown', () => {
5139
}) => {
5240
const finalComponents = {
5341
...components,
54-
55-
code: MockCode,
5642
h1: MockH1,
5743
p: MockP,
5844
};
45+
46+
if (children === '__CODE__' && finalComponents.code) {
47+
return (
48+
<div data-testid="mock-react-markdown-root">
49+
{finalComponents.code({
50+
inline: false,
51+
className: 'language-javascript',
52+
children: 'console.log("Hello");',
53+
} as React.ComponentProps<'code'>)}
54+
</div>
55+
);
56+
}
57+
58+
if (children === '__MERMAID__' && finalComponents.code) {
59+
return (
60+
<div data-testid="mock-react-markdown-root">
61+
{finalComponents.code({
62+
inline: false,
63+
className: 'language-mermaid',
64+
children: 'graph TD; A-->B;',
65+
} as React.ComponentProps<'code'>)}
66+
</div>
67+
);
68+
}
69+
70+
if (children === '__TABLE__' && finalComponents.table) {
71+
return (
72+
<div data-testid="mock-react-markdown-root">
73+
{finalComponents.table({
74+
children: (
75+
<tbody>
76+
<tr>
77+
<td>mdts</td>
78+
</tr>
79+
</tbody>
80+
),
81+
} as React.ComponentProps<'table'>)}
82+
</div>
83+
);
84+
}
85+
5986
return (
6087
<div data-testid="mock-react-markdown-root">
6188
<ActualReactMarkdown components={finalComponents} remarkPlugins={remarkPlugins} rehypePlugins={rehypePlugins}>
@@ -69,27 +96,34 @@ jest.mock('react-markdown', () => {
6996

7097
// Mock SyntaxHighlighter
7198
jest.mock('react-syntax-highlighter', () => ({
72-
Prism: {
73-
SyntaxHighlighter: ({
74-
children,
75-
language,
76-
...props
77-
}: {
78-
children: React.ReactNode;
79-
language: string;
80-
[key: string]: unknown;
81-
}) => (
82-
<pre data-testid="mock-syntax-highlighter" className={`language-${language}`} {...props}>
83-
<code data-testid="mock-syntax-highlighter-code">{children}</code>
84-
</pre>
85-
),
86-
},
99+
PrismAsyncLight: (props: {
100+
children: React.ReactNode;
101+
language: string;
102+
className?: string;
103+
customStyle?: unknown;
104+
showLineNumbers?: boolean;
105+
codeTagProps?: unknown;
106+
PreTag?: unknown;
107+
[key: string]: unknown;
108+
}) => (
109+
<pre
110+
data-testid="mock-syntax-highlighter"
111+
className={props.className}
112+
data-language={props.language}
113+
>
114+
<code data-testid="mock-syntax-highlighter-code">{props.children}</code>
115+
</pre>
116+
),
87117
nightOwl: {},
88118
prism: {},
89119
}));
90120

91121

92-
const renderWithProviders = (content: string, selectedFilePath: string | null = null) => {
122+
const renderWithProviders = (
123+
content: string,
124+
selectedFilePath: string | null = null,
125+
enableBreaks = false
126+
) => {
93127
const theme = createTheme({
94128
palette: {
95129
mode: 'light',
@@ -122,7 +156,11 @@ const renderWithProviders = (content: string, selectedFilePath: string | null =
122156
<Provider store={store}>
123157
<BrowserRouter>
124158
<ThemeProvider theme={theme}>
125-
<MarkdownRenderer content={content} selectedFilePath={selectedFilePath} />
159+
<MarkdownRenderer
160+
content={content}
161+
selectedFilePath={selectedFilePath}
162+
enableBreaks={enableBreaks}
163+
/>
126164
</ThemeProvider>
127165
</BrowserRouter>
128166
</Provider>
@@ -146,15 +184,15 @@ describe('MarkdownRenderer', () => {
146184
});
147185

148186
it('renders code blocks with syntax highlighting', () => {
149-
const content = '```javascript\nconsole.log("Hello");\n```';
187+
const content = '__CODE__';
150188
renderWithProviders(content);
151189
expect(screen.getByTestId('mock-syntax-highlighter')).toBeInTheDocument();
152190
expect(screen.getByTestId('mock-syntax-highlighter-code')).toHaveTextContent('console.log("Hello");');
153-
expect(screen.getByTestId('mock-syntax-highlighter')).toHaveClass('language-javascript');
191+
expect(screen.getByTestId('mock-syntax-highlighter')).toHaveAttribute('data-language', 'javascript');
154192
});
155193

156194
it('renders mermaid diagrams for mermaid code blocks', () => {
157-
const content = '```mermaid\ngraph TD; A-->B;\n```';
195+
const content = '__MERMAID__';
158196
renderWithProviders(content);
159197
expect(screen.getByTestId('mock-mermaid-chart')).toBeInTheDocument();
160198
expect(screen.getByTestId('mock-mermaid-chart')).toHaveTextContent('graph TD; A-->B;');
@@ -204,4 +242,17 @@ describe('MarkdownRenderer', () => {
204242
const link = screen.getByTestId('markdown-link');
205243
expect(link).toHaveAttribute('href', '#section');
206244
});
245+
246+
it('renders single line breaks as breaks when enabled', () => {
247+
const { container } = renderWithProviders('First line\nSecond line', null, true);
248+
expect(container.querySelector('br')).toBeInTheDocument();
249+
});
250+
251+
it('wraps markdown tables in a table wrapper', () => {
252+
const content = '__TABLE__';
253+
const { container } = renderWithProviders(content);
254+
expect(container.querySelector('.table-wrapper')).toBeInTheDocument();
255+
expect(screen.getByRole('table')).toBeInTheDocument();
256+
expect(screen.getByText('mdts')).toBeInTheDocument();
257+
});
207258
});

0 commit comments

Comments
 (0)