4 const assert
= require('assert');
5 const th
= require('../../lib/template-helper');
6 const lint
= require('html-minifier-lint').lint
; // eslint-disable-line node/no-unpublished-require
7 const stubLogger
= require('../stub-logger');
9 function lintHtml(html
) {
10 const result
= lint(html
);
11 stubLogger
.debug('validHtml', '', { result
, html
});
15 describe('Template Helper', function () {
16 let ctx
, options
, pagePathLevel
;
18 beforeEach(function () {
24 describe('dateOrNot', function () {
26 beforeEach(function () {
28 otherwise
= 'otherwise';
30 it('covers', function () {
31 const result
= th
.dateOrNot(date
, otherwise
);
32 assert
.strictEqual(result
, date
.toString());
34 it('covers no date', function () {
36 const result
= th
.dateOrNot(date
, otherwise
);
37 assert
.strictEqual(result
, otherwise
);
39 it('covers ms', function () {
40 const result
= th
.dateOrNot(date
.getTime(), otherwise
);
41 assert
.strictEqual(result
, date
.toString());
43 it('covers naught', function () {
44 const result
= th
.dateOrNot(0, otherwise
);
45 assert
.strictEqual(result
, otherwise
);
47 it('covers the infinite', function () {
48 const result
= th
.dateOrNot(-Infinity
, otherwise
);
49 assert
.strictEqual(result
, otherwise
);
53 describe('dateFormat', function () {
54 it('renders otherwise', function () {
55 const expected
= 'otherwise';
56 const result
= th
.dateFormat(undefined, undefined, undefined, expected
);
57 assert
.strictEqual(result
, expected
);
59 it('handles invalid date', function () {
60 const expected
= 'otherwise';
61 const result
= th
.dateFormat(new Date('bad date'), undefined, undefined, expected
);
62 assert
.strictEqual(result
, expected
);
64 it('renders Infinity', function () {
65 const expected
= 'end of time';
66 const result
= th
.dateFormat(Infinity
, expected
);
67 assert
.strictEqual(result
, expected
);
69 it('renders -Infinity', function () {
70 const expected
= 'beginning of time';
71 const result
= th
.dateFormat(-Infinity
, undefined, expected
);
72 assert
.strictEqual(result
, expected
);
74 it('renders a Date', function () {
75 const expected
= 'Mar 27, 2022, 3:28:05 PM PDT';
76 const result
= th
.dateFormat(new Date('2022-03-27T22:28:05.049Z'));
77 assert
.strictEqual(result
, expected
);
79 it('renders a timestamp', function () {
80 const expected
= 'Mar 27, 2022, 3:28:05 PM PDT';
81 const result
= th
.dateFormat(1648420085049);
82 assert
.strictEqual(result
, expected
);
86 describe('secondsToPeriod', function () {
87 it('covers seconds', function () {
88 const result
= th
.secondsToPeriod(45);
89 assert
.strictEqual(result
, '45 seconds');
91 it('covers minutes', function () {
92 const result
= th
.secondsToPeriod(105);
93 assert
.strictEqual(result
, '1 minute 45 seconds');
95 it('covers hours', function () {
96 const result
= th
.secondsToPeriod(3705);
97 assert
.strictEqual(result
, '1 hour 1 minute 45 seconds');
99 it('covers days', function () {
100 const result
= th
.secondsToPeriod(90105);
101 assert
.strictEqual(result
, '1 day 1 hour 1 minute 45 seconds');
103 it('covers months', function () {
104 const result
= th
.secondsToPeriod(5274105);
105 assert
.strictEqual(result
, '2 months 1 day 1 hour 1 minute 45 seconds');
107 }); // secondsToPeriod
109 describe('htmlHead', function () {
110 it('covers', function () {
111 const result
= th
.htmlHead(pagePathLevel
, ctx
, options
);
114 it('covers elements', function () {
115 options
.headElements
= [ '<div>foop</div>', '<div>poof</div>' ];
116 const result
= th
.htmlHead(pagePathLevel
, ctx
, options
);
119 it('covers onLoad', function () {
120 options
.onload
= 'onLoadFn';
121 const result
= th
.htmlHead(pagePathLevel
, ctx
, options
);
126 describe('renderNavLink', function () {
128 beforeEach(function () {
130 href: 'https://example.com/',
134 it('covers no class', function () {
135 const result
= th
.renderNavLink(nav
);
138 it('covers class', function () {
139 nav
.class = 'foo bar';
140 const result
= th
.renderNavLink(nav
);
145 describe('OL', function () {
146 it('covers', function () {
147 const result
= th
.OL(['item', 'another item'], 1, { class: 'class' }, (item
) => ({ class: `${item}-class` }));
150 it('covers defaults', function () {
151 const result
= th
.OL([]);
156 describe('UL', function () {
157 it('covers', function () {
158 const result
= th
.UL(['item', 'another item'], 1, { class: 'class' }, (item
) => ({ class: `${item}-class` }));
161 it('covers defaults', function () {
162 const result
= th
.UL([]);
167 describe('LI', function () {
168 it('covers defaults', function () {
169 const result
= th
.LI('item');
174 describe('indented', function () {
175 it('covers', function () {
176 const result
= th
.indented(2, ['foo', 'bar']);
177 result
.forEach((r
) => assert(r
.startsWith('\t\t')));
181 describe('htmlBody', function () {
182 it('covers no main', function () {
183 const result
= th
.htmlBody(pagePathLevel
, ctx
, options
);
188 describe('htmlHeader', function () {
189 it('covers no links', function () {
190 const result
= th
.htmlHeader(pagePathLevel
, ctx
, options
);
193 it('covers links and logo', function () {
196 href: 'https://exmaple.com/',
200 options
.logoUrl
= '/static/logo.svg';
201 const result
= th
.htmlHeader(pagePathLevel
, ctx
, options
);
206 describe('htmlFooter', function () {
207 it('covers', function () {
208 options
.footerEntries
= [
212 const result
= th
.htmlFooter(ctx
, options
);
215 it('covers default', function () {
216 const result
= th
.htmlFooter(ctx
, options
);
221 describe('htmlMessages', function () {
222 it('covers', function () {
223 ctx
.errors
= ['an error'];
224 ctx
.notifications
= ['a notification'];
225 options
.errorHeading
= 'Errors';
226 options
.notificationHeading
= 'Notices';
227 options
.errorContent
= ['<p>Message about errors.</p>'];
228 options
.notificationContent
= ['<p>Message about notifications.</p>'];
229 const result
= th
.htmlMessages(ctx
, options
);
234 describe('htmlPage', function () {
236 beforeEach(function () {
239 it('covers', function () {
240 const result
= th
.htmlPage(pagePathLevel
, ctx
, options
, main
);
244 it('covers defaults', function () {
245 const result
= th
.htmlPage(pagePathLevel
, ctx
, options
, main
);
249 it('covers user', function () {
251 authenticatedProfile: 'https://user.example.com/',
253 const result
= th
.htmlPage(pagePathLevel
, ctx
, options
, main
);
257 it('covers user at root path', function () {
259 authenticatedIdentifier: 'user',
262 const result
= th
.htmlPage(pagePathLevel
, ctx
, options
, main
);
266 it('covers logout redirect', function () {
268 authenticatedIdentifier: 'user',
270 ctx
.url
= 'https://app.example.com/this_page';
271 const result
= th
.htmlPage(pagePathLevel
, ctx
, options
, main
);
275 it('covers existing navLinks', function () {
277 authenticatedIdentifier: 'user',
279 options
.navLinks
= [{
283 const result
= th
.htmlPage(pagePathLevel
, ctx
, options
);