blob: 6056849106ac1a2669f94f433625b9dbd6433d6e [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001import {assert} from 'chai';
2import {renderMarkdown, shouldRenderMarkdown} from './md-helper.js';
3
4describe('shouldRenderMarkdown', () => {
5 it('defaults to false', () => {
6 const actual = shouldRenderMarkdown();
7 assert.isFalse(actual);
8 });
9
10 it('returns true for enabled projects', () => {
11 const actual = shouldRenderMarkdown({project:'astor',
12 availableProjects: new Set(['astor'])});
13 assert.isTrue(actual);
14 });
15
16 it('returns false for disabled projects', () => {
17 const actual = shouldRenderMarkdown({project:'hazelnut',
18 availableProjects: new Set(['astor'])});
19 assert.isFalse(actual);
20 });
21
22 it('user pref can disable markdown', () => {
23 const actual = shouldRenderMarkdown({project:'astor',
24 enabledProjects: new Set(['astor']), enabled: false});
25 assert.isFalse(actual);
26 });
27});
28
29describe('renderMarkdown', () => {
30 it('can render empty string', () => {
31 const actual = renderMarkdown('');
32 assert.equal(actual, '');
33 });
34
35 it('can render basic string', () => {
36 const actual = renderMarkdown('hello world');
37 assert.equal(actual, '<p>hello world</p>\n');
38 });
39
40 it('can render lists', () => {
41 const input = '* First item\n* Second item\n* Third item\n* Fourth item';
42 const actual = renderMarkdown(input);
43 const expected = '<ul>\n<li>First item</li>\n<li>Second item</li>\n' +
44 '<li>Third item</li>\n<li>Fourth item</li>\n</ul>\n';
45 assert.equal(actual, expected);
46 });
47
48 it('can render headings', () => {
49 const actual = renderMarkdown('# Heading level 1\n\n## Heading level 2');
50 assert.equal(actual,
51 '<h1>Heading level 1</h1>\n<h2>Heading level 2</h2>\n');
52 });
53
Adrià Vilanova Martínezac4a6442022-05-15 19:05:13 +020054 it('can render codeblocks', () => {
55 const actual = renderMarkdown('```\nhello world\n```');
56 assert.equal(actual,
57 '<pre><code>hello world\n</code></pre>\n');
58 });
59
Copybara854996b2021-09-07 19:36:02 +000060 describe('can render links', () => {
61 it('for simple links', () => {
62 const actual = renderMarkdown('[clickme](http://google.com)');
63 const expected = `<p><span class="annotated-link"><a title="" ` +
64 `href="http://google.com"><span class="material-icons link">` +
65 `link</span>clickme</a><span class="tooltip">Link destination: ` +
66 `http://google.com</span></span></p>\n`;
67 assert.equal(actual, expected);
68 });
69
70 it('and indicates malformed link', () => {
71 const actual = renderMarkdown('[clickme](google.com)');
72 const expected = `<p><span class="annotated-link"><a title="" ` +
73 `href="google.com"><span class="material-icons link_off">link_off` +
74 `</span>clickme</a><span class="tooltip">Link may be malformed: ` +
75 `google.com</span></span></p>\n`;
76 assert.equal(actual, expected);
77 });
Adrià Vilanova Martínez2d5457a2022-01-13 13:25:39 +010078
79 it('correctly renders emails', () => {
80 const actual = renderMarkdown('[person@google.com](mailto:person@google.com)');
81 const expected = `<p><span class="annotated-link"><a title="" ` +
82 `href="mailto:person@google.com"><span class="material-icons link">` +
83 `link</span>person@google.com</a><span class="tooltip">Link destination: ` +
84 `mailto:person@google.com</span></span></p>\n`;
85 assert.equal(actual, expected);
86 });
87
88 it('correctly renders monorail user links', () => {
89 const actual = renderMarkdown('[person@google.com](/u/person@google.com)');
90 const expected = `<p><span class="annotated-link"><a title="" ` +
91 `href="/u/person@google.com"><span class="material-icons ` +
92 `link">link</span>person@google.com</a><span class="tooltip">Link destination: ` +
93 `/u/person@google.com</span></span></p>\n`
94 assert.equal(actual, expected)
95 });
96
Copybara854996b2021-09-07 19:36:02 +000097 });
98
99 it('preserves bolding from description templates', () => {
100 const input = `<b>What's the problem?</b>\n<b>1.</b> A\n<b>2.</b> B`;
101 const actual = renderMarkdown(input);
102 const expected = `<p><strong>What's the problem?</strong>\n<strong>1.` +
103 `</strong> A\n<strong>2.</strong> B</p>\n`;
104 assert.equal(actual, expected);
105 });
106
107 it('escapes HTML content', () => {
108 let actual = renderMarkdown('<input></input>');
109 assert.equal(actual, '<p>&lt;input&gt;&lt;/input&gt;</p>\n');
110
111 actual = renderMarkdown('<a href="https://google.com">clickme</a>');
112 assert.equal(actual,
Adrià Vilanova Martínezac4a6442022-05-15 19:05:13 +0200113 `<p>&lt;a href="<span class="annotated-link"><a title="" ` +
114 `href="https://google.com&quot;>clickme</a"><span ` +
115 `class="material-icons link_off">link_off</span>` +
116 `https://google.com"&gt;clickme&lt;/a</a><span ` +
117 `class="tooltip">Link may be malformed: ` +
118 `https://google.com"&gt;clickme&lt;/a</span></span>&gt;</p>\n`);
119 });
120
121 it('escapes video content', () => {
122 const actual = renderMarkdown('<video src="//youtube" control></video>');
123 assert.equal(actual, '<p>&lt;video src="//youtube" control&gt;&lt;/video&gt;</p>\n');
Copybara854996b2021-09-07 19:36:02 +0000124 });
125});