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('secondsToPeriod', function () {
54 it('covers seconds', function () {
55 const result
= th
.secondsToPeriod(45);
56 assert
.strictEqual(result
, '45 seconds');
58 it('covers minutes', function () {
59 const result
= th
.secondsToPeriod(105);
60 assert
.strictEqual(result
, '1 minute 45 seconds');
62 it('covers hours', function () {
63 const result
= th
.secondsToPeriod(3705);
64 assert
.strictEqual(result
, '1 hour 1 minute 45 seconds');
66 it('covers days', function () {
67 const result
= th
.secondsToPeriod(90105);
68 assert
.strictEqual(result
, '1 day 1 hour 1 minute 45 seconds');
70 it('covers months', function () {
71 const result
= th
.secondsToPeriod(5274105);
72 assert
.strictEqual(result
, '2 months 1 day 1 hour 1 minute 45 seconds');
74 }); // secondsToPeriod
76 describe('htmlHead', function () {
77 it('covers', function () {
78 const result
= th
.htmlHead(pagePathLevel
, ctx
, options
);
81 it('covers elements', function () {
82 options
.headElements
= [ '<div>foop</div>', '<div>poof</div>' ];
83 const result
= th
.htmlHead(pagePathLevel
, ctx
, options
);
86 it('covers onLoad', function () {
87 options
.onload
= 'onLoadFn';
88 const result
= th
.htmlHead(pagePathLevel
, ctx
, options
);
93 describe('renderNavLink', function () {
95 beforeEach(function () {
97 href: 'https://example.com/',
101 it('covers no class', function () {
102 const result
= th
.renderNavLink(nav
);
105 it('covers class', function () {
106 nav
.class = 'foo bar';
107 const result
= th
.renderNavLink(nav
);
112 describe('OL', function () {
113 it('covers', function () {
114 const result
= th
.OL(['item', 'another item'], 1, { class: 'class' }, (item
) => ({ class: `${item}-class` }));
117 it('covers defaults', function () {
118 const result
= th
.OL([]);
123 describe('UL', function () {
124 it('covers', function () {
125 const result
= th
.UL(['item', 'another item'], 1, { class: 'class' }, (item
) => ({ class: `${item}-class` }));
128 it('covers defaults', function () {
129 const result
= th
.UL([]);
134 describe('LI', function () {
135 it('covers defaults', function () {
136 const result
= th
.LI('item');
141 describe('htmlBody', function () {
142 it('covers no main', function () {
143 const result
= th
.htmlBody(pagePathLevel
, ctx
, options
);
148 describe('htmlHeader', function () {
149 it('covers no links', function () {
150 const result
= th
.htmlHeader(pagePathLevel
, ctx
, options
);
153 it('covers links and logo', function () {
156 href: 'https://exmaple.com/',
160 options
.logoUrl
= '/static/logo.svg';
161 const result
= th
.htmlHeader(pagePathLevel
, ctx
, options
);
166 describe('htmlFooter', function () {
167 it('covers', function () {
168 options
.footerEntries
= [
172 const result
= th
.htmlFooter(ctx
, options
);
175 it('covers default', function () {
176 const result
= th
.htmlFooter(ctx
, options
);
181 describe('htmlMessages', function () {
182 it('covers', function () {
183 ctx
.errors
= ['an error'];
184 ctx
.notifications
= ['a notification'];
185 options
.errorHeading
= 'Errors';
186 options
.notificationHeading
= 'Notices';
187 options
.errorContent
= ['<p>Message about errors.</p>'];
188 options
.notificationContent
= ['<p>Message about notifications.</p>'];
189 const result
= th
.htmlMessages(ctx
, options
);
194 describe('htmlPage', function () {
196 beforeEach(function () {
199 it('covers', function () {
200 const result
= th
.htmlPage(pagePathLevel
, ctx
, options
, main
);
204 it('covers defaults', function () {
205 const result
= th
.htmlPage(pagePathLevel
, ctx
, options
, main
);
209 it('covers user', function () {
211 authenticatedProfile: 'https://user.example.com/',
213 const result
= th
.htmlPage(pagePathLevel
, ctx
, options
, main
);
217 it('covers user at root path', function () {
219 authenticatedIdentifier: 'user',
222 const result
= th
.htmlPage(pagePathLevel
, ctx
, options
, main
);
226 it('covers logout redirect', function () {
228 authenticatedIdentifier: 'user',
230 ctx
.url
= 'https://app.example.com/this_page';
231 const result
= th
.htmlPage(pagePathLevel
, ctx
, options
, main
);
235 it('covers existing navLinks', function () {
237 authenticatedIdentifier: 'user',
239 options
.navLinks
= [{
243 const result
= th
.htmlPage(pagePathLevel
, ctx
, options
);