change html linter, export as test helper
[squeep-html-template-helper] / test / lib / template-helper.js
1 /* eslint-env mocha */
2 'use strict';
3
4 const assert = require('assert');
5 const th = require('../../lib/template-helper');
6 const stubLogger = require('../stub-logger');
7 const LintHtml = require('../lint-html');
8
9 const lintHtml = new LintHtml(stubLogger);
10
11 describe('Template Helper', function () {
12 let ctx, options, pagePathLevel;
13
14 beforeEach(function () {
15 pagePathLevel = 2;
16 ctx = {};
17 options = {
18 pageTitle: 'Test Page',
19 };
20 });
21
22 describe('initContext', function () {
23 it('covers', function () {
24 th.initContext(ctx);
25 assert(ctx.errors);
26 assert(ctx.notifications);
27 assert(Array.isArray(ctx.errors));
28 assert(Array.isArray(ctx.notifications));
29 });
30 }); // initContext
31
32 describe('dateOrNot', function () {
33 let date, otherwise;
34 beforeEach(function () {
35 date = new Date();
36 otherwise = 'otherwise';
37 });
38 it('covers', function () {
39 const result = th.dateOrNot(date, otherwise);
40 assert.strictEqual(result, date.toString());
41 });
42 it('covers no date', function () {
43 date = undefined;
44 const result = th.dateOrNot(date, otherwise);
45 assert.strictEqual(result, otherwise);
46 });
47 it('covers ms', function () {
48 const result = th.dateOrNot(date.getTime(), otherwise);
49 assert.strictEqual(result, date.toString());
50 });
51 it('covers naught', function () {
52 const result = th.dateOrNot(0, otherwise);
53 assert.strictEqual(result, otherwise);
54 });
55 it('covers the infinite', function () {
56 const result = th.dateOrNot(-Infinity, otherwise);
57 assert.strictEqual(result, otherwise);
58 });
59 }); // dateOrNot
60
61 describe('dateFormat', function () {
62 it('renders otherwise', function () {
63 const expected = 'otherwise';
64 const result = th.dateFormat(undefined, undefined, undefined, expected);
65 assert.strictEqual(result, expected);
66 });
67 it('handles invalid date', function () {
68 const expected = 'otherwise';
69 const result = th.dateFormat(new Date('bad date'), undefined, undefined, expected);
70 assert.strictEqual(result, expected);
71 });
72 it('renders Infinity', function () {
73 const expected = 'end of time';
74 const result = th.dateFormat(Infinity, expected);
75 assert.strictEqual(result, expected);
76 });
77 it('renders -Infinity', function () {
78 const expected = 'beginning of time';
79 const result = th.dateFormat(-Infinity, undefined, expected);
80 assert.strictEqual(result, expected);
81 });
82 it('renders a Date', function () {
83 const expected = 'Mar 27, 2022, 3:28:05 PM PDT';
84 const result = th.dateFormat(new Date('2022-03-27T22:28:05.049Z'));
85 assert.strictEqual(result, expected);
86 });
87 it('renders a timestamp', function () {
88 const expected = 'Mar 27, 2022, 3:28:05 PM PDT';
89 const result = th.dateFormat(1648420085049);
90 assert.strictEqual(result, expected);
91 });
92 }); // dateFormat
93
94 describe('timeElement', function () {
95 it('renders a date', function () {
96 const when = new Date('2022-09-11T21:17:56.872Z');
97 const expected = '<time datetime="2022-09-11T21:17:56.872Z">Sep 11, 2022, 2:17:56 PM PDT</time>';
98 const result = th.timeElement(when);
99 assert.strictEqual(result, expected);
100 });
101 it('covers title', function () {
102 const when = new Date('2022-09-11T21:17:56.872Z');
103 const expected = '<time title="a date" datetime="2022-09-11T21:17:56.872Z">Sep 11, 2022, 2:17:56 PM PDT</time>';
104 const result = th.timeElement(when, { title: 'a date' });
105 assert.strictEqual(result, expected);
106 });
107 it('covers other', function () {
108 const expected = '<time>Mar 27, 2022, 3:28:05 PM PDT</time>';
109 const result = th.timeElement(1648420085049);
110 assert.strictEqual(result, expected);
111 });
112 }); // timeElement
113
114 describe('secondsToPeriod', function () {
115 it('covers seconds', function () {
116 const result = th.secondsToPeriod(45);
117 assert.strictEqual(result, '45 seconds');
118 });
119 it('covers minutes', function () {
120 const result = th.secondsToPeriod(105);
121 assert.strictEqual(result, '1 minute 45 seconds');
122 });
123 it('covers hours', function () {
124 const result = th.secondsToPeriod(3705);
125 assert.strictEqual(result, '1 hour 1 minute 45 seconds');
126 });
127 it('covers days', function () {
128 const result = th.secondsToPeriod(90105);
129 assert.strictEqual(result, '1 day 1 hour 1 minute 45 seconds');
130 });
131 it('covers months', function () {
132 const result = th.secondsToPeriod(5274105);
133 assert.strictEqual(result, '2 months 1 day 1 hour 1 minute 45 seconds');
134 });
135 }); // secondsToPeriod
136
137 describe('htmlHead', function () {
138 it('covers', function () {
139 const result = th.htmlHead(pagePathLevel, ctx, options);
140 assert(result);
141 });
142 it('covers elements', function () {
143 options.headElements = [ '<div>foop</div>', '<div>poof</div>' ];
144 const result = th.htmlHead(pagePathLevel, ctx, options);
145 assert(result);
146 });
147 it('covers onLoad', function () {
148 options.onload = 'onLoadFn';
149 const result = th.htmlHead(pagePathLevel, ctx, options);
150 assert(result);
151 });
152 }); // htmlHead
153
154 describe('renderNavLink', function () {
155 let nav;
156 beforeEach(function () {
157 nav = {
158 href: 'https://example.com/',
159 text: 'example',
160 };
161 });
162 it('covers no class', function () {
163 const result = th.renderNavLink(nav);
164 assert(result);
165 });
166 it('covers class', function () {
167 nav.class = 'foo bar';
168 const result = th.renderNavLink(nav);
169 assert(result);
170 });
171 }); // renderNavLink
172
173 describe('OL', function () {
174 it('covers', function () {
175 const result = th.OL(['item', 'another item'], 1, { class: 'class' }, (item) => ({ class: `${item}-class` }));
176 assert(result);
177 });
178 it('covers defaults', function () {
179 const result = th.OL([]);
180 assert(result);
181 });
182 }); // OL
183
184 describe('UL', function () {
185 it('covers', function () {
186 const result = th.UL(['item', 'another item'], 1, { class: 'class' }, (item) => ({ class: `${item}-class` }));
187 assert(result);
188 });
189 it('covers defaults', function () {
190 const result = th.UL([]);
191 assert(result);
192 });
193 }); // UL
194
195 describe('LI', function () {
196 it('covers defaults', function () {
197 const result = th.LI('item');
198 assert(result);
199 });
200 }); // LI
201
202 describe('indented', function () {
203 it('covers', function () {
204 const result = th.indented(2, ['foo', 'bar']);
205 result.forEach((r) => assert(r.startsWith('\t\t')));
206 });
207 }); // indented
208
209 describe('htmlBody', function () {
210 it('covers no main', function () {
211 const result = th.htmlBody(pagePathLevel, ctx, options);
212 assert(result);
213 });
214 }); // htmlBody
215
216 describe('htmlHeader', function () {
217 it('covers no links', function () {
218 const result = th.htmlHeader(pagePathLevel, ctx, options);
219 assert(result);
220 });
221 it('covers links and logo', function () {
222 options.navLinks = [
223 {
224 href: 'https://exmaple.com/',
225 text: 'example',
226 },
227 ];
228 options.logoUrl = '/static/logo.svg';
229 const result = th.htmlHeader(pagePathLevel, ctx, options);
230 assert(result);
231 });
232 }); // htmlHeader
233
234 describe('htmlFooter', function () {
235 it('covers', function () {
236 options.footerEntries = [
237 '<div>foop</div>',
238 '<div>blah</div>',
239 ]
240 const result = th.htmlFooter(ctx, options);
241 assert(result);
242 });
243 it('covers default', function () {
244 const result = th.htmlFooter(ctx, options);
245 assert(!result);
246 });
247 }); // htmlFooter
248
249 describe('htmlMessages', function () {
250 it('covers', function () {
251 ctx.errors = ['an error'];
252 ctx.notifications = ['a notification'];
253 options.errorHeading = 'Errors';
254 options.notificationHeading = 'Notices';
255 options.errorContent = ['<p>Message about errors.</p>'];
256 options.notificationContent = ['<p>Message about notifications.</p>'];
257 const result = th.htmlMessages(ctx, options);
258 assert(result);
259 });
260 }); // htmlMessages
261
262 describe('htmlPage', function () {
263 let main;
264 beforeEach(function () {
265 main = [];
266 });
267 it('covers', async function () {
268 const result = th.htmlPage(pagePathLevel, ctx, options, main);
269 await lintHtml.lint(result);
270 assert(result);
271 });
272 it('covers defaults', async function () {
273 const result = th.htmlPage(pagePathLevel, ctx, options, main);
274 await lintHtml.lint(result);
275 assert(result);
276 });
277 it('covers user', async function () {
278 ctx.session = {
279 authenticatedProfile: 'https://user.example.com/',
280 };
281 const result = th.htmlPage(pagePathLevel, ctx, options, main);
282 await lintHtml.lint(result);
283 assert(result);
284 });
285 it('covers user at root path', async function () {
286 ctx.session = {
287 authenticatedIdentifier: 'user',
288 };
289 pagePathLevel = 0;
290 const result = th.htmlPage(pagePathLevel, ctx, options, main);
291 await lintHtml.lint(result);
292 assert(result);
293 });
294 it('covers logout redirect', async function () {
295 ctx.session = {
296 authenticatedIdentifier: 'user',
297 };
298 ctx.url = 'https://app.example.com/this_page';
299 const result = th.htmlPage(pagePathLevel, ctx, options, main);
300 await lintHtml.lint(result);
301 assert(result);
302 });
303 it('covers existing navLinks', async function () {
304 ctx.session = {
305 authenticatedIdentifier: 'user',
306 };
307 options.navLinks = [{
308 text: 'link',
309 href: 'link',
310 }];
311 const result = th.htmlPage(pagePathLevel, ctx, options);
312 await lintHtml.lint(result);
313 assert(result);
314 });
315 }); // htmlPage
316
317 });