-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathhighlighter.test.mjs
More file actions
75 lines (61 loc) · 2.23 KB
/
highlighter.test.mjs
File metadata and controls
75 lines (61 loc) · 2.23 KB
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
import assert from 'node:assert/strict';
import { describe, it, mock } from 'node:test';
// Mock dependencies
const mockShiki = {
codeToHtml: mock.fn(() => '<pre><code>highlighted code</code></pre>'),
codeToHast: mock.fn(() => ({ type: 'element', tagName: 'pre' })),
};
mock.module('@shikijs/core', {
namedExports: { createHighlighterCoreSync: () => mockShiki },
});
mock.module('@shikijs/engine-javascript', {
namedExports: { createJavaScriptRegexEngine: () => ({}) },
});
mock.module('shiki/themes/nord.mjs', {
defaultExport: { name: 'nord', colors: { 'editor.background': '#2e3440' } },
});
describe('createHighlighter', async () => {
const { createHighlighter } = await import('../highlighter.mjs');
describe('getLanguageDisplayName', () => {
it('returns display name for known languages', () => {
const langs = [
{ name: 'javascript', displayName: 'JavaScript', aliases: ['js'] },
];
const highlighter = createHighlighter({ langs });
assert.strictEqual(
highlighter.getLanguageDisplayName('javascript'),
'JavaScript'
);
assert.strictEqual(
highlighter.getLanguageDisplayName('js'),
'JavaScript'
);
});
it('returns original name for unknown languages', () => {
const highlighter = createHighlighter({ langs: [] });
assert.strictEqual(
highlighter.getLanguageDisplayName('unknown'),
'unknown'
);
});
});
describe('highlightToHtml', () => {
it('extracts inner HTML from code tag', () => {
mockShiki.codeToHtml.mock.mockImplementationOnce(
() => '<pre><code>const x = 1;</code></pre>'
);
const highlighter = createHighlighter({});
const result = highlighter.highlightToHtml('const x = 1;', 'javascript');
assert.strictEqual(result, 'const x = 1;');
});
});
describe('highlightToHast', () => {
it('returns HAST tree from shiki', () => {
const expectedHast = { type: 'element', tagName: 'pre' };
mockShiki.codeToHast.mock.mockImplementationOnce(() => expectedHast);
const highlighter = createHighlighter({});
const result = highlighter.highlightToHast('code', 'javascript');
assert.deepStrictEqual(result, expectedHast);
});
});
});