add indention helper
[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('indented', function () {
175 it('covers', function () {
176 const result = th.indented(2, ['foo', 'bar']);
177 result.forEach((r) => assert(r.startsWith('\t\t')));
178 });
179 }); // indented
180
181 describe('htmlBody', function () {
182 it('covers no main', function () {
183 const result = th.htmlBody(pagePathLevel, ctx, options);
184 assert(result);
185 });
186 }); // htmlBody
187
188 describe('htmlHeader', function () {
189 it('covers no links', function () {
190 const result = th.htmlHeader(pagePathLevel, ctx, options);
191 assert(result);
192 });
193 it('covers links and logo', function () {
194 options.navLinks = [
195 {
196 href: 'https://exmaple.com/',
197 text: 'example',
198 },
199 ];
200 options.logoUrl = '/static/logo.svg';
201 const result = th.htmlHeader(pagePathLevel, ctx, options);
202 assert(result);
203 });
204 }); // htmlHeader
205
206 describe('htmlFooter', function () {
207 it('covers', function () {
208 options.footerEntries = [
209 '<div>foop</div>',
210 '<div>blah</div>',
211 ]
212 const result = th.htmlFooter(ctx, options);
213 assert(result);
214 });
215 it('covers default', function () {
216 const result = th.htmlFooter(ctx, options);
217 assert(!result);
218 });
219 }); // htmlFooter
220
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);
230 assert(result);
231 });
232 }); // htmlMessages
233
234 describe('htmlPage', function () {
235 let main;
236 beforeEach(function () {
237 main = [];
238 });
239 it('covers', function () {
240 const result = th.htmlPage(pagePathLevel, ctx, options, main);
241 lintHtml(result);
242 assert(result);
243 });
244 it('covers defaults', function () {
245 const result = th.htmlPage(pagePathLevel, ctx, options, main);
246 lintHtml(result);
247 assert(result);
248 });
249 it('covers user', function () {
250 ctx.session = {
251 authenticatedProfile: 'https://user.example.com/',
252 };
253 const result = th.htmlPage(pagePathLevel, ctx, options, main);
254 lintHtml(result);
255 assert(result);
256 });
257 it('covers user at root path', function () {
258 ctx.session = {
259 authenticatedIdentifier: 'user',
260 };
261 pagePathLevel = 0;
262 const result = th.htmlPage(pagePathLevel, ctx, options, main);
263 lintHtml(result);
264 assert(result);
265 });
266 it('covers logout redirect', function () {
267 ctx.session = {
268 authenticatedIdentifier: 'user',
269 };
270 ctx.url = 'https://app.example.com/this_page';
271 const result = th.htmlPage(pagePathLevel, ctx, options, main);
272 lintHtml(result);
273 assert(result);
274 });
275 it('covers existing navLinks', function () {
276 ctx.session = {
277 authenticatedIdentifier: 'user',
278 };
279 options.navLinks = [{
280 text: 'link',
281 href: 'link',
282 }];
283 const result = th.htmlPage(pagePathLevel, ctx, options);
284 lintHtml(result);
285 assert(result);
286 });
287 }); // htmlPage
288
289 });