-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathindex.test.js
More file actions
449 lines (376 loc) · 11.9 KB
/
index.test.js
File metadata and controls
449 lines (376 loc) · 11.9 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
const { cleanup, render, screen } = require('@testing-library/react');
const React = require('react');
const BaseUrlContext = require('../contexts/BaseUrl');
const markdown = require('../index');
const { tableFlattening } = require('../processor/plugin/table-flattening');
const { options } = require('../options');
test('it should have the proper utils exports', () => {
expect(typeof markdown.utils.BaseUrlContext).toBe('object');
expect(typeof markdown.utils.GlossaryContext).toBe('object');
expect(typeof markdown.utils.VariablesContext).toBe('object');
expect(markdown.utils.options).toStrictEqual({
alwaysThrow: false,
compatibilityMode: false,
copyButtons: true,
correctnewlines: false,
markdownOptions: {
fences: true,
commonmark: true,
gfm: true,
ruleSpaces: false,
listItemIndent: '1',
spacedTable: true,
paddedTable: true,
},
normalize: true,
safeMode: false,
settings: { position: false },
theme: 'light',
});
});
test('image', () => {
const { container } = render(markdown.default(''));
expect(container.innerHTML).toMatchSnapshot();
});
test('heading', () => {
const { container } = render(markdown.default('## Example Header'));
expect(container.innerHTML).toMatchSnapshot();
});
test('magic image', () => {
const { container } = render(
markdown.default(
`
[block:image]
{
"images": [
{
"image": [
"https://files.readme.io/6f52e22-man-eating-pizza-and-making-an-ok-gesture.jpg",
"man-eating-pizza-and-making-an-ok-gesture.jpg",
"",
1024,
682,
"#d1c8c5"
],
"caption": "A guy. Eating pizza. And making an OK gesture.",
"sizing": "full"
}
]
}
[/block]
`,
options
)
);
expect(container.innerHTML).toMatchSnapshot();
});
test('list items', () => {
const { container } = render(markdown.default('- listitem1'));
expect(container.innerHTML).toMatchSnapshot();
});
test('check list items', () => {
const { container } = render(markdown.default('- [ ] checklistitem1\n- [x] checklistitem1'));
expect(container.innerHTML).toMatchSnapshot();
});
test('gemoji generation', () => {
const { container } = render(markdown.default(':sparkles:'));
expect(container.querySelector('.lightbox')).not.toBeInTheDocument();
});
test('should strip out inputs', () => {
const { container } = render(markdown.default('<input type="text" value="value" />'));
expect(container).toBeEmptyDOMElement();
});
test('tables', () => {
const { container } = render(
markdown.default(`| Tables | Are | Cool |
| ------------- |:-------------:| -----:|
| col 3 is | right-aligned | $1600 |
| col 2 is | centered | $12 |
| zebra stripes | are neat | $1 |
`)
);
expect(container.innerHTML.trim()).toMatchSnapshot();
});
test('headings', () => {
render(
markdown.default(`# Heading 1
## Heading 2
### Heading 3
#### Heading 4
##### Heading 5
###### Heading 6`)
);
expect(screen.getAllByRole('heading')).toHaveLength(6);
});
test('anchors', () => {
const { container } = render(
markdown.default(`
[link](http://example.com)
[xss](javascript:alert)
[doc](doc:slug)
[ref](ref:slug)
[blog](blog:slug)
[changelog](changelog:slug)
[page](page:slug)
`)
);
expect(container.innerHTML).toMatchSnapshot();
});
test('anchor target: should default to _self', () => {
const { container } = render(markdown.default('[test](https://example.com)'));
expect(container.innerHTML).toMatchSnapshot();
});
test('anchor target: should allow _blank if using HTML', () => {
const { container } = render(markdown.default('<a href="https://example.com" target="_blank">test</a>'));
expect(container.innerHTML).toMatchSnapshot();
});
test('anchor target: should allow download if using HTML', () => {
const { container } = render(markdown.default('<a download="example.png" href="" target="_blank">test</a>'));
expect(container.innerHTML).toMatchSnapshot();
});
test('anchors with baseUrl', () => {
const { container } = render(
React.createElement(
BaseUrlContext.Provider,
{
value: '/child/v1.0',
},
markdown.html(
`
[doc](doc:slug)
[ref](ref:slug)
[blog](blog:slug)
[changelog](changelog:slug)
[page](page:slug)
`
)
)
);
expect(container.innerHTML).toMatchSnapshot();
});
test('anchors with baseUrl and special characters in url hash', () => {
const { container } = render(markdown.default('[ref](ref:slug#整)'));
expect(container.innerHTML).toMatchSnapshot();
});
test('emojis', () => {
const { container } = render(
markdown.default(`
:joy:
:fa-lock:
:unknown-emoji:
`)
);
expect(container.innerHTML).toMatchSnapshot();
});
describe('code samples', () => {
it('should codify code', () => {
const { container } = render(
markdown.default(`
\`\`\`javascript
var a = 1;
\`\`\`
\`\`\`
code-without-language
\`\`\`
`)
);
expect(container.querySelectorAll('pre')).toHaveLength(2);
expect(container.querySelectorAll('button')).toHaveLength(2);
});
describe('`copyButtons` option', () => {
it('should not insert the CopyCode component if `copyButtons=false`', () => {
render(
markdown.react('This is a sentence and it contains a piece of `code` wrapped in backticks.', {
copyButtons: false,
})
);
expect(screen.queryByRole('button')).not.toBeInTheDocument();
});
});
it('should parse indented code on the first line', () => {
const md = ' const code = true;';
expect(markdown.mdast(md)).toMatchSnapshot();
});
});
test('should render nothing if nothing passed in', () => {
expect(markdown.html('')).toBeNull();
});
test('`correctnewlines` option', () => {
let { container } = render(markdown.react('test\ntest\ntest', { correctnewlines: true }));
expect(container).toContainHTML('<p>test\ntest\ntest</p>');
cleanup();
({ container } = render(markdown.react('test\ntest\ntest', { correctnewlines: false })));
expect(container).toContainHTML('<p>test<br>\ntest<br>\ntest</p>');
});
describe('`alwaysThrow` option', () => {
it('should throw if `alwaysThrow` is true and magic block has invalid JSON', () => {
const shouldThrow = () =>
render(
markdown.default(
`[block:api-header]
{,
"title": "Uh-oh, I'm invalid",
"level": 2
}
[/block]`,
{ alwaysThrow: true }
)
);
expect(() => shouldThrow()).toThrow('Invalid Magic Block JSON');
});
it('should not throw if `alwaysThrow` is true but magic block has valid JSON', () => {
const shouldThrow = () =>
render(
markdown.default(
`[block:api-header]
{
"title": "Ooh I'm valid 💅",
"level": 2
}
[/block]`,
{ alwaysThrow: true }
)
);
expect(() => shouldThrow()).not.toThrow('Invalid Magic Block JSON');
});
});
// TODO not sure if this needs to work or not?
// Isn't it a good thing to always strip HTML?
describe('`stripHtml` option', () => {
it('should allow html by default', () => {
expect(markdown.html('<p>Test</p>')).toBe('<p>Test</p>');
expect(markdown.html('<p>Test</p>', { stripHtml: false })).toBe('<p>Test</p>');
});
it.skip('should escape unknown tags', () => {
expect(markdown.html('<unknown-tag>Test</unknown-tag>')).toBe('<p><unknown-tag>Test</unknown-tag></p>');
});
it('should allow certain attributes', () => {
expect(markdown.html('<p id="test">Test</p>')).toBe('<p id="test">Test</p>');
});
it('should strip unknown attributes', () => {
expect(markdown.html('<p unknown="test">Test</p>')).toBe('<p>Test</p>');
});
it.skip('should escape everything if `stripHtml=true`', () => {
expect(markdown.html('<p>Test</p>', { stripHtml: true })).toBe('<p><p>Test</p></p>\n');
});
});
test('should strip dangerous iframe tag', () => {
const { container } = render(markdown.react('<p><iframe src="javascript:alert(\'delta\')"></iframe></p>'));
expect(container).toContainHTML('<p></p>');
});
test('should strip dangerous img attributes', () => {
const { container } = render(markdown.default('<img src="x" onerror="alert(\'charlie\')">'));
expect(container).toContainHTML(
'<span aria-label="" class="img" role="button" tabindex="0"><img src="x" align="" alt="" caption="" height="auto" title="" width="auto" loading="lazy"><span class="lightbox" role="dialog" tabindex="0"><span class="lightbox-inner"><img src="x" align="" caption="" height="auto" title="Click to close..." width="auto" alt="" class="lightbox-img" loading="lazy"></span></span></span>'
);
});
describe('tree flattening', () => {
it('should bring nested mdast data up to the top child level', () => {
const text = `
| | Col. B |
|:-------:|:-------:|
| Cell A1 | Cell B1 |
| Cell A2 | Cell B2 |
| Cell A3 | |
`;
const hast = markdown.hast(text);
const table = hast.children[1];
expect(table.children).toHaveLength(2);
expect(table.children[0].value).toBe('Col. B');
expect(table.children[1].value).toBe('Cell A1 Cell B1 Cell A2 Cell B2 Cell A3');
});
it('should not throw an error if missing values', () => {
const tree = {
tagName: 'table',
children: [
{
tagName: 'tHead',
},
{
tagName: 'tBody',
},
],
};
const [head, body] = tableFlattening(tree).children;
expect(head.value).toBe('');
expect(body.value).toBe('');
});
});
describe('export multiple Markdown renderers', () => {
const text = `# Hello World
| Col. A | Col. B | Col. C |
|:-------:|:-------:|:-------:|
| Cell A1 | Cell B1 | Cell C1 |
| Cell A2 | Cell B2 | Cell C2 |
| Cell A3 | Cell B3 | Cell C3 |
[Embed Title](https://jsfiddle.net/rafegoldberg/5VA5j/ "@embed")
> ❗️ UhOh
>
> Lorem ipsum dolor sit amet consectetur adipisicing elit.
`;
const tree = {
type: 'root',
children: [
{
type: 'heading',
depth: 1,
children: [
{
type: 'text',
value: 'Hello World',
},
],
},
],
};
it('renders plain markdown as React', () => {
expect(markdown.plain(text)).toMatchSnapshot();
});
it('renders custom React components', () => {
expect(markdown.react(text)).toMatchSnapshot();
});
it('renders hAST', () => {
expect(markdown.hast(text)).toMatchSnapshot();
});
it('renders mdAST', () => {
expect(markdown.mdast(text)).toMatchSnapshot();
});
it('renders MD', () => {
expect(markdown.md(tree)).toMatchSnapshot();
});
it('renders plainText from AST', () => {
expect(markdown.astToPlainText(tree)).toMatchSnapshot();
});
it('astToPlainText should return an empty string if no value', () => {
expect(markdown.astToPlainText()).toBe('');
});
it('allows complex compact headings', () => {
const mdxt = `#Basic Text
##🙀 oh noes!
###**6**. Oh No
Lorem ipsum dolor!`;
const html = markdown.html(mdxt);
expect(html).toMatchSnapshot();
});
it('renders HTML', () => {
expect(markdown.html(text)).toMatchSnapshot();
});
it('returns null for blank input', () => {
expect(markdown.html('')).toBeNull();
expect(markdown.plain('')).toBeNull();
expect(markdown.react('')).toBeNull();
expect(markdown.hast('')).toBeNull();
expect(markdown.mdast('')).toBeNull();
expect(markdown.md('')).toBeNull();
});
});
describe('prefix anchors with "section-"', () => {
it('should add a section- prefix to heading anchors', () => {
expect(markdown.html('# heading')).toMatchSnapshot();
});
it('"section-" anchors should split on camelCased words', () => {
const { container } = render(markdown.react('# camelCased'));
const anchor = container.querySelectorAll('.heading-anchor_backwardsCompatibility')[0];
expect(anchor).toHaveAttribute('id', 'section-camel-cased');
});
});