Skip to content

Commit d514ea4

Browse files
committed
Add unit tests
1 parent 39aef99 commit d514ea4

File tree

1 file changed

+126
-0
lines changed
  • packages/docusaurus-mdx-loader/src/remark/toc/__tests__

1 file changed

+126
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import {toHeadingHTMLValue} from '../utils';
9+
import type {Heading} from 'mdast';
10+
11+
describe('toHeadingHTMLValue', () => {
12+
async function convert(heading: Heading): Promise<string> {
13+
const {toString} = await import('mdast-util-to-string');
14+
return toHeadingHTMLValue(heading, toString);
15+
}
16+
17+
it('converts a simple heading', async () => {
18+
const heading: Heading = {
19+
type: 'heading',
20+
depth: 2,
21+
children: [
22+
{
23+
type: 'text',
24+
value: 'Some heading text',
25+
},
26+
],
27+
};
28+
29+
await expect(convert(heading)).resolves.toMatchInlineSnapshot(
30+
`"Some heading text"`,
31+
);
32+
});
33+
34+
it('converts a heading with b tag', async () => {
35+
const heading: Heading = {
36+
type: 'heading',
37+
depth: 2,
38+
children: [
39+
{
40+
type: 'mdxJsxTextElement',
41+
name: 'b',
42+
attributes: [],
43+
children: [
44+
{
45+
type: 'text',
46+
value: 'Some title',
47+
},
48+
],
49+
},
50+
],
51+
};
52+
53+
await expect(convert(heading)).resolves.toMatchInlineSnapshot(
54+
`"<b>Some title</b>"`,
55+
);
56+
});
57+
58+
it('converts a heading with span tag + className', async () => {
59+
const heading: Heading = {
60+
type: 'heading',
61+
depth: 2,
62+
children: [
63+
{
64+
type: 'mdxJsxTextElement',
65+
name: 'span',
66+
attributes: [
67+
{
68+
type: 'mdxJsxAttribute',
69+
name: 'className',
70+
value: 'my-class',
71+
},
72+
],
73+
children: [
74+
{
75+
type: 'text',
76+
value: 'Some title',
77+
},
78+
],
79+
},
80+
],
81+
};
82+
83+
await expect(convert(heading)).resolves.toMatchInlineSnapshot(
84+
`"<span class="my-class">Some title</span>"`,
85+
);
86+
});
87+
88+
it('converts a heading with image', async () => {
89+
const heading: Heading = {
90+
type: 'heading',
91+
depth: 2,
92+
children: [
93+
{
94+
type: 'mdxJsxTextElement',
95+
name: 'img',
96+
attributes: [
97+
{
98+
type: 'mdxJsxAttribute',
99+
name: 'src',
100+
value: '/img/slash-introducing.svg',
101+
},
102+
{
103+
type: 'mdxJsxAttribute',
104+
name: 'height',
105+
value: '32',
106+
},
107+
{
108+
type: 'mdxJsxAttribute',
109+
name: 'alt',
110+
value: 'test',
111+
},
112+
],
113+
children: [],
114+
},
115+
{
116+
type: 'text',
117+
value: ' Some title',
118+
},
119+
],
120+
};
121+
122+
await expect(convert(heading)).resolves.toMatchInlineSnapshot(
123+
`"<img></img> Some title"`,
124+
);
125+
});
126+
});

0 commit comments

Comments
 (0)