a21b141eaeec7e37c3429fa9b1ea2d268fd4bbcc
[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 lint = require('html-minifier-lint').lint; // eslint-disable-line node/no-unpublished-require
7 const stubLogger = require('../stub-logger');
8
9 function lintHtml(html) {
10 const result = lint(html);
11 stubLogger.debug('validHtml', '', { result, html });
12 assert(!result);
13 }
14
15 describe('Template Helper', function () {
16 let ctx, options, pagePathLevel;
17
18 beforeEach(function () {
19 pagePathLevel = 2;
20 ctx = {};
21 options = {};
22 });
23
24 describe('dateOrNot', function () {
25 let date, otherwise;
26 beforeEach(function () {
27 date = new Date();
28 otherwise = 'otherwise';
29 });
30 it('covers', function () {
31 const result = th.dateOrNot(date, otherwise);
32 assert.strictEqual(result, date.toString());
33 });
34 it('covers no date', function () {
35 date = undefined;
36 const result = th.dateOrNot(date, otherwise);
37 assert.strictEqual(result, otherwise);
38 });
39 it('covers ms', function () {
40 const result = th.dateOrNot(date.getTime(), otherwise);
41 assert.strictEqual(result, date.toString());
42 });
43 it('covers naught', function () {
44 const result = th.dateOrNot(0, otherwise);
45 assert.strictEqual(result, otherwise);
46 });
47 it('covers the infinite', function () {
48 const result = th.dateOrNot(-Infinity, otherwise);
49 assert.strictEqual(result, otherwise);
50 });
51 }); // dateOrNot
52
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);
58 });
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);
63 });
64 it('renders Infinity', function () {
65 const expected = 'end of time';
66 const result = th.dateFormat(Infinity, expected);
67 assert.strictEqual(result, expected);
68 });
69 it('renders -Infinity', function () {
70 const expected = 'beginning of time';
71 const result = th.dateFormat(-Infinity, undefined, expected);
72 assert.strictEqual(result, expected);
73 });
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);
78 });
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);
83 });
84 }); // dateFormat
85
86 describe('secondsToPeriod', function () {
87 it('covers seconds', function () {
88 const result = th.secondsToPeriod(45);
89 assert.strictEqual(result, '45 seconds');
90 });
91 it('covers minutes', function () {
92 const result = th.secondsToPeriod(105);
93 assert.strictEqual(result, '1 minute 45 seconds');
94 });
95 it('covers hours', function () {
96 const result = th.secondsToPeriod(3705);
97 assert.strictEqual(result, '1 hour 1 minute 45 seconds');
98 });
99 it('covers days', function () {
100 const result = th.secondsToPeriod(90105);
101 assert.strictEqual(result, '1 day 1 hour 1 minute 45 seconds');
102 });
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');
106 });
107 }); // secondsToPeriod
108
109 describe('htmlHead', function () {
110 it('covers', function () {
111 const result = th.htmlHead(pagePathLevel, ctx, options);
112 assert(result);
113 });
114 it('covers elements', function () {
115 options.headElements = [ '<div>foop</div>', '<div>poof</div>' ];
116 const result = th.htmlHead(pagePathLevel, ctx, options);
117 assert(result);
118 });
119 it('covers onLoad', function () {
120 options.onload = 'onLoadFn';
121 const result = th.htmlHead(pagePathLevel, ctx, options);
122 assert(result);
123 });
124 }); // htmlHead
125
126 describe('renderNavLink', function () {
127 let nav;
128 beforeEach(function () {
129 nav = {
130 href: 'https://example.com/',
131 text: 'example',
132 };
133 });
134 it('covers no class', function () {
135 const result = th.renderNavLink(nav);
136 assert(result);
137 });
138 it('covers class', function () {
139 nav.class = 'foo bar';
140 const result = th.renderNavLink(nav);
141 assert(result);
142 });
143 }); // renderNavLink
144
145 describe('OL', function () {
146 it('covers', function () {
147 const result = th.OL(['item', 'another item'], 1, { class: 'class' }, (item) => ({ class: `${item}-class` }));
148 assert(result);
149 });
150 it('covers defaults', function () {
151 const result = th.OL([]);
152 assert(result);
153 });
154 }); // OL
155
156 describe('UL', function () {
157 it('covers', function () {
158 const result = th.UL(['item', 'another item'], 1, { class: 'class' }, (item) => ({ class: `${item}-class` }));
159 assert(result);
160 });
161 it('covers defaults', function () {
162 const result = th.UL([]);
163 assert(result);
164 });
165 }); // UL
166
167 describe('LI', function () {
168 it('covers defaults', function () {
169 const result = th.LI('item');
170 assert(result);
171 });
172 }); // LI
173
174 describe('htmlBody', function () {
175 it('covers no main', function () {
176 const result = th.htmlBody(pagePathLevel, ctx, options);
177 assert(result);
178 });
179 }); // htmlBody
180
181 describe('htmlHeader', function () {
182 it('covers no links', function () {
183 const result = th.htmlHeader(pagePathLevel, ctx, options);
184 assert(result);
185 });
186 it('covers links and logo', function () {
187 options.navLinks = [
188 {
189 href: 'https://exmaple.com/',
190 text: 'example',
191 },
192 ];
193 options.logoUrl = '/static/logo.svg';
194 const result = th.htmlHeader(pagePathLevel, ctx, options);
195 assert(result);
196 });
197 }); // htmlHeader
198
199 describe('htmlFooter', function () {
200 it('covers', function () {
201 options.footerEntries = [
202 '<div>foop</div>',
203 '<div>blah</div>',
204 ]
205 const result = th.htmlFooter(ctx, options);
206 assert(result);
207 });
208 it('covers default', function () {
209 const result = th.htmlFooter(ctx, options);
210 assert(!result);
211 });
212 }); // htmlFooter
213
214 describe('htmlMessages', function () {
215 it('covers', function () {
216 ctx.errors = ['an error'];
217 ctx.notifications = ['a notification'];
218 options.errorHeading = 'Errors';
219 options.notificationHeading = 'Notices';
220 options.errorContent = ['<p>Message about errors.</p>'];
221 options.notificationContent = ['<p>Message about notifications.</p>'];
222 const result = th.htmlMessages(ctx, options);
223 assert(result);
224 });
225 }); // htmlMessages
226
227 describe('htmlPage', function () {
228 let main;
229 beforeEach(function () {
230 main = [];
231 });
232 it('covers', function () {
233 const result = th.htmlPage(pagePathLevel, ctx, options, main);
234 lintHtml(result);
235 assert(result);
236 });
237 it('covers defaults', function () {
238 const result = th.htmlPage(pagePathLevel, ctx, options, main);
239 lintHtml(result);
240 assert(result);
241 });
242 it('covers user', function () {
243 ctx.session = {
244 authenticatedProfile: 'https://user.example.com/',
245 };
246 const result = th.htmlPage(pagePathLevel, ctx, options, main);
247 lintHtml(result);
248 assert(result);
249 });
250 it('covers user at root path', function () {
251 ctx.session = {
252 authenticatedIdentifier: 'user',
253 };
254 pagePathLevel = 0;
255 const result = th.htmlPage(pagePathLevel, ctx, options, main);
256 lintHtml(result);
257 assert(result);
258 });
259 it('covers logout redirect', function () {
260 ctx.session = {
261 authenticatedIdentifier: 'user',
262 };
263 ctx.url = 'https://app.example.com/this_page';
264 const result = th.htmlPage(pagePathLevel, ctx, options, main);
265 lintHtml(result);
266 assert(result);
267 });
268 it('covers existing navLinks', function () {
269 ctx.session = {
270 authenticatedIdentifier: 'user',
271 };
272 options.navLinks = [{
273 text: 'link',
274 href: 'link',
275 }];
276 const result = th.htmlPage(pagePathLevel, ctx, options);
277 lintHtml(result);
278 assert(result);
279 });
280 }); // htmlPage
281
282 });