blob: cd77d55b97c3483ebd223250517bc41fb1034c6c [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
54 describe('can render links', () => {
55 it('for simple links', () => {
56 const actual = renderMarkdown('[clickme](http://google.com)');
57 const expected = `<p><span class="annotated-link"><a title="" ` +
58 `href="http://google.com"><span class="material-icons link">` +
59 `link</span>clickme</a><span class="tooltip">Link destination: ` +
60 `http://google.com</span></span></p>\n`;
61 assert.equal(actual, expected);
62 });
63
64 it('and indicates malformed link', () => {
65 const actual = renderMarkdown('[clickme](google.com)');
66 const expected = `<p><span class="annotated-link"><a title="" ` +
67 `href="google.com"><span class="material-icons link_off">link_off` +
68 `</span>clickme</a><span class="tooltip">Link may be malformed: ` +
69 `google.com</span></span></p>\n`;
70 assert.equal(actual, expected);
71 });
Adrià Vilanova Martínez2d5457a2022-01-13 13:25:39 +010072
73 it('correctly renders emails', () => {
74 const actual = renderMarkdown('[person@google.com](mailto:person@google.com)');
75 const expected = `<p><span class="annotated-link"><a title="" ` +
76 `href="mailto:person@google.com"><span class="material-icons link">` +
77 `link</span>person@google.com</a><span class="tooltip">Link destination: ` +
78 `mailto:person@google.com</span></span></p>\n`;
79 assert.equal(actual, expected);
80 });
81
82 it('correctly renders monorail user links', () => {
83 const actual = renderMarkdown('[person@google.com](/u/person@google.com)');
84 const expected = `<p><span class="annotated-link"><a title="" ` +
85 `href="/u/person@google.com"><span class="material-icons ` +
86 `link">link</span>person@google.com</a><span class="tooltip">Link destination: ` +
87 `/u/person@google.com</span></span></p>\n`
88 assert.equal(actual, expected)
89 });
90
Copybara854996b2021-09-07 19:36:02 +000091 });
92
93 it('preserves bolding from description templates', () => {
94 const input = `<b>What's the problem?</b>\n<b>1.</b> A\n<b>2.</b> B`;
95 const actual = renderMarkdown(input);
96 const expected = `<p><strong>What's the problem?</strong>\n<strong>1.` +
97 `</strong> A\n<strong>2.</strong> B</p>\n`;
98 assert.equal(actual, expected);
99 });
100
101 it('escapes HTML content', () => {
102 let actual = renderMarkdown('<input></input>');
103 assert.equal(actual, '<p>&lt;input&gt;&lt;/input&gt;</p>\n');
104
105 actual = renderMarkdown('<a href="https://google.com">clickme</a>');
106 assert.equal(actual,
107 '<p>&lt;a href="https://google.com"&gt;clickme&lt;/a&gt;</p>\n');
108 });
109});