This repository has been archived by the owner on Mar 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtransform.test.js
90 lines (75 loc) · 2.43 KB
/
transform.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
85
86
87
88
89
90
/** eslint-env browser,jest,node
*
* @jest-environment jsdom
*/
jest.mock('highlight.js');
const domTransform = require('metalsmith-dom-transform');
const highlight = require('highlight.js');
const {promisify} = require('util');
const transform = require('./transform');
test('passes options to highlight.js', () => {
const myOptions = {foo: 'bar'};
transform(myOptions);
expect(highlight.configure).toHaveBeenCalledWith(myOptions);
});
beforeEach(() => {
highlight.highlightBlock.mockReset();
highlight.highlightBlock.mockImplementation(block => {
block.classList.add('highlighted');
});
});
describe('highlights', () => {
let root;
let transformer;
beforeEach(() => {
transformer = promisify(transform());
root = document.createElement('main');
});
test('multiple <code> blocks', () => {
root.innerHTML = `<h1>Non-code text</h1>
<p>P with <code>inline</code> code.</p>
<pre><code>Code Block Within pre</code></pre>
<code>Naked code block</code>`;
return transformer(root, {}, {}).then(() => {
expect(root.outerHTML).toMatchSnapshot();
});
});
test('document fragment', () => {
const fragment = document.createDocumentFragment();
const code = document.createElement('code');
code.innerHTML = `UGLY && CODE`;
root.innerHTML = `<p>Some <code>inline</code> code.</p>`;
fragment.appendChild(code);
fragment.appendChild(root);
return transformer(fragment, {}, {}).then(() => {
expect(highlight.highlightBlock).toHaveBeenCalledTimes(2);
expect(code.outerHTML).toMatchSnapshot();
expect(root.outerHTML).toMatchSnapshot();
});
});
test('via custom selector', () => {
let transformer = promisify(transform({selector: 'pre > code'}));
root.innerHTML = `<h1>Non-code text</h1>
<p>P with <code>inline</code> code.</p>
<pre><code>Code Block Within pre</code></pre>
<code>Naked code block</code>`;
return transformer(root, {}, {}).then(() => {
expect(highlight.highlightBlock).toHaveBeenCalledTimes(1);
expect(root.outerHTML).toMatchSnapshot();
});
});
});
describe('via domTransform', () => {
let files;
let plugin;
beforeEach(() => {
files = {
'index.html': {contents: new Buffer('<p>Hello <code>code</code></p>')},
};
plugin = promisify(domTransform({transforms: [transform()]}));
return plugin(files, {});
});
test('runs', () => {
expect(files['index.html'].contents.toString()).toMatchSnapshot();
});
});