-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeBlockHandler.test.ts
More file actions
135 lines (117 loc) · 3.62 KB
/
Copy pathcodeBlockHandler.test.ts
File metadata and controls
135 lines (117 loc) · 3.62 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { describe, it, expect } from 'vitest'
import { parseMdxToBlocks } from './mdxBlockParser'
import { MdxParserError, ParserErrorCode } from './parserErrors'
// Side-effect import: registers CodeBlock handler
import './codeBlockHandler'
const ctx = { locale: 'en' }
describe('CodeBlock handler', () => {
it('parses language, title, and template literal code', async () => {
const blocks = await parseMdxToBlocks(
'<CodeBlock language="html" title="test" code={`qwe`} />',
ctx
)
expect(blocks).toEqual([
{
__component: 'blocks.code-block',
code: 'qwe',
language: 'html',
title: 'test'
}
])
})
it('parses multiline code from a template literal', async () => {
const blocks = await parseMdxToBlocks(
`<CodeBlock language="python" title="hello.py" code={\`def hello():
print("world")\`} />`,
ctx
)
expect(blocks[0]).toMatchObject({
__component: 'blocks.code-block',
language: 'python',
title: 'hello.py',
code: 'def hello():\n print("world")'
})
})
it('preserves source indentation as-is', async () => {
const blocks = await parseMdxToBlocks(
`<CodeBlock language="javascript" code={\`function run() {
if (true) {
return 1
}
}\`} />`,
ctx
)
expect((blocks[0] as { code: string }).code).toBe(
'function run() {\n if (true) {\n return 1\n }\n}'
)
})
it('omits title when not provided', async () => {
const blocks = await parseMdxToBlocks(
'<CodeBlock language="javascript" code={`const x = 1`} />',
ctx
)
expect(blocks[0]).toEqual({
__component: 'blocks.code-block',
code: 'const x = 1',
language: 'javascript'
})
})
it('preserves markdown order around CodeBlock', async () => {
const blocks = await parseMdxToBlocks(
`Intro paragraph.
<CodeBlock language="bash" code={\`echo hi\`} />
Outro paragraph.`,
ctx
)
expect(blocks).toHaveLength(3)
expect(blocks[0]).toMatchObject({ __component: 'blocks.paragraph' })
expect(blocks[1]).toMatchObject({
__component: 'blocks.code-block',
code: 'echo hi',
language: 'bash'
})
expect(blocks[2]).toMatchObject({ __component: 'blocks.paragraph' })
})
})
describe('CodeBlock handler — errors', () => {
it('returns MISSING_REQUIRED_PROP when language is missing', async () => {
const result = await parseMdxToBlocks(
'<CodeBlock code={`const x = 1`} />',
ctx
)
expect(result).toBeInstanceOf(MdxParserError)
expect(result).toMatchObject({
code: ParserErrorCode.MISSING_REQUIRED_PROP
})
})
it('returns MISSING_REQUIRED_PROP when code is missing', async () => {
const result = await parseMdxToBlocks(
'<CodeBlock language="javascript" />',
ctx
)
expect(result).toBeInstanceOf(MdxParserError)
expect(result).toMatchObject({
code: ParserErrorCode.MISSING_REQUIRED_PROP
})
})
it('returns INVALID_PROP_VALUE for unsupported language', async () => {
const result = await parseMdxToBlocks(
'<CodeBlock language="cobol" code={`x`} />',
ctx
)
expect(result).toBeInstanceOf(MdxParserError)
expect(result).toMatchObject({
code: ParserErrorCode.INVALID_PROP_VALUE
})
})
it('returns DYNAMIC_EXPRESSION for template interpolation', async () => {
const result = await parseMdxToBlocks(
'<CodeBlock language="javascript" code={`hello ${name}`} />',
ctx
)
expect(result).toBeInstanceOf(MdxParserError)
expect(result).toMatchObject({
code: ParserErrorCode.DYNAMIC_EXPRESSION
})
})
})