/
create-html.test.js
173 lines (154 loc) · 5.36 KB
/
create-html.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
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
const {
getHtmlLayout,
getUpdatedHtmlLayout,
getHtmlNav,
getHtmlTitleBody,
} = require('./create-html');
describe('Unit tests for getHtmlNav', () => {
test('Should create navbar with link to index page (no args)', () => {
expect(getHtmlNav()).toBe(
`<div><ul><li><a href='./index.html'>Home</a></li></ul></div>`
);
});
test('Should create navbar with link to index and user-defined pages (string arg)', () => {
expect(getHtmlNav('Silver Blaze.txt')).toBe(
`<div><ul><li><a href='./index.html'>Home</a></li><li><a href='./Silver Blaze.html'>Silver Blaze</a></li></ul></div>`
);
});
test('Should create navbar with link to index and user-defined pages (string array arg)', () => {
expect(
getHtmlNav([
'Silver Blaze.txt',
'The Adventure of the Six Napoleans.txt',
])
).toBe(
`<div><ul><li><a href='./index.html'>Home</a></li><li><a href='./Silver Blaze.html'>Silver Blaze</a></li><li><a href='./The Adventure of the Six Napoleans.html'>The Adventure of the Six Napoleans</a></li></ul></div>`
);
});
test('Should create navbar with link to index page (empty array arg)', () => {
expect(getHtmlNav([])).toBe(
`<div><ul><li><a href='./index.html'>Home</a></li></ul></div>`
);
});
});
const html = `<!DOCTYPE html>
<html lang="en-CA">
<head>
<link rel="stylesheet" href="{stylesheet}" />
<meta charset="utf-8" />
<title>{title}</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<div id="nav">{nav}</div>
<h1>{title}</h1>
{body}
</body>
</html>
`;
const expected = `<!DOCTYPE html>
<html lang="en-CA">
<head>
<link rel="stylesheet" href="style.css" />
<meta charset="utf-8" />
<title> </title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<div id="nav"> </div>
<h1> </h1>
</body>
</html>
`;
describe('Unit tests for getHtmlLayout', () => {
test('Should return default HTML template', () => {
expect(getHtmlLayout()).toBe(html);
});
});
describe('Unit tests for getUpdatedHtmlLayout', () => {
test('Should replace placeholders with blank space', () => {
expect(
getUpdatedHtmlLayout({
layout: html,
})
).toBe(expected);
});
test('Should replace stylesheet placeholder with stylesheet url', () => {
let result = getUpdatedHtmlLayout({
layout: html,
stylesheet:
'https://cdnjs.cloudflare.com/ajax/libs/tufte-css/1.8.0/tufte.min.css',
});
expect(
result.includes(
`<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tufte-css/1.8.0/tufte.min.css" />`
)
).toBeTruthy();
});
test('Should replace title placeholder with title text', () => {
let result = getUpdatedHtmlLayout({
layout: html,
title: 'Title',
});
expect(result.includes(`<h1>Title</h1>`)).toBeTruthy();
});
test('Should replace nav placeholder with nav html', () => {
let result = getUpdatedHtmlLayout({
layout: html,
nav: `<div><ul><li><a href='./index.html'>Home</a></li></ul></div>`,
});
expect(
result.includes(
`<div id="nav"><div><ul><li><a href='./index.html'>Home</a></li></ul></div></div>`
)
).toBeTruthy();
});
test('Should replace body placeholder with body text', () => {
let result = getUpdatedHtmlLayout({
layout: html,
body: '<p>Body Text</p>',
});
expect(
result.includes(`<body>
<div id="nav"> </div>
<h1> </h1>
<p>Body Text</p>
</body>`)
).toBeTruthy();
});
test('Should replace lang placeholder with lang', () => {
let result = getUpdatedHtmlLayout({
layout: html,
lang: 'fr-CA',
});
expect(result.includes(`<html lang="fr-CA">`)).toBeTruthy();
});
});
describe('Unit tests for getHtmlTitleBody', () => {
test('Should return an object of title and body for text file with title', () => {
const txtFile = `Sample Title\n\n\nThis is a sample text file.`;
const expected = {
title: 'Sample Title',
body:
'<h1>Sample Title</h1>\n' +
'<p> This is a sample text file.</p>\n',
};
expect(getHtmlTitleBody(txtFile, true)).toEqual(expected);
});
test('Should return an object of empty title and body for text file with no title', () => {
const txtFile = `This is a sample text file.`;
const expected = {
title: '',
body: '<p>This is a sample text file.</p>\n',
};
expect(getHtmlTitleBody(txtFile, true)).toEqual(expected);
});
test('Should return an object containing empty title and body for markdown file', () => {
const mdFile = `# This is a heading`;
const expected = {
title: '',
body: '<h1>This is a heading</h1>\n',
};
expect(getHtmlTitleBody(mdFile, false)).toEqual(expected);
});
});