Skip to content

Commit fa4472c

Browse files
committed
build: fix up markdown renderer tests
Fixes that the tests for the Markdown renderer weren't compiling after the recent changes. I've also reworked them to make them closer to how they work inside of our scripts.
1 parent 6fb6026 commit fa4472c

File tree

2 files changed

+129
-133
lines changed

2 files changed

+129
-133
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import {DocsMarkdownRenderer} from './docs-marked-renderer.mjs';
2+
import * as marked from 'marked';
3+
4+
describe('DocsMarkdownRenderer', () => {
5+
let renderer: DocsMarkdownRenderer;
6+
beforeEach(() => {
7+
renderer = new DocsMarkdownRenderer();
8+
});
9+
10+
function transform(markdown: string): string {
11+
marked.setOptions({renderer});
12+
13+
return renderer.finalizeOutput(marked.parse(markdown, {async: false}), 'test.html');
14+
}
15+
16+
it('generates regular headings for h1 and h2', () => {
17+
expect(transform('# a')).toContain('<h1>a</h1>');
18+
expect(transform('## b')).toContain('<h2 ');
19+
});
20+
21+
it('creates header link for h3 and h4 headings', () => {
22+
expectEqualIgnoreLeadingWhitespace(
23+
transform('### header 3'),
24+
`
25+
<div class="docs-markdown">
26+
<h3 id="header-3" class="docs-header-link">
27+
<span header-link="header-3"></span>
28+
header 3
29+
</h3>
30+
</div>
31+
`,
32+
);
33+
expectEqualIgnoreLeadingWhitespace(
34+
transform('#### header 4'),
35+
`
36+
<div class="docs-markdown">
37+
<h4 id="header-4" class="docs-header-link">
38+
<span header-link="header-4"></span>
39+
header 4
40+
</h4>
41+
</div>
42+
`,
43+
);
44+
});
45+
46+
it('generates links', () => {
47+
expect(transform('[some text](something "some title")')).toContain(
48+
'<a href="guide/something" title="some title">some text</a>',
49+
);
50+
expect(transform('[some text](#some-hash "some title")\n ### some hash')).toContain(
51+
'<a href="#some-hash" title="some title">some text</a>',
52+
);
53+
expect(transform('[some text](https://google.com)')).toContain(
54+
'<a href="https://google.com">some text</a>',
55+
);
56+
});
57+
58+
it('generates html using new API', () => {
59+
const result = transform(`<!-- example(
60+
{
61+
"example": "exampleName",
62+
"file": "example-html.html",
63+
"region": "some-region"
64+
}
65+
) -->`);
66+
expectEqualIgnoreLeadingWhitespace(
67+
result,
68+
'<div class="docs-markdown"></div><div material-docs-example="exampleName"\n' +
69+
'file="example-html.html"\n' +
70+
'region="some-region"></div><div class="docs-markdown"></div>',
71+
);
72+
});
73+
74+
it('generates html using new API with no region', () => {
75+
const result = transform(`<!-- example(
76+
{
77+
"example": "exampleName",
78+
"file": "example-html.html"
79+
}
80+
) -->`);
81+
expectEqualIgnoreLeadingWhitespace(
82+
result,
83+
'<div class="docs-markdown"></div><div material-docs-example="exampleName"\n' +
84+
'file="example-html.html"\n' +
85+
'></div><div class="docs-markdown"></div>',
86+
);
87+
});
88+
89+
it('generates html using new API with no file and no region', () => {
90+
const result = transform(`<!-- example(
91+
{
92+
"example": "exampleName"
93+
}
94+
) -->`);
95+
expectEqualIgnoreLeadingWhitespace(
96+
result,
97+
'<div class="docs-markdown"></div>' +
98+
'<div material-docs-example="exampleName"\n></div>' +
99+
'<div class="docs-markdown"></div>',
100+
);
101+
});
102+
103+
it('generates html using old API', () => {
104+
expect(transform('<!-- example(name) -->')).toEqual(
105+
'<div class="docs-markdown"></div>' +
106+
'<div material-docs-example="name"></div>' +
107+
'<div class="docs-markdown"></div>',
108+
);
109+
});
110+
111+
it('does not allow id links with no matching id element', () => {
112+
spyOn(console, 'error');
113+
spyOn(process, 'exit');
114+
transform('[text](#does-not-exist)');
115+
expect((console.error as jasmine.Spy).calls.allArgs()).toEqual([
116+
[jasmine.stringMatching(/Could not process file: test\.html/)],
117+
[jasmine.stringMatching(/Found link to "does-not-exist"\. This heading does not exist/)],
118+
]);
119+
expect(process.exit).toHaveBeenCalledWith(1);
120+
});
121+
122+
function expectEqualIgnoreLeadingWhitespace(actual: string, expected: string) {
123+
expect(stripLeadingWhitespace(actual.trim())).toEqual(stripLeadingWhitespace(expected.trim()));
124+
}
125+
126+
function stripLeadingWhitespace(s: string) {
127+
return s.replace(/^\s*/gm, '');
128+
}
129+
});

tools/markdown-to-html/docs-marked-renderer.spec.ts

Lines changed: 0 additions & 133 deletions
This file was deleted.

0 commit comments

Comments
 (0)