75481adfee0b010330cc89a39656fd7f28e862ef
4 const assert
= require('assert');
5 const th
= require('../../lib/template-helper');
6 const stubLogger
= require('../stub-logger');
8 const { makeHtmlLint
} = require('../lint-html');
9 const { HtmlValidate
} = require('html-validate');
10 const htmlValidate
= new HtmlValidate();
11 const htmlLint
= makeHtmlLint(stubLogger
, htmlValidate
);
13 describe('Template Helper', function () {
14 let ctx
, options
, pagePathLevel
;
16 beforeEach(function () {
22 describe('initContext', function () {
23 it('covers', function () {
26 assert(ctx
.notifications
);
27 assert(Array
.isArray(ctx
.errors
));
28 assert(Array
.isArray(ctx
.notifications
));
32 describe('dateOrNot', function () {
34 beforeEach(function () {
36 otherwise
= 'otherwise';
38 it('covers', function () {
39 const result
= th
.dateOrNot(date
, otherwise
);
40 assert
.strictEqual(result
, date
.toString());
42 it('covers no date', function () {
44 const result
= th
.dateOrNot(date
, otherwise
);
45 assert
.strictEqual(result
, otherwise
);
47 it('covers ms', function () {
48 const result
= th
.dateOrNot(date
.getTime(), otherwise
);
49 assert
.strictEqual(result
, date
.toString());
51 it('covers naught', function () {
52 const result
= th
.dateOrNot(0, otherwise
);
53 assert
.strictEqual(result
, otherwise
);
55 it('covers the infinite', function () {
56 const result
= th
.dateOrNot(-Infinity
, otherwise
);
57 assert
.strictEqual(result
, otherwise
);
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
);
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
);
72 it('renders Infinity', function () {
73 const expected
= 'end of time';
74 const result
= th
.dateFormat(Infinity
, expected
);
75 assert
.strictEqual(result
, expected
);
77 it('renders -Infinity', function () {
78 const expected
= 'beginning of time';
79 const result
= th
.dateFormat(-Infinity
, undefined, expected
);
80 assert
.strictEqual(result
, expected
);
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
);
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
);
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
);
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
);
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
);
114 describe('secondsToPeriod', function () {
115 it('covers seconds', function () {
116 const result
= th
.secondsToPeriod(45);
117 assert
.strictEqual(result
, '45 seconds');
119 it('covers minutes', function () {
120 const result
= th
.secondsToPeriod(105);
121 assert
.strictEqual(result
, '1 minute 45 seconds');
123 it('covers hours', function () {
124 const result
= th
.secondsToPeriod(3705);
125 assert
.strictEqual(result
, '1 hour 1 minute 45 seconds');
127 it('covers days', function () {
128 const result
= th
.secondsToPeriod(90105);
129 assert
.strictEqual(result
, '1 day 1 hour 1 minute 45 seconds');
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');
135 }); // secondsToPeriod
137 describe('htmlHead', function () {
138 it('covers', function () {
139 const result
= th
.htmlHead(pagePathLevel
, ctx
, options
);
142 it('covers elements', function () {
143 options
.headElements
= [ '<div>foop</div>', '<div>poof</div>' ];
144 const result
= th
.htmlHead(pagePathLevel
, ctx
, options
);
147 it('covers onLoad', function () {
148 options
.onload
= 'onLoadFn';
149 const result
= th
.htmlHead(pagePathLevel
, ctx
, options
);
154 describe('renderNavLink', function () {
156 beforeEach(function () {
158 href: 'https://example.com/',
162 it('covers no class', function () {
163 const result
= th
.renderNavLink(nav
);
166 it('covers class', function () {
167 nav
.class = 'foo bar';
168 const result
= th
.renderNavLink(nav
);
173 describe('OL', function () {
174 it('covers', function () {
175 const result
= th
.OL(['item', 'another item'], 1, { class: 'class' }, (item
) => ({ class: `${item}-class` }));
178 it('covers defaults', function () {
179 const result
= th
.OL([]);
184 describe('UL', function () {
185 it('covers', function () {
186 const result
= th
.UL(['item', 'another item'], 1, { class: 'class' }, (item
) => ({ class: `${item}-class` }));
189 it('covers defaults', function () {
190 const result
= th
.UL([]);
195 describe('LI', function () {
196 it('covers defaults', function () {
197 const result
= th
.LI('item');
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')));
209 describe('htmlBody', function () {
210 it('covers no main', function () {
211 const result
= th
.htmlBody(pagePathLevel
, ctx
, options
);
216 describe('htmlHeader', function () {
217 it('covers no links', function () {
218 const result
= th
.htmlHeader(pagePathLevel
, ctx
, options
);
221 it('covers links and logo', function () {
224 href: 'https://exmaple.com/',
228 options
.logoUrl
= '/static/logo.svg';
229 const result
= th
.htmlHeader(pagePathLevel
, ctx
, options
);
234 describe('htmlFooter', function () {
235 it('covers', function () {
236 options
.footerEntries
= [
240 const result
= th
.htmlFooter(ctx
, options
);
243 it('covers default', function () {
244 const result
= th
.htmlFooter(ctx
, options
);
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
);
262 describe('htmlPage', function () {
264 beforeEach(function () {
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';
271 th
.UL(['an item', 'another item']),
272 th
.timeElement(new Date(), { title: 'now' }),
275 it('covers', async
function () {
276 const result
= th
.htmlPage(pagePathLevel
, ctx
, options
, main
);
277 await
htmlLint(result
);
280 it('covers defaults', async
function () {
281 const result
= th
.htmlPage(pagePathLevel
, ctx
, options
, main
);
282 await
htmlLint(result
);
285 it('covers user', async
function () {
287 authenticatedProfile: 'https://user.example.com/',
289 const result
= th
.htmlPage(pagePathLevel
, ctx
, options
, main
);
290 await
htmlLint(result
);
293 it('covers user at root path', async
function () {
295 authenticatedIdentifier: 'user',
298 const result
= th
.htmlPage(pagePathLevel
, ctx
, options
, main
);
299 await
htmlLint(result
);
302 it('covers logout redirect', async
function () {
304 authenticatedIdentifier: 'user',
306 ctx
.url
= 'https://app.example.com/this_page';
307 const result
= th
.htmlPage(pagePathLevel
, ctx
, options
, main
);
308 await
htmlLint(result
);
311 it('covers existing navLinks', async
function () {
313 authenticatedIdentifier: 'user',
315 options
.navLinks
= [{
319 const result
= th
.htmlPage(pagePathLevel
, ctx
, options
);
320 await
htmlLint(result
);