File size: 6,729 Bytes
9332ccf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { MarkdownDisplay } from './MarkdownDisplay.js';
import { LoadedSettings } from '../../config/settings.js';
import { renderWithProviders } from '../../test-utils/render.js';
describe('<MarkdownDisplay />', () => {
const baseProps = {
isPending: false,
terminalWidth: 80,
availableTerminalHeight: 40,
};
beforeEach(() => {
vi.clearAllMocks();
});
it('renders nothing for empty text', async () => {
const { lastFrame, unmount } = await renderWithProviders(
<MarkdownDisplay {...baseProps} text="" />,
);
expect(lastFrame({ allowEmpty: true })).toMatchSnapshot();
unmount();
});
it('renders a simple paragraph', async () => {
const text = 'Hello, world.';
const { lastFrame, unmount } = await renderWithProviders(
<MarkdownDisplay {...baseProps} text={text} />,
);
expect(lastFrame()).toMatchSnapshot();
unmount();
});
const lineEndings = [
{ name: 'Windows', eol: '\r\n' },
{ name: 'Unix', eol: '\n' },
];
describe.each(lineEndings)('with $name line endings', ({ eol }) => {
it('renders headers with correct levels', async () => {
const text = `
# Header 1
## Header 2
### Header 3
#### Header 4
`.replace(/\n/g, eol);
const { lastFrame, unmount } = await renderWithProviders(
<MarkdownDisplay {...baseProps} text={text} />,
);
expect(lastFrame()).toMatchSnapshot();
unmount();
});
it('renders a fenced code block with a language', async () => {
const text = '```javascript\nconst x = 1;\nconsole.log(x);\n```'.replace(
/\n/g,
eol,
);
const { lastFrame, unmount } = await renderWithProviders(
<MarkdownDisplay {...baseProps} text={text} />,
);
expect(lastFrame()).toMatchSnapshot();
unmount();
});
it('renders a fenced code block without a language', async () => {
const text = '```\nplain text\n```'.replace(/\n/g, eol);
const { lastFrame, unmount } = await renderWithProviders(
<MarkdownDisplay {...baseProps} text={text} />,
);
expect(lastFrame()).toMatchSnapshot();
unmount();
});
it('handles unclosed (pending) code blocks', async () => {
const text = '```typescript\nlet y = 2;'.replace(/\n/g, eol);
const { lastFrame, unmount } = await renderWithProviders(
<MarkdownDisplay {...baseProps} text={text} isPending={true} />,
);
expect(lastFrame()).toMatchSnapshot();
unmount();
});
it('renders unordered lists with different markers', async () => {
const text = `
- item A
* item B
+ item C
`.replace(/\n/g, eol);
const { lastFrame, unmount } = await renderWithProviders(
<MarkdownDisplay {...baseProps} text={text} />,
);
expect(lastFrame()).toMatchSnapshot();
unmount();
});
it('renders nested unordered lists', async () => {
const text = `
* Level 1
* Level 2
* Level 3
`.replace(/\n/g, eol);
const { lastFrame, unmount } = await renderWithProviders(
<MarkdownDisplay {...baseProps} text={text} />,
);
expect(lastFrame()).toMatchSnapshot();
unmount();
});
it('renders ordered lists', async () => {
const text = `
1. First item
2. Second item
`.replace(/\n/g, eol);
const { lastFrame, unmount } = await renderWithProviders(
<MarkdownDisplay {...baseProps} text={text} />,
);
expect(lastFrame()).toMatchSnapshot();
unmount();
});
it('renders horizontal rules', async () => {
const text = `
Hello
---
World
***
Test
`.replace(/\n/g, eol);
const { lastFrame, unmount } = await renderWithProviders(
<MarkdownDisplay {...baseProps} text={text} />,
);
expect(lastFrame()).toMatchSnapshot();
unmount();
});
it('renders tables correctly', async () => {
const text = `
| Header 1 | Header 2 |
|----------|:--------:|
| Cell 1 | Cell 2 |
| Cell 3 | Cell 4 |
`.replace(/\n/g, eol);
const { lastFrame, unmount } = await renderWithProviders(
<MarkdownDisplay {...baseProps} text={text} />,
);
expect(lastFrame()).toMatchSnapshot();
unmount();
});
it('handles a table at the end of the input', async () => {
const text = `
Some text before.
| A | B |
|---|
| 1 | 2 |`.replace(/\n/g, eol);
const { lastFrame, unmount } = await renderWithProviders(
<MarkdownDisplay {...baseProps} text={text} />,
);
expect(lastFrame()).toMatchSnapshot();
unmount();
});
it('inserts a single space between paragraphs', async () => {
const text = `Paragraph 1.
Paragraph 2.`.replace(/\n/g, eol);
const { lastFrame, unmount } = await renderWithProviders(
<MarkdownDisplay {...baseProps} text={text} />,
);
expect(lastFrame()).toMatchSnapshot();
unmount();
});
it('correctly parses a mix of markdown elements', async () => {
const text = `
# Main Title
Here is a paragraph.
- List item 1
- List item 2
\`\`\`
some code
\`\`\`
Another paragraph.
`.replace(/\n/g, eol);
const { lastFrame, unmount } = await renderWithProviders(
<MarkdownDisplay {...baseProps} text={text} />,
);
expect(lastFrame()).toMatchSnapshot();
unmount();
});
it('hides line numbers in code blocks when showLineNumbers is false', async () => {
const text = '```javascript\nconst x = 1;\n```'.replace(/\n/g, eol);
const settings = new LoadedSettings(
{ path: '', settings: {}, originalSettings: {} },
{ path: '', settings: {}, originalSettings: {} },
{
path: '',
settings: { ui: { showLineNumbers: false } },
originalSettings: { ui: { showLineNumbers: false } },
},
{ path: '', settings: {}, originalSettings: {} },
true,
[],
);
const { lastFrame, unmount } = await renderWithProviders(
<MarkdownDisplay {...baseProps} text={text} />,
{ settings },
);
expect(lastFrame()).toMatchSnapshot();
expect(lastFrame()).not.toContain('1 const x = 1;');
unmount();
});
it('shows line numbers in code blocks by default', async () => {
const text = '```javascript\nconst x = 1;\n```'.replace(/\n/g, eol);
const { lastFrame, unmount } = await renderWithProviders(
<MarkdownDisplay {...baseProps} text={text} />,
);
expect(lastFrame()).toMatchSnapshot();
expect(lastFrame()).toContain('1 const x = 1;');
unmount();
});
});
});
|