blob: 9f7dba10c80160af890a35128156377a64135ea9 [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 });
72 });
73
74 it('preserves bolding from description templates', () => {
75 const input = `<b>What's the problem?</b>\n<b>1.</b> A\n<b>2.</b> B`;
76 const actual = renderMarkdown(input);
77 const expected = `<p><strong>What's the problem?</strong>\n<strong>1.` +
78 `</strong> A\n<strong>2.</strong> B</p>\n`;
79 assert.equal(actual, expected);
80 });
81
82 it('escapes HTML content', () => {
83 let actual = renderMarkdown('<input></input>');
84 assert.equal(actual, '<p>&lt;input&gt;&lt;/input&gt;</p>\n');
85
86 actual = renderMarkdown('<a href="https://google.com">clickme</a>');
87 assert.equal(actual,
88 '<p>&lt;a href="https://google.com"&gt;clickme&lt;/a&gt;</p>\n');
89 });
90});