-
Notifications
You must be signed in to change notification settings - Fork 10
/
test.js
84 lines (74 loc) · 2.23 KB
/
test.js
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
'use strict';
const rehype = require('rehype');
const rehypeParse = require('rehype-parse');
const unified = require('unified');
const dedent = require('dedent');
const rehypePrism = require('./index');
const processHtml = (html, options) => {
return rehype()
.data('settings', { fragment: true })
.use(rehypePrism, options)
.processSync(html)
.toString();
};
test('copies the language- class to pre tag', () => {
const result = processHtml(dedent`
<pre><code class="language-css"></code></pre>
`);
expect(result).toMatchSnapshot();
});
test('finds code and highlights', () => {
const result = processHtml(dedent`
<div>
<p>foo</p>
<pre><code class="language-css">p { color: red }</code></pre>
</div>
`);
expect(result).toMatchSnapshot();
});
test('handles newlines in code correctly', () => {
const html = dedent`
<div>
<p>foo</p>
<pre><code class="language-typescript{2}">interface Thing {
a: number
}
</code>
</pre>
</div>
`;
const stringResult = processHtml(html);
expect(stringResult).toMatchSnapshot();
const parsedAst = unified().use(rehypeParse, { fragment: true }).parse(html);
const transformedAst = unified().use(rehypePrism).run(parsedAst);
expect(transformedAst).resolves.toMatchSnapshot();
});
test('handles uppercase languages correctly', () => {
const result = processHtml(dedent`
<div>
<p>foo</p>
<pre><code class="language-CSS">p { color: red }</code></pre>
</div>
`);
expect(result).toMatchSnapshot();
});
test('does nothing to code block without language- class', () => {
const result = processHtml(dedent`
<pre><code>p { color: red }</code></pre>
`);
expect(result).toMatchSnapshot();
});
test('throw error with fake language- class', () => {
expect(() => {
processHtml(dedent`
<pre><code class="language-thisisnotalanguage">p { color: red }</code></pre>
`);
}).toThrow(/Unknown language/);
});
test('with options.ignoreMissing, does nothing to code block with fake language- class', () => {
const html = dedent`
<pre><code class="language-thisisnotalanguage">p { color: red }</code></pre>
`;
const result = processHtml(html, { ignoreMissing: true });
expect(result).toMatchSnapshot();
});