c0cd94851343de6a757fd7912ed85b72b7a4a6c7
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('renders Infinity', function () {
60 const expected
= 'end of time';
61 const result
= th
.dateFormat(Infinity
, expected
);
62 assert
.strictEqual(result
, expected
);
64 it('renders -Infinity', function () {
65 const expected
= 'beginning of time';
66 const result
= th
.dateFormat(-Infinity
, undefined, expected
);
67 assert
.strictEqual(result
, expected
);
69 it('renders a Date', function () {
70 const expected
= 'Mar 27, 2022, 3:28:05 PM PDT';
71 const result
= th
.dateFormat(new Date('2022-03-27T22:28:05.049Z'));
72 assert
.strictEqual(result
, expected
);
74 it('renders a timestamp', function () {
75 const expected
= 'Mar 27, 2022, 3:28:05 PM PDT';
76 const result
= th
.dateFormat(1648420085049);
77 assert
.strictEqual(result
, expected
);
81 describe('secondsToPeriod', function () {
82 it('covers seconds', function () {
83 const result
= th
.secondsToPeriod(45);
84 assert
.strictEqual(result
, '45 seconds');
86 it('covers minutes', function () {
87 const result
= th
.secondsToPeriod(105);
88 assert
.strictEqual(result
, '1 minute 45 seconds');
90 it('covers hours', function () {
91 const result
= th
.secondsToPeriod(3705);
92 assert
.strictEqual(result
, '1 hour 1 minute 45 seconds');
94 it('covers days', function () {
95 const result
= th
.secondsToPeriod(90105);
96 assert
.strictEqual(result
, '1 day 1 hour 1 minute 45 seconds');
98 it('covers months', function () {
99 const result
= th
.secondsToPeriod(5274105);
100 assert
.strictEqual(result
, '2 months 1 day 1 hour 1 minute 45 seconds');
102 }); // secondsToPeriod
104 describe('htmlHead', function () {
105 it('covers', function () {
106 const result
= th
.htmlHead(pagePathLevel
, ctx
, options
);
109 it('covers elements', function () {
110 options
.headElements
= [ '<div>foop</div>', '<div>poof</div>' ];
111 const result
= th
.htmlHead(pagePathLevel
, ctx
, options
);
114 it('covers onLoad', function () {
115 options
.onload
= 'onLoadFn';
116 const result
= th
.htmlHead(pagePathLevel
, ctx
, options
);
121 describe('renderNavLink', function () {
123 beforeEach(function () {
125 href: 'https://example.com/',
129 it('covers no class', function () {
130 const result
= th
.renderNavLink(nav
);
133 it('covers class', function () {
134 nav
.class = 'foo bar';
135 const result
= th
.renderNavLink(nav
);
140 describe('OL', function () {
141 it('covers', function () {
142 const result
= th
.OL(['item', 'another item'], 1, { class: 'class' }, (item
) => ({ class: `${item}-class` }));
145 it('covers defaults', function () {
146 const result
= th
.OL([]);
151 describe('UL', function () {
152 it('covers', function () {
153 const result
= th
.UL(['item', 'another item'], 1, { class: 'class' }, (item
) => ({ class: `${item}-class` }));
156 it('covers defaults', function () {
157 const result
= th
.UL([]);
162 describe('LI', function () {
163 it('covers defaults', function () {
164 const result
= th
.LI('item');
169 describe('htmlBody', function () {
170 it('covers no main', function () {
171 const result
= th
.htmlBody(pagePathLevel
, ctx
, options
);
176 describe('htmlHeader', function () {
177 it('covers no links', function () {
178 const result
= th
.htmlHeader(pagePathLevel
, ctx
, options
);
181 it('covers links and logo', function () {
184 href: 'https://exmaple.com/',
188 options
.logoUrl
= '/static/logo.svg';
189 const result
= th
.htmlHeader(pagePathLevel
, ctx
, options
);
194 describe('htmlFooter', function () {
195 it('covers', function () {
196 options
.footerEntries
= [
200 const result
= th
.htmlFooter(ctx
, options
);
203 it('covers default', function () {
204 const result
= th
.htmlFooter(ctx
, options
);
209 describe('htmlMessages', function () {
210 it('covers', function () {
211 ctx
.errors
= ['an error'];
212 ctx
.notifications
= ['a notification'];
213 options
.errorHeading
= 'Errors';
214 options
.notificationHeading
= 'Notices';
215 options
.errorContent
= ['<p>Message about errors.</p>'];
216 options
.notificationContent
= ['<p>Message about notifications.</p>'];
217 const result
= th
.htmlMessages(ctx
, options
);
222 describe('htmlPage', function () {
224 beforeEach(function () {
227 it('covers', function () {
228 const result
= th
.htmlPage(pagePathLevel
, ctx
, options
, main
);
232 it('covers defaults', function () {
233 const result
= th
.htmlPage(pagePathLevel
, ctx
, options
, main
);
237 it('covers user', function () {
239 authenticatedProfile: 'https://user.example.com/',
241 const result
= th
.htmlPage(pagePathLevel
, ctx
, options
, main
);
245 it('covers user at root path', function () {
247 authenticatedIdentifier: 'user',
250 const result
= th
.htmlPage(pagePathLevel
, ctx
, options
, main
);
254 it('covers logout redirect', function () {
256 authenticatedIdentifier: 'user',
258 ctx
.url
= 'https://app.example.com/this_page';
259 const result
= th
.htmlPage(pagePathLevel
, ctx
, options
, main
);
263 it('covers existing navLinks', function () {
265 authenticatedIdentifier: 'user',
267 options
.navLinks
= [{
271 const result
= th
.htmlPage(pagePathLevel
, ctx
, options
);