Skip to content

Commit d3bf9c4

Browse files
committed
test(plugin): add tests for directive syntax
1 parent 0e55182 commit d3bf9c4

File tree

2 files changed

+195
-2
lines changed

2 files changed

+195
-2
lines changed

tests/src/__snapshots__/index.test.ts.snap

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,128 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`File extension - plugin Options: directiveSyntax should render both file markups 1`] = `
4+
"<p><a href="../file" download="file.txt" class="yfm-file"><span class="yfm-file__icon"></span>file.txt</a></p>
5+
<p><a class="yfm-file" href="path/to/video" download="video.mp4"><span class="yfm-file__icon"></span>video.mp4</a></p>
6+
"
7+
`;
8+
9+
exports[`File extension - plugin Options: directiveSyntax should render only new (directive) file markup 1`] = `
10+
"<p>{% file src="../file" name="file.txt" %}</p>
11+
<p><a class="yfm-file" href="path/to/video" download="video.mp4"><span class="yfm-file__icon"></span>video.mp4</a></p>
12+
"
13+
`;
14+
15+
exports[`File extension - plugin Options: directiveSyntax should render only old file markup 1`] = `
16+
"<p><a href="../file" download="file.txt" class="yfm-file"><span class="yfm-file__icon"></span>file.txt</a></p>
17+
<p>:file<a href="path/to/video">video.mp4</a></p>
18+
"
19+
`;
20+
21+
exports[`File extension - plugin dyrective syntax should add allowed attrs 1`] = `
22+
"<p><a class="yfm-file" href="../readme" download="readme.md" referrerpolicy="origin" rel="external" target="_blank" type="text/plain" hreflang="en"><span class="yfm-file__icon"></span>readme.md</a></p>
23+
"
24+
`;
25+
26+
exports[`File extension - plugin dyrective syntax should generate file token 1`] = `
27+
[
28+
Token {
29+
"attrs": null,
30+
"block": true,
31+
"children": null,
32+
"content": "",
33+
"hidden": false,
34+
"info": "",
35+
"level": 0,
36+
"map": [
37+
0,
38+
1,
39+
],
40+
"markup": "",
41+
"meta": null,
42+
"nesting": 1,
43+
"tag": "p",
44+
"type": "paragraph_open",
45+
},
46+
Token {
47+
"attrs": null,
48+
"block": true,
49+
"children": [
50+
Token {
51+
"attrs": [
52+
[
53+
"class",
54+
"yfm-file",
55+
],
56+
[
57+
"href",
58+
"path/to/video",
59+
],
60+
[
61+
"download",
62+
"video.mp4",
63+
],
64+
],
65+
"block": false,
66+
"children": null,
67+
"content": "video.mp4",
68+
"hidden": false,
69+
"info": "",
70+
"level": 0,
71+
"map": null,
72+
"markup": ":file",
73+
"meta": null,
74+
"nesting": 0,
75+
"tag": "",
76+
"type": "yfm_file",
77+
},
78+
],
79+
"content": ":file[video.mp4](path/to/video)",
80+
"hidden": false,
81+
"info": "",
82+
"level": 1,
83+
"map": [
84+
0,
85+
1,
86+
],
87+
"markup": "",
88+
"meta": null,
89+
"nesting": 0,
90+
"tag": "",
91+
"type": "inline",
92+
},
93+
Token {
94+
"attrs": null,
95+
"block": true,
96+
"children": null,
97+
"content": "",
98+
"hidden": false,
99+
"info": "",
100+
"level": 0,
101+
"map": null,
102+
"markup": "",
103+
"meta": null,
104+
"nesting": -1,
105+
"tag": "p",
106+
"type": "paragraph_close",
107+
},
108+
]
109+
`;
110+
111+
exports[`File extension - plugin dyrective syntax should not add attrs not from whitelist 1`] = `
112+
"<p><a class="yfm-file" href="a.md" download="a.md"><span class="yfm-file__icon"></span>a.md</a></p>
113+
"
114+
`;
115+
116+
exports[`File extension - plugin dyrective syntax should render file directive 1`] = `
117+
"<p><a class="yfm-file" href="path/to/video" download="video.mp4"><span class="yfm-file__icon"></span>video.mp4</a></p>
118+
"
119+
`;
120+
121+
exports[`File extension - plugin dyrective syntax should render file inside text 1`] = `
122+
"<p>before<a class="yfm-file" href="../../docs/readme.md" download="file.txt"><span class="yfm-file__icon"></span>file.txt</a>after</p>
123+
"
124+
`;
125+
3126
exports[`File extension - plugin should add extra attrs 1`] = `
4127
"<p><a href="../file" download="file.txt" class="yfm-file" data-yfm-file="yes"><span class="yfm-file__icon"></span>file.txt</a></p>
5128
"

tests/src/index.test.ts

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import MarkdownIt from 'markdown-it';
22
import transform from '@diplodoc/transform';
3+
import dd from 'ts-dedent';
34

45
import {type TransformOptions, transform as fileTransformer} from '../../src/plugin';
56

67
function html(markup: string, opts?: TransformOptions) {
78
return transform(markup, {
89
// override the default markdown-it-attrs delimiters,
910
// to make it easier to check html for non-valid file markup
10-
leftDelimiter: '[',
11-
rightDelimiter: ']',
11+
leftDelimiter: '[[',
12+
rightDelimiter: ']]',
1213
plugins: [fileTransformer({bundle: false, ...opts})],
1314
}).result.html;
1415
}
@@ -175,4 +176,73 @@ describe('File extension - plugin', () => {
175176
});
176177
expect(md.render('{% file src="../file" name="file.txt" %}')).toMatchSnapshot();
177178
});
179+
180+
describe('dyrective syntax', () => {
181+
it('should render file directive', () => {
182+
expect(
183+
html(':file[video.mp4](path/to/video)', {directiveSyntax: 'only'}),
184+
).toMatchSnapshot();
185+
});
186+
187+
it('should render file inside text', () => {
188+
expect(
189+
html('before:file[file.txt](../../docs/readme.md)after', {directiveSyntax: 'only'}),
190+
).toMatchSnapshot();
191+
});
192+
193+
it('should not render file without file url', () => {
194+
expect(html(':file[file.txt]', {directiveSyntax: 'only'})).toBe(
195+
`<p>:file[file.txt]</p>\n`,
196+
);
197+
});
198+
199+
it('should not render file without file name', () => {
200+
expect(html(':file(path/to/file)', {directiveSyntax: 'only'})).toBe(
201+
`<p>:file(path/to/file)</p>\n`,
202+
);
203+
});
204+
205+
it('should not add attrs not from whitelist', () => {
206+
expect(
207+
html(':file[a.md](a.md){data-list=1}', {directiveSyntax: 'only'}),
208+
).toMatchSnapshot();
209+
});
210+
211+
it('should add allowed attrs', () => {
212+
expect(
213+
html(
214+
':file[readme.md](../readme){referrerpolicy=origin rel=external target=\'_blank\' type="text/plain" hreflang=en}',
215+
{
216+
directiveSyntax: 'only',
217+
},
218+
),
219+
).toMatchSnapshot();
220+
});
221+
222+
it('should generate file token', () => {
223+
expect(
224+
tokens(':file[video.mp4](path/to/video)', {directiveSyntax: 'only'}),
225+
).toMatchSnapshot();
226+
});
227+
});
228+
229+
describe('Options: directiveSyntax', () => {
230+
const markup = dd`
231+
{% file src="../file" name="file.txt" %}
232+
233+
:file[video.mp4](path/to/video)
234+
`;
235+
236+
it('should render only old file markup', () => {
237+
expect(html(markup, {directiveSyntax: 'disabled'})).toMatchSnapshot();
238+
});
239+
240+
it('should render only new (directive) file markup', () => {
241+
expect(html(markup, {directiveSyntax: 'only'})).toMatchSnapshot();
242+
});
243+
244+
it('should render both file markups', () => {
245+
expect(html(markup, {directiveSyntax: 'enabled'})).toMatchSnapshot();
246+
});
247+
});
178248
});

0 commit comments

Comments
 (0)