forked from simonhaenisch/md-to-pdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
116 lines (88 loc) · 3.75 KB
/
test.js
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
const path = require('path');
const test = require('ava');
const config = require('./util/config');
const getHtml = require('./util/get-html');
const getMarkedWithHighlighter = require('./util/get-marked-with-highlighter');
const getPdfFilePath = require('./util/get-pdf-file-path');
const { getDir, getMarginObject } = require('./util/helpers');
const readFile = require('./util/read-file');
const isMdFile = require('./util/is-md-file');
const getMdFilesInDir = require('./util/get-md-files-in-dir');
// --
// get-html
test('getHtml should return a valid html document', t => {
const html = getHtml('', config).replace(/\n/g, '');
t.regex(html, /<!DOCTYPE html>.*<html>.*<head>.*<body class="">.*<\/body>.*<\/html>/);
});
test('getHtml should inject rendered markdown', t => {
const html = getHtml('# Foo', config).replace(/\n/g, '');
t.regex(html, /<body class=""><h1 id="foo">Foo<\/h1>.*<\/body>/);
});
test('getHtml should inject body classes', t => {
const html = getHtml('', { ...config, body_class: ['foo', 'bar'] }).replace(/\n/g, '');
t.regex(html, /<body class="foo bar">/);
});
// --
// get-marked-with-highlighter
test('getMarkedWithHighlighter should highlight js code', t => {
const marked = getMarkedWithHighlighter({});
const html = marked('```js\nvar foo="bar";\n```');
t.true(html.includes('<code class="hljs js">'));
});
test('getMarkedWithHighlighter should highlight unknown code as plaintext', t => {
const marked = getMarkedWithHighlighter({});
const html = marked('```\nvar foo="bar";\n```');
t.true(html.includes('<code class="hljs plaintext">'));
});
// --
// get-pdf-file-path
test('getPdfFilePath should return the same path but with .pdf extension', t => {
const mdFilePath = path.posix.join('/', 'var', 'foo', 'bar.md');
t.is(getPdfFilePath(mdFilePath), '/var/foo/bar.pdf');
});
// --
// helpers
test('getDir should get the directory the given file is in', t => {
const filePath = path.posix.join('/', 'var', 'foo', 'bar.txt');
t.is(getDir(filePath), '/var/foo');
});
test('getMarginObject should be able to handle all valid CSS margin inputs', t => {
t.deepEqual(getMarginObject('1em'), { top: '1em', right: '1em', bottom: '1em', left: '1em' });
t.deepEqual(getMarginObject('1px 2px'), { top: '1px', right: '2px', bottom: '1px', left: '2px' });
t.deepEqual(getMarginObject('1mm 2mm 3mm'), { top: '1mm', right: '2mm', bottom: '3mm', left: '2mm' });
t.deepEqual(getMarginObject('1in 2in 3in 4in'), { top: '1in', right: '2in', bottom: '3in', left: '4in' });
t.is(getMarginObject(''), null);
t.throws(() => getMarginObject(null));
t.throws(() => getMarginObject({}));
t.throws(() => getMarginObject(0));
t.throws(() => getMarginObject('1em 2em 3em 4em 5em'));
});
// --
// read-file
test('readFile should return the content of a file', async t => {
const gitignoreContent = '.vscode\n.nyc_output\ncoverage\n';
t.is(await readFile('.gitignore'), gitignoreContent);
t.is(await readFile('.gitignore', 'windows1252'), gitignoreContent);
});
// --
// is-md-file
test('isMdFile should return true if the file extension indicates a markdown file', t => {
t.is(isMdFile('md.txt'), false);
t.is(isMdFile('.md.txt'), true);
t.is(isMdFile('test.txt'), false);
t.is(isMdFile('test.md'), true);
t.is(isMdFile('test.md.notmd'), false);
t.is(isMdFile('test.md.txt'), true);
t.is(isMdFile('test.mkd'), true);
t.is(isMdFile('test.mkd.txt'), true);
t.is(isMdFile('test.mdown'), true);
t.is(isMdFile('test.mdown.txt'), true);
t.is(isMdFile('test.markdown'), true);
t.is(isMdFile('test.markdown.txt'), true);
});
// --
// get-md-files-in-dir
test('getMdFilesInDir should return the list of markdown files in a directory', async t => {
t.deepEqual(await getMdFilesInDir('./util'), []);
t.deepEqual(await getMdFilesInDir('.'), ['changelog.md', 'readme.md']);
});