75481adfee0b010330cc89a39656fd7f28e862ef
[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
8 const { makeHtmlLint } = require('../lint-html');
9 const { HtmlValidate } = require('html-validate');
10 const htmlValidate = new HtmlValidate();
11 const htmlLint = makeHtmlLint(stubLogger, htmlValidate);
12
13 describe('Template Helper', function () {
14 let ctx, options, pagePathLevel;
15
16 beforeEach(function () {
17 pagePathLevel = 2;
18 ctx = {};
19 options = {};
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 th.initContext(ctx);
266 ctx.errors.push('an error');
267 ctx.notifications.push('a notice');
268 options.headElements = ['<link rel="author" href="https://example.com/">'];
269 options.pageTitle = 'Test Page';
270 main = [
271 th.UL(['an item', 'another item']),
272 th.timeElement(new Date(), { title: 'now' }),
273 ];
274 });
275 it('covers', async function () {
276 const result = th.htmlPage(pagePathLevel, ctx, options, main);
277 await htmlLint(result);
278 assert(result);
279 });
280 it('covers defaults', async function () {
281 const result = th.htmlPage(pagePathLevel, ctx, options, main);
282 await htmlLint(result);
283 assert(result);
284 });
285 it('covers user', async function () {
286 ctx.session = {
287 authenticatedProfile: 'https://user.example.com/',
288 };
289 const result = th.htmlPage(pagePathLevel, ctx, options, main);
290 await htmlLint(result);
291 assert(result);
292 });
293 it('covers user at root path', async function () {
294 ctx.session = {
295 authenticatedIdentifier: 'user',
296 };
297 pagePathLevel = 0;
298 const result = th.htmlPage(pagePathLevel, ctx, options, main);
299 await htmlLint(result);
300 assert(result);
301 });
302 it('covers logout redirect', async function () {
303 ctx.session = {
304 authenticatedIdentifier: 'user',
305 };
306 ctx.url = 'https://app.example.com/this_page';
307 const result = th.htmlPage(pagePathLevel, ctx, options, main);
308 await htmlLint(result);
309 assert(result);
310 });
311 it('covers existing navLinks', async function () {
312 ctx.session = {
313 authenticatedIdentifier: 'user',
314 };
315 options.navLinks = [{
316 text: 'link',
317 href: 'link',
318 }];
319 const result = th.htmlPage(pagePathLevel, ctx, options);
320 await htmlLint(result);
321 assert(result);
322 });
323 }); // htmlPage
324
325 });